Allow passing a string to the window property server requests instead
[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 "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "windef.h"
29
30 #include "handle.h"
31 #include "thread.h"
32 #include "request.h"
33
34 struct semaphore
35 {
36     struct object  obj;    /* object header */
37     unsigned int   count;  /* current count */
38     unsigned int   max;    /* maximum possible count */
39 };
40
41 static void semaphore_dump( struct object *obj, int verbose );
42 static int semaphore_signaled( struct object *obj, struct thread *thread );
43 static int semaphore_satisfied( struct object *obj, struct thread *thread );
44 static int semaphore_signal( struct object *obj, unsigned int access );
45
46 static const struct object_ops semaphore_ops =
47 {
48     sizeof(struct semaphore),      /* size */
49     semaphore_dump,                /* dump */
50     add_queue,                     /* add_queue */
51     remove_queue,                  /* remove_queue */
52     semaphore_signaled,            /* signaled */
53     semaphore_satisfied,           /* satisfied */
54     semaphore_signal,              /* signal */
55     no_get_fd,                     /* get_fd */
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( sync_namespace, &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 int release_semaphore( struct semaphore *sem, unsigned int count,
83                               unsigned int *prev )
84 {
85     if (prev) *prev = sem->count;
86     if (sem->count + count < sem->count || sem->count + count > sem->max)
87     {
88         set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
89         return 0;
90     }
91     else if (sem->count)
92     {
93         /* there cannot be any thread to wake up if the count is != 0 */
94         sem->count += count;
95     }
96     else
97     {
98         sem->count = count;
99         wake_up( &sem->obj, count );
100     }
101     return 1;
102 }
103
104 static void semaphore_dump( struct object *obj, int verbose )
105 {
106     struct semaphore *sem = (struct semaphore *)obj;
107     assert( obj->ops == &semaphore_ops );
108     fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
109     dump_object_name( &sem->obj );
110     fputc( '\n', stderr );
111 }
112
113 static int semaphore_signaled( struct object *obj, struct thread *thread )
114 {
115     struct semaphore *sem = (struct semaphore *)obj;
116     assert( obj->ops == &semaphore_ops );
117     return (sem->count > 0);
118 }
119
120 static int semaphore_satisfied( struct object *obj, struct thread *thread )
121 {
122     struct semaphore *sem = (struct semaphore *)obj;
123     assert( obj->ops == &semaphore_ops );
124     assert( sem->count );
125     sem->count--;
126     return 0;  /* not abandoned */
127 }
128
129 static int semaphore_signal( struct object *obj, unsigned int access )
130 {
131     struct semaphore *sem = (struct semaphore *)obj;
132     assert( obj->ops == &semaphore_ops );
133
134     if (!(access & SEMAPHORE_MODIFY_STATE))
135     {
136         set_error( STATUS_ACCESS_DENIED );
137         return 0;
138     }
139     return release_semaphore( sem, 1, NULL );
140 }
141
142 /* create a semaphore */
143 DECL_HANDLER(create_semaphore)
144 {
145     struct semaphore *sem;
146
147     reply->handle = 0;
148     if ((sem = create_semaphore( get_req_data(), get_req_data_size(),
149                                  req->initial, req->max )))
150     {
151         reply->handle = alloc_handle( current->process, sem, req->access, req->inherit );
152         release_object( sem );
153     }
154 }
155
156 /* open a handle to a semaphore */
157 DECL_HANDLER(open_semaphore)
158 {
159     reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
160                                  &semaphore_ops, req->access, req->inherit );
161 }
162
163 /* release a semaphore */
164 DECL_HANDLER(release_semaphore)
165 {
166     struct semaphore *sem;
167
168     if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
169                                                    SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
170     {
171         release_semaphore( sem, req->count, &reply->prev_count );
172         release_object( sem );
173     }
174 }