2 * Server-side semaphore management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
41 struct object obj; /* object header */
42 unsigned int count; /* current count */
43 unsigned int max; /* maximum possible count */
46 static void semaphore_dump( struct object *obj, int verbose );
47 static int semaphore_signaled( struct object *obj, struct thread *thread );
48 static int semaphore_satisfied( struct object *obj, struct thread *thread );
49 static unsigned int semaphore_map_access( struct object *obj, unsigned int access );
50 static int semaphore_signal( struct object *obj, unsigned int access );
52 static const struct object_ops semaphore_ops =
54 sizeof(struct semaphore), /* size */
55 semaphore_dump, /* dump */
56 add_queue, /* add_queue */
57 remove_queue, /* remove_queue */
58 semaphore_signaled, /* signaled */
59 semaphore_satisfied, /* satisfied */
60 semaphore_signal, /* signal */
61 no_get_fd, /* get_fd */
62 semaphore_map_access, /* map_access */
63 default_get_sd, /* get_sd */
64 default_set_sd, /* set_sd */
65 no_lookup_name, /* lookup_name */
66 no_open_file, /* open_file */
67 no_close_handle, /* close_handle */
68 no_destroy /* destroy */
72 static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name,
73 unsigned int attr, unsigned int initial, unsigned int max,
74 const struct security_descriptor *sd )
76 struct semaphore *sem;
78 if (!max || (initial > max))
80 set_error( STATUS_INVALID_PARAMETER );
83 if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops )))
85 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
87 /* initialize it if it didn't already exist */
90 if (sd) default_set_sd( &sem->obj, sd, OWNER_SECURITY_INFORMATION|
91 GROUP_SECURITY_INFORMATION|
92 DACL_SECURITY_INFORMATION|
93 SACL_SECURITY_INFORMATION );
99 static int release_semaphore( struct semaphore *sem, unsigned int count,
102 if (prev) *prev = sem->count;
103 if (sem->count + count < sem->count || sem->count + count > sem->max)
105 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
110 /* there cannot be any thread to wake up if the count is != 0 */
116 wake_up( &sem->obj, count );
121 static void semaphore_dump( struct object *obj, int verbose )
123 struct semaphore *sem = (struct semaphore *)obj;
124 assert( obj->ops == &semaphore_ops );
125 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
126 dump_object_name( &sem->obj );
127 fputc( '\n', stderr );
130 static int semaphore_signaled( struct object *obj, struct thread *thread )
132 struct semaphore *sem = (struct semaphore *)obj;
133 assert( obj->ops == &semaphore_ops );
134 return (sem->count > 0);
137 static int semaphore_satisfied( struct object *obj, struct thread *thread )
139 struct semaphore *sem = (struct semaphore *)obj;
140 assert( obj->ops == &semaphore_ops );
141 assert( sem->count );
143 return 0; /* not abandoned */
146 static unsigned int semaphore_map_access( struct object *obj, unsigned int access )
148 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
149 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE;
150 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
151 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS;
152 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
155 static int semaphore_signal( struct object *obj, unsigned int access )
157 struct semaphore *sem = (struct semaphore *)obj;
158 assert( obj->ops == &semaphore_ops );
160 if (!(access & SEMAPHORE_MODIFY_STATE))
162 set_error( STATUS_ACCESS_DENIED );
165 return release_semaphore( sem, 1, NULL );
168 /* create a semaphore */
169 DECL_HANDLER(create_semaphore)
171 struct semaphore *sem;
172 struct unicode_str name;
173 struct directory *root = NULL;
174 const struct object_attributes *objattr = get_req_data();
175 const struct security_descriptor *sd;
179 if (!objattr_is_valid( objattr, get_req_data_size() ))
182 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
183 objattr_get_name( objattr, &name );
185 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
188 if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max, sd )))
190 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
191 reply->handle = alloc_handle( current->process, sem, req->access, req->attributes );
193 reply->handle = alloc_handle_no_access_check( current->process, sem, req->access, req->attributes );
194 release_object( sem );
197 if (root) release_object( root );
200 /* open a handle to a semaphore */
201 DECL_HANDLER(open_semaphore)
203 struct unicode_str name;
204 struct directory *root = NULL;
205 struct semaphore *sem;
207 get_req_unicode_str( &name );
208 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
211 if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops )))
213 reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes );
214 release_object( sem );
217 if (root) release_object( root );
220 /* release a semaphore */
221 DECL_HANDLER(release_semaphore)
223 struct semaphore *sem;
225 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
226 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
228 release_semaphore( sem, req->count, &reply->prev_count );
229 release_object( sem );