ntdll: Add RtlDosPathNameToRelativeNtPathName_U.
[wine.git] / server / semaphore.c
blob53b42a886df5a087f76ca6aad41f642a4e5a1bc1
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"
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <sys/types.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 static const WCHAR semaphore_name[] = {'S','e','m','a','p','h','o','r','e'};
41 struct type_descr semaphore_type =
43 { semaphore_name, sizeof(semaphore_name) }, /* name */
44 SEMAPHORE_ALL_ACCESS, /* valid_access */
45 { /* mapping */
46 STANDARD_RIGHTS_READ | SEMAPHORE_QUERY_STATE,
47 STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE,
48 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
49 SEMAPHORE_ALL_ACCESS
53 struct semaphore
55 struct object obj; /* object header */
56 unsigned int count; /* current count */
57 unsigned int max; /* maximum possible count */
60 static void semaphore_dump( struct object *obj, int verbose );
61 static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry );
62 static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry );
63 static int semaphore_signal( struct object *obj, unsigned int access );
65 static const struct object_ops semaphore_ops =
67 sizeof(struct semaphore), /* size */
68 &semaphore_type, /* type */
69 semaphore_dump, /* dump */
70 add_queue, /* add_queue */
71 remove_queue, /* remove_queue */
72 semaphore_signaled, /* signaled */
73 semaphore_satisfied, /* satisfied */
74 semaphore_signal, /* signal */
75 no_get_fd, /* get_fd */
76 default_map_access, /* map_access */
77 default_get_sd, /* get_sd */
78 default_set_sd, /* set_sd */
79 default_get_full_name, /* get_full_name */
80 no_lookup_name, /* lookup_name */
81 directory_link_name, /* link_name */
82 default_unlink_name, /* unlink_name */
83 no_open_file, /* open_file */
84 no_kernel_obj_list, /* get_kernel_obj_list */
85 no_close_handle, /* close_handle */
86 no_destroy /* destroy */
90 static struct semaphore *create_semaphore( struct object *root, const struct unicode_str *name,
91 unsigned int attr, unsigned int initial, unsigned int max,
92 const struct security_descriptor *sd )
94 struct semaphore *sem;
96 if (!max || (initial > max))
98 set_error( STATUS_INVALID_PARAMETER );
99 return NULL;
101 if ((sem = create_named_object( root, &semaphore_ops, name, attr, sd )))
103 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
105 /* initialize it if it didn't already exist */
106 sem->count = initial;
107 sem->max = max;
110 return sem;
113 static int release_semaphore( struct semaphore *sem, unsigned int count,
114 unsigned int *prev )
116 if (prev) *prev = sem->count;
117 if (sem->count + count < sem->count || sem->count + count > sem->max)
119 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
120 return 0;
122 else if (sem->count)
124 /* there cannot be any thread to wake up if the count is != 0 */
125 sem->count += count;
127 else
129 sem->count = count;
130 wake_up( &sem->obj, count );
132 return 1;
135 static void semaphore_dump( struct object *obj, int verbose )
137 struct semaphore *sem = (struct semaphore *)obj;
138 assert( obj->ops == &semaphore_ops );
139 fprintf( stderr, "Semaphore count=%d max=%d\n", sem->count, sem->max );
142 static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry )
144 struct semaphore *sem = (struct semaphore *)obj;
145 assert( obj->ops == &semaphore_ops );
146 return (sem->count > 0);
149 static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry )
151 struct semaphore *sem = (struct semaphore *)obj;
152 assert( obj->ops == &semaphore_ops );
153 assert( sem->count );
154 sem->count--;
157 static int semaphore_signal( struct object *obj, unsigned int access )
159 struct semaphore *sem = (struct semaphore *)obj;
160 assert( obj->ops == &semaphore_ops );
162 if (!(access & SEMAPHORE_MODIFY_STATE))
164 set_error( STATUS_ACCESS_DENIED );
165 return 0;
167 return release_semaphore( sem, 1, NULL );
170 /* create a semaphore */
171 DECL_HANDLER(create_semaphore)
173 struct semaphore *sem;
174 struct unicode_str name;
175 struct object *root;
176 const struct security_descriptor *sd;
177 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
179 if (!objattr) return;
181 if ((sem = create_semaphore( root, &name, objattr->attributes, req->initial, req->max, sd )))
183 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
184 reply->handle = alloc_handle( current->process, sem, req->access, objattr->attributes );
185 else
186 reply->handle = alloc_handle_no_access_check( current->process, sem,
187 req->access, objattr->attributes );
188 release_object( sem );
191 if (root) release_object( root );
194 /* open a handle to a semaphore */
195 DECL_HANDLER(open_semaphore)
197 struct unicode_str name = get_req_unicode_str();
199 reply->handle = open_object( current->process, req->rootdir, req->access,
200 &semaphore_ops, &name, req->attributes );
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 );
216 /* query details about the semaphore */
217 DECL_HANDLER(query_semaphore)
219 struct semaphore *sem;
221 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
222 SEMAPHORE_QUERY_STATE, &semaphore_ops )))
224 reply->current = sem->count;
225 reply->max = sem->max;
226 release_object( sem );