usp10: Add some tests to ScriptGetFontProperties.
[wine/multimedia.git] / server / semaphore.c
blobd81889847271d73ba377ace55876248dae10c87c
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
38 struct semaphore
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 unsigned int semaphore_map_access( struct object *obj, unsigned int access );
49 static int semaphore_signal( struct object *obj, unsigned int access );
51 static const struct object_ops semaphore_ops =
53 sizeof(struct semaphore), /* size */
54 semaphore_dump, /* dump */
55 add_queue, /* add_queue */
56 remove_queue, /* remove_queue */
57 semaphore_signaled, /* signaled */
58 semaphore_satisfied, /* satisfied */
59 semaphore_signal, /* signal */
60 no_get_fd, /* get_fd */
61 semaphore_map_access, /* map_access */
62 no_lookup_name, /* lookup_name */
63 no_close_handle, /* close_handle */
64 no_destroy /* destroy */
68 static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name,
69 unsigned int attr, unsigned int initial, unsigned int max )
71 struct semaphore *sem;
73 if (!max || (initial > max))
75 set_error( STATUS_INVALID_PARAMETER );
76 return NULL;
78 if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops )))
80 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
82 /* initialize it if it didn't already exist */
83 sem->count = initial;
84 sem->max = max;
87 return sem;
90 static int release_semaphore( struct semaphore *sem, unsigned int count,
91 unsigned int *prev )
93 if (prev) *prev = sem->count;
94 if (sem->count + count < sem->count || sem->count + count > sem->max)
96 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
97 return 0;
99 else if (sem->count)
101 /* there cannot be any thread to wake up if the count is != 0 */
102 sem->count += count;
104 else
106 sem->count = count;
107 wake_up( &sem->obj, count );
109 return 1;
112 static void semaphore_dump( struct object *obj, int verbose )
114 struct semaphore *sem = (struct semaphore *)obj;
115 assert( obj->ops == &semaphore_ops );
116 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
117 dump_object_name( &sem->obj );
118 fputc( '\n', stderr );
121 static int semaphore_signaled( struct object *obj, struct thread *thread )
123 struct semaphore *sem = (struct semaphore *)obj;
124 assert( obj->ops == &semaphore_ops );
125 return (sem->count > 0);
128 static int semaphore_satisfied( struct object *obj, struct thread *thread )
130 struct semaphore *sem = (struct semaphore *)obj;
131 assert( obj->ops == &semaphore_ops );
132 assert( sem->count );
133 sem->count--;
134 return 0; /* not abandoned */
137 static unsigned int semaphore_map_access( struct object *obj, unsigned int access )
139 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
140 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE;
141 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
142 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS;
143 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
146 static int semaphore_signal( struct object *obj, unsigned int access )
148 struct semaphore *sem = (struct semaphore *)obj;
149 assert( obj->ops == &semaphore_ops );
151 if (!(access & SEMAPHORE_MODIFY_STATE))
153 set_error( STATUS_ACCESS_DENIED );
154 return 0;
156 return release_semaphore( sem, 1, NULL );
159 /* create a semaphore */
160 DECL_HANDLER(create_semaphore)
162 struct semaphore *sem;
163 struct unicode_str name;
164 struct directory *root = NULL;
166 reply->handle = 0;
167 get_req_unicode_str( &name );
168 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
169 return;
171 if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max )))
173 reply->handle = alloc_handle( current->process, sem, req->access, req->attributes );
174 release_object( sem );
177 if (root) release_object( root );
180 /* open a handle to a semaphore */
181 DECL_HANDLER(open_semaphore)
183 struct unicode_str name;
184 struct directory *root = NULL;
185 struct semaphore *sem;
187 get_req_unicode_str( &name );
188 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
189 return;
191 if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops )))
193 reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes );
194 release_object( sem );
197 if (root) release_object( root );
200 /* release a semaphore */
201 DECL_HANDLER(release_semaphore)
203 struct semaphore *sem;
205 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
206 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
208 release_semaphore( sem, req->count, &reply->prev_count );
209 release_object( sem );