ntdll: Implement LdrRegisterDllNotification and LdrUnregisterDllNotification.
[wine.git] / server / semaphore.c
blob08ff1536ceed80de3b1280a62a7af85e70de23ee
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"
37 #include "security.h"
39 struct semaphore
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 struct object_type *semaphore_get_type( struct object *obj );
48 static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry );
49 static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry );
50 static unsigned int semaphore_map_access( struct object *obj, unsigned int access );
51 static int semaphore_signal( struct object *obj, unsigned int access );
53 static const struct object_ops semaphore_ops =
55 sizeof(struct semaphore), /* size */
56 semaphore_dump, /* dump */
57 semaphore_get_type, /* get_type */
58 add_queue, /* add_queue */
59 remove_queue, /* remove_queue */
60 semaphore_signaled, /* signaled */
61 semaphore_satisfied, /* satisfied */
62 semaphore_signal, /* signal */
63 no_get_fd, /* get_fd */
64 semaphore_map_access, /* map_access */
65 default_get_sd, /* get_sd */
66 default_set_sd, /* set_sd */
67 no_lookup_name, /* lookup_name */
68 directory_link_name, /* link_name */
69 default_unlink_name, /* unlink_name */
70 no_open_file, /* open_file */
71 no_close_handle, /* close_handle */
72 no_destroy /* destroy */
76 static struct semaphore *create_semaphore( struct object *root, const struct unicode_str *name,
77 unsigned int attr, unsigned int initial, unsigned int max,
78 const struct security_descriptor *sd )
80 struct semaphore *sem;
82 if (!max || (initial > max))
84 set_error( STATUS_INVALID_PARAMETER );
85 return NULL;
87 if ((sem = create_named_object( root, &semaphore_ops, name, attr, sd )))
89 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
91 /* initialize it if it didn't already exist */
92 sem->count = initial;
93 sem->max = max;
96 return sem;
99 static int release_semaphore( struct semaphore *sem, unsigned int count,
100 unsigned int *prev )
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 );
106 return 0;
108 else if (sem->count)
110 /* there cannot be any thread to wake up if the count is != 0 */
111 sem->count += count;
113 else
115 sem->count = count;
116 wake_up( &sem->obj, count );
118 return 1;
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\n", sem->count, sem->max );
128 static struct object_type *semaphore_get_type( struct object *obj )
130 static const WCHAR name[] = {'S','e','m','a','p','h','o','r','e'};
131 static const struct unicode_str str = { name, sizeof(name) };
132 return get_object_type( &str );
135 static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry )
137 struct semaphore *sem = (struct semaphore *)obj;
138 assert( obj->ops == &semaphore_ops );
139 return (sem->count > 0);
142 static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry )
144 struct semaphore *sem = (struct semaphore *)obj;
145 assert( obj->ops == &semaphore_ops );
146 assert( sem->count );
147 sem->count--;
150 static unsigned int semaphore_map_access( struct object *obj, unsigned int access )
152 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SEMAPHORE_QUERY_STATE;
153 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE;
154 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
155 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS;
156 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
159 static int semaphore_signal( struct object *obj, unsigned int access )
161 struct semaphore *sem = (struct semaphore *)obj;
162 assert( obj->ops == &semaphore_ops );
164 if (!(access & SEMAPHORE_MODIFY_STATE))
166 set_error( STATUS_ACCESS_DENIED );
167 return 0;
169 return release_semaphore( sem, 1, NULL );
172 /* create a semaphore */
173 DECL_HANDLER(create_semaphore)
175 struct semaphore *sem;
176 struct unicode_str name;
177 struct object *root;
178 const struct security_descriptor *sd;
179 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
181 if (!objattr) return;
183 if ((sem = create_semaphore( root, &name, objattr->attributes, req->initial, req->max, sd )))
185 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
186 reply->handle = alloc_handle( current->process, sem, req->access, objattr->attributes );
187 else
188 reply->handle = alloc_handle_no_access_check( current->process, sem,
189 req->access, objattr->attributes );
190 release_object( sem );
193 if (root) release_object( root );
196 /* open a handle to a semaphore */
197 DECL_HANDLER(open_semaphore)
199 struct unicode_str name = get_req_unicode_str();
201 reply->handle = open_object( current->process, req->rootdir, req->access,
202 &semaphore_ops, &name, req->attributes );
205 /* release a semaphore */
206 DECL_HANDLER(release_semaphore)
208 struct semaphore *sem;
210 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
211 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
213 release_semaphore( sem, req->count, &reply->prev_count );
214 release_object( sem );
218 /* query details about the semaphore */
219 DECL_HANDLER(query_semaphore)
221 struct semaphore *sem;
223 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
224 SEMAPHORE_QUERY_STATE, &semaphore_ops )))
226 reply->current = sem->count;
227 reply->max = sem->max;
228 release_object( sem );