libwine: Emulate MS linker stub for builtin dlls.
[wine/wine64.git] / server / semaphore.c
blobaf651e9b56f950982f4f7d2838c6b3672c888d38
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 default_get_sd, /* get_sd */
63 default_set_sd, /* set_sd */
64 no_lookup_name, /* lookup_name */
65 no_open_file, /* open_file */
66 no_close_handle, /* close_handle */
67 no_destroy /* destroy */
71 static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name,
72 unsigned int attr, unsigned int initial, unsigned int max )
74 struct semaphore *sem;
76 if (!max || (initial > max))
78 set_error( STATUS_INVALID_PARAMETER );
79 return NULL;
81 if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops )))
83 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
85 /* initialize it if it didn't already exist */
86 sem->count = initial;
87 sem->max = max;
90 return sem;
93 static int release_semaphore( struct semaphore *sem, unsigned int count,
94 unsigned int *prev )
96 if (prev) *prev = sem->count;
97 if (sem->count + count < sem->count || sem->count + count > sem->max)
99 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
100 return 0;
102 else if (sem->count)
104 /* there cannot be any thread to wake up if the count is != 0 */
105 sem->count += count;
107 else
109 sem->count = count;
110 wake_up( &sem->obj, count );
112 return 1;
115 static void semaphore_dump( struct object *obj, int verbose )
117 struct semaphore *sem = (struct semaphore *)obj;
118 assert( obj->ops == &semaphore_ops );
119 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
120 dump_object_name( &sem->obj );
121 fputc( '\n', stderr );
124 static int semaphore_signaled( struct object *obj, struct thread *thread )
126 struct semaphore *sem = (struct semaphore *)obj;
127 assert( obj->ops == &semaphore_ops );
128 return (sem->count > 0);
131 static int semaphore_satisfied( struct object *obj, struct thread *thread )
133 struct semaphore *sem = (struct semaphore *)obj;
134 assert( obj->ops == &semaphore_ops );
135 assert( sem->count );
136 sem->count--;
137 return 0; /* not abandoned */
140 static unsigned int semaphore_map_access( struct object *obj, unsigned int access )
142 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
143 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE;
144 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
145 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS;
146 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
149 static int semaphore_signal( struct object *obj, unsigned int access )
151 struct semaphore *sem = (struct semaphore *)obj;
152 assert( obj->ops == &semaphore_ops );
154 if (!(access & SEMAPHORE_MODIFY_STATE))
156 set_error( STATUS_ACCESS_DENIED );
157 return 0;
159 return release_semaphore( sem, 1, NULL );
162 /* create a semaphore */
163 DECL_HANDLER(create_semaphore)
165 struct semaphore *sem;
166 struct unicode_str name;
167 struct directory *root = NULL;
169 reply->handle = 0;
170 get_req_unicode_str( &name );
171 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
172 return;
174 if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max )))
176 reply->handle = alloc_handle( current->process, sem, req->access, req->attributes );
177 release_object( sem );
180 if (root) release_object( root );
183 /* open a handle to a semaphore */
184 DECL_HANDLER(open_semaphore)
186 struct unicode_str name;
187 struct directory *root = NULL;
188 struct semaphore *sem;
190 get_req_unicode_str( &name );
191 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
192 return;
194 if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops )))
196 reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes );
197 release_object( sem );
200 if (root) release_object( root );
203 /* release a semaphore */
204 DECL_HANDLER(release_semaphore)
206 struct semaphore *sem;
208 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
209 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
211 release_semaphore( sem, req->count, &reply->prev_count );
212 release_object( sem );