server: Make alloc_handle use attributes instead of inherit flag.
[wine] / server / semaphore.c
1 /*
2  * Server-side semaphore management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
37
38 struct semaphore
39 {
40     struct object  obj;    /* object header */
41     unsigned int   count;  /* current count */
42     unsigned int   max;    /* maximum possible count */
43 };
44
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 );
49
50 static const struct object_ops semaphore_ops =
51 {
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 */
63 };
64
65
66 static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name,
67                                            unsigned int attr, unsigned int initial, unsigned int max )
68 {
69     struct semaphore *sem;
70
71     if (!max || (initial > max))
72     {
73         set_error( STATUS_INVALID_PARAMETER );
74         return NULL;
75     }
76     if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops )))
77     {
78         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
79         {
80             /* initialize it if it didn't already exist */
81             sem->count = initial;
82             sem->max   = max;
83         }
84     }
85     return sem;
86 }
87
88 static int release_semaphore( struct semaphore *sem, unsigned int count,
89                               unsigned int *prev )
90 {
91     if (prev) *prev = sem->count;
92     if (sem->count + count < sem->count || sem->count + count > sem->max)
93     {
94         set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
95         return 0;
96     }
97     else if (sem->count)
98     {
99         /* there cannot be any thread to wake up if the count is != 0 */
100         sem->count += count;
101     }
102     else
103     {
104         sem->count = count;
105         wake_up( &sem->obj, count );
106     }
107     return 1;
108 }
109
110 static void semaphore_dump( struct object *obj, int verbose )
111 {
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 );
117 }
118
119 static int semaphore_signaled( struct object *obj, struct thread *thread )
120 {
121     struct semaphore *sem = (struct semaphore *)obj;
122     assert( obj->ops == &semaphore_ops );
123     return (sem->count > 0);
124 }
125
126 static int semaphore_satisfied( struct object *obj, struct thread *thread )
127 {
128     struct semaphore *sem = (struct semaphore *)obj;
129     assert( obj->ops == &semaphore_ops );
130     assert( sem->count );
131     sem->count--;
132     return 0;  /* not abandoned */
133 }
134
135 static int semaphore_signal( struct object *obj, unsigned int access )
136 {
137     struct semaphore *sem = (struct semaphore *)obj;
138     assert( obj->ops == &semaphore_ops );
139
140     if (!(access & SEMAPHORE_MODIFY_STATE))
141     {
142         set_error( STATUS_ACCESS_DENIED );
143         return 0;
144     }
145     return release_semaphore( sem, 1, NULL );
146 }
147
148 /* create a semaphore */
149 DECL_HANDLER(create_semaphore)
150 {
151     struct semaphore *sem;
152     struct unicode_str name;
153     struct directory *root = NULL;
154
155     reply->handle = 0;
156     get_req_unicode_str( &name );
157     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
158         return;
159
160     if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max )))
161     {
162         reply->handle = alloc_handle( current->process, sem, req->access, req->attributes );
163         release_object( sem );
164     }
165
166     if (root) release_object( root );
167 }
168
169 /* open a handle to a semaphore */
170 DECL_HANDLER(open_semaphore)
171 {
172     struct unicode_str name;
173     struct directory *root = NULL;
174     struct semaphore *sem;
175
176     get_req_unicode_str( &name );
177     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
178         return;
179
180     if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops )))
181     {
182         reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes );
183         release_object( sem );
184     }
185
186     if (root) release_object( root );
187 }
188
189 /* release a semaphore */
190 DECL_HANDLER(release_semaphore)
191 {
192     struct semaphore *sem;
193
194     if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
195                                                    SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
196     {
197         release_semaphore( sem, req->count, &reply->prev_count );
198         release_object( sem );
199     }
200 }