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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
40 struct object obj; /* object header */
41 unsigned int count; /* current count */
42 unsigned int max; /* maximum possible count */
45 static void semaphore_dump( struct object *obj, int verbose );
46 static int semaphore_signaled( struct object *obj, struct thread *thread );
47 static int semaphore_satisfied( struct object *obj, struct thread *thread );
48 static int semaphore_signal( struct object *obj, unsigned int access );
50 static const struct object_ops semaphore_ops =
52 sizeof(struct semaphore), /* size */
53 semaphore_dump, /* dump */
54 add_queue, /* add_queue */
55 remove_queue, /* remove_queue */
56 semaphore_signaled, /* signaled */
57 semaphore_satisfied, /* satisfied */
58 semaphore_signal, /* signal */
59 no_get_fd, /* get_fd */
60 no_lookup_name, /* lookup_name */
61 no_close_handle, /* close_handle */
62 no_destroy /* destroy */
66 static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name,
67 unsigned int attr, unsigned int initial, unsigned int max )
69 struct semaphore *sem;
71 if (!max || (initial > max))
73 set_error( STATUS_INVALID_PARAMETER );
76 if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops )))
78 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
80 /* initialize it if it didn't already exist */
88 static int release_semaphore( struct semaphore *sem, unsigned int count,
91 if (prev) *prev = sem->count;
92 if (sem->count + count < sem->count || sem->count + count > sem->max)
94 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
99 /* there cannot be any thread to wake up if the count is != 0 */
105 wake_up( &sem->obj, count );
110 static void semaphore_dump( struct object *obj, int verbose )
112 struct semaphore *sem = (struct semaphore *)obj;
113 assert( obj->ops == &semaphore_ops );
114 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
115 dump_object_name( &sem->obj );
116 fputc( '\n', stderr );
119 static int semaphore_signaled( struct object *obj, struct thread *thread )
121 struct semaphore *sem = (struct semaphore *)obj;
122 assert( obj->ops == &semaphore_ops );
123 return (sem->count > 0);
126 static int semaphore_satisfied( struct object *obj, struct thread *thread )
128 struct semaphore *sem = (struct semaphore *)obj;
129 assert( obj->ops == &semaphore_ops );
130 assert( sem->count );
132 return 0; /* not abandoned */
135 static int semaphore_signal( struct object *obj, unsigned int access )
137 struct semaphore *sem = (struct semaphore *)obj;
138 assert( obj->ops == &semaphore_ops );
140 if (!(access & SEMAPHORE_MODIFY_STATE))
142 set_error( STATUS_ACCESS_DENIED );
145 return release_semaphore( sem, 1, NULL );
148 /* create a semaphore */
149 DECL_HANDLER(create_semaphore)
151 struct semaphore *sem;
152 struct unicode_str name;
153 struct directory *root = NULL;
156 get_req_unicode_str( &name );
157 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
160 if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max )))
162 reply->handle = alloc_handle( current->process, sem, req->access, req->attributes );
163 release_object( sem );
166 if (root) release_object( root );
169 /* open a handle to a semaphore */
170 DECL_HANDLER(open_semaphore)
172 struct unicode_str name;
173 struct directory *root = NULL;
174 struct semaphore *sem;
176 get_req_unicode_str( &name );
177 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
180 if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops )))
182 reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes );
183 release_object( sem );
186 if (root) release_object( root );
189 /* release a semaphore */
190 DECL_HANDLER(release_semaphore)
192 struct semaphore *sem;
194 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
195 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
197 release_semaphore( sem, req->count, &reply->prev_count );
198 release_object( sem );