Send message for WSAAsyncSelect sockets directly from the server,
[wine] / server / semaphore.c
1 /*
2  * Server-side semaphore management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "winnt.h"
26
27 #include "handle.h"
28 #include "thread.h"
29 #include "request.h"
30
31 struct semaphore
32 {
33     struct object  obj;    /* object header */
34     unsigned int   count;  /* current count */
35     unsigned int   max;    /* maximum possible count */
36 };
37
38 static void semaphore_dump( struct object *obj, int verbose );
39 static int semaphore_signaled( struct object *obj, struct thread *thread );
40 static int semaphore_satisfied( struct object *obj, struct thread *thread );
41
42 static const struct object_ops semaphore_ops =
43 {
44     sizeof(struct semaphore),      /* size */
45     semaphore_dump,                /* dump */
46     add_queue,                     /* add_queue */
47     remove_queue,                  /* remove_queue */
48     semaphore_signaled,            /* signaled */
49     semaphore_satisfied,           /* satisfied */
50     NULL,                          /* get_poll_events */
51     NULL,                          /* poll_event */
52     no_get_fd,                     /* get_fd */
53     no_flush,                      /* flush */
54     no_get_file_info,              /* get_file_info */
55     NULL,                          /* queue_async */
56     no_destroy                     /* destroy */
57 };
58
59
60 static struct semaphore *create_semaphore( const WCHAR *name, size_t len,
61                                            unsigned int initial, unsigned int max )
62 {
63     struct semaphore *sem;
64
65     if (!max || (initial > max))
66     {
67         set_error( STATUS_INVALID_PARAMETER );
68         return NULL;
69     }
70     if ((sem = create_named_object( &semaphore_ops, name, len )))
71     {
72         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
73         {
74             /* initialize it if it didn't already exist */
75             sem->count = initial;
76             sem->max   = max;
77         }
78     }
79     return sem;
80 }
81
82 static unsigned int release_semaphore( handle_t handle, unsigned int count )
83 {
84     struct semaphore *sem;
85     unsigned int prev = 0;
86
87     if ((sem = (struct semaphore *)get_handle_obj( current->process, handle,
88                                                    SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
89     {
90         prev = sem->count;
91         if (sem->count + count < sem->count || sem->count + count > sem->max)
92         {
93             set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
94         }
95         else if (sem->count)
96         {
97             /* there cannot be any thread waiting if the count is != 0 */
98             assert( !sem->obj.head );
99             sem->count += count;
100         }
101         else
102         {
103             sem->count = count;
104             wake_up( &sem->obj, count );
105         }
106         release_object( sem );
107     }
108     return prev;
109 }
110
111 static void semaphore_dump( struct object *obj, int verbose )
112 {
113     struct semaphore *sem = (struct semaphore *)obj;
114     assert( obj->ops == &semaphore_ops );
115     fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
116     dump_object_name( &sem->obj );
117     fputc( '\n', stderr );
118 }
119
120 static int semaphore_signaled( struct object *obj, struct thread *thread )
121 {
122     struct semaphore *sem = (struct semaphore *)obj;
123     assert( obj->ops == &semaphore_ops );
124     return (sem->count > 0);
125 }
126
127 static int semaphore_satisfied( struct object *obj, struct thread *thread )
128 {
129     struct semaphore *sem = (struct semaphore *)obj;
130     assert( obj->ops == &semaphore_ops );
131     assert( sem->count );
132     sem->count--;
133     return 0;  /* not abandoned */
134 }
135
136 /* create a semaphore */
137 DECL_HANDLER(create_semaphore)
138 {
139     struct semaphore *sem;
140
141     reply->handle = 0;
142     if ((sem = create_semaphore( get_req_data(), get_req_data_size(),
143                                  req->initial, req->max )))
144     {
145         reply->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit );
146         release_object( sem );
147     }
148 }
149
150 /* open a handle to a semaphore */
151 DECL_HANDLER(open_semaphore)
152 {
153     reply->handle = open_object( get_req_data(), get_req_data_size(),
154                                  &semaphore_ops, req->access, req->inherit );
155 }
156
157 /* release a semaphore */
158 DECL_HANDLER(release_semaphore)
159 {
160     reply->prev_count = release_semaphore( req->handle, req->count );
161 }