d3dx8: Test the function D3DXVec3Cross really.
[wine] / server / semaphore.c
CommitLineData
d30dfd24
AJ
1/*
2 * Server-side semaphore management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
0799c1a7
AJ
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
360a3f91 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
d30dfd24
AJ
19 */
20
5769d1de
AJ
21#include "config.h"
22#include "wine/port.h"
23
d30dfd24
AJ
24#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
95414ef4 27#include <stdarg.h>
d30dfd24 28
1a1583a3
GG
29#include "ntstatus.h"
30#define WIN32_NO_STATUS
435e2e63 31#include "windef.h"
a996000a 32#include "winternl.h"
43c190e7
AJ
33
34#include "handle.h"
35#include "thread.h"
5bc78089 36#include "request.h"
b0e5fb43 37#include "security.h"
d30dfd24
AJ
38
39struct semaphore
40{
41 struct object obj; /* object header */
42 unsigned int count; /* current count */
43 unsigned int max; /* maximum possible count */
44};
45
338e757d 46static void semaphore_dump( struct object *obj, int verbose );
d30dfd24
AJ
47static int semaphore_signaled( struct object *obj, struct thread *thread );
48static int semaphore_satisfied( struct object *obj, struct thread *thread );
03f46e13 49static unsigned int semaphore_map_access( struct object *obj, unsigned int access );
f92fff66 50static int semaphore_signal( struct object *obj, unsigned int access );
d30dfd24
AJ
51
52static const struct object_ops semaphore_ops =
53{
1dca5e24
AJ
54 sizeof(struct semaphore), /* size */
55 semaphore_dump, /* dump */
56 add_queue, /* add_queue */
57 remove_queue, /* remove_queue */
58 semaphore_signaled, /* signaled */
59 semaphore_satisfied, /* satisfied */
f92fff66 60 semaphore_signal, /* signal */
1ab243ba 61 no_get_fd, /* get_fd */
03f46e13 62 semaphore_map_access, /* map_access */
c1707d89
RS
63 default_get_sd, /* get_sd */
64 default_set_sd, /* set_sd */
baffcb95 65 no_lookup_name, /* lookup_name */
7e71c1dd 66 no_open_file, /* open_file */
b9b1ea9c 67 no_close_handle, /* close_handle */
1dca5e24 68 no_destroy /* destroy */
d30dfd24
AJ
69};
70
71
5daae3df 72static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name,
b0e5fb43
RS
73 unsigned int attr, unsigned int initial, unsigned int max,
74 const struct security_descriptor *sd )
d30dfd24
AJ
75{
76 struct semaphore *sem;
77
78 if (!max || (initial > max))
79 {
cb1fc735 80 set_error( STATUS_INVALID_PARAMETER );
d30dfd24
AJ
81 return NULL;
82 }
5daae3df 83 if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops )))
d30dfd24 84 {
893987b8 85 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
5bc78089
AJ
86 {
87 /* initialize it if it didn't already exist */
88 sem->count = initial;
89 sem->max = max;
b0e5fb43
RS
90 if (sd) default_set_sd( &sem->obj, sd, OWNER_SECURITY_INFORMATION|
91 GROUP_SECURITY_INFORMATION|
92 DACL_SECURITY_INFORMATION|
93 SACL_SECURITY_INFORMATION );
5bc78089 94 }
d30dfd24 95 }
5bc78089 96 return sem;
d30dfd24
AJ
97}
98
f92fff66
MM
99static int release_semaphore( struct semaphore *sem, unsigned int count,
100 unsigned int *prev )
d30dfd24 101{
f92fff66
MM
102 if (prev) *prev = sem->count;
103 if (sem->count + count < sem->count || sem->count + count > sem->max)
d30dfd24 104 {
f92fff66
MM
105 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
106 return 0;
107 }
108 else if (sem->count)
109 {
110 /* there cannot be any thread to wake up if the count is != 0 */
111 sem->count += count;
112 }
113 else
114 {
115 sem->count = count;
116 wake_up( &sem->obj, count );
d30dfd24 117 }
f92fff66 118 return 1;
d30dfd24
AJ
119}
120
338e757d 121static void semaphore_dump( struct object *obj, int verbose )
d30dfd24
AJ
122{
123 struct semaphore *sem = (struct semaphore *)obj;
124 assert( obj->ops == &semaphore_ops );
d16319ce
AJ
125 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
126 dump_object_name( &sem->obj );
127 fputc( '\n', stderr );
d30dfd24
AJ
128}
129
130static int semaphore_signaled( struct object *obj, struct thread *thread )
131{
132 struct semaphore *sem = (struct semaphore *)obj;
133 assert( obj->ops == &semaphore_ops );
134 return (sem->count > 0);
135}
136
137static int semaphore_satisfied( struct object *obj, struct thread *thread )
138{
139 struct semaphore *sem = (struct semaphore *)obj;
140 assert( obj->ops == &semaphore_ops );
141 assert( sem->count );
142 sem->count--;
143 return 0; /* not abandoned */
144}
145
03f46e13
AJ
146static unsigned int semaphore_map_access( struct object *obj, unsigned int access )
147{
148 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
149 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE;
150 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
151 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | SEMAPHORE_ALL_ACCESS;
152 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
153}
154
f92fff66
MM
155static int semaphore_signal( struct object *obj, unsigned int access )
156{
157 struct semaphore *sem = (struct semaphore *)obj;
158 assert( obj->ops == &semaphore_ops );
159
160 if (!(access & SEMAPHORE_MODIFY_STATE))
161 {
162 set_error( STATUS_ACCESS_DENIED );
163 return 0;
164 }
165 return release_semaphore( sem, 1, NULL );
166}
167
43c190e7
AJ
168/* create a semaphore */
169DECL_HANDLER(create_semaphore)
170{
5bc78089 171 struct semaphore *sem;
ead9b062 172 struct unicode_str name;
5daae3df 173 struct directory *root = NULL;
b0e5fb43
RS
174 const struct object_attributes *objattr = get_req_data();
175 const struct security_descriptor *sd;
5bc78089 176
9caa71ee 177 reply->handle = 0;
b0e5fb43
RS
178
179 if (!objattr_is_valid( objattr, get_req_data_size() ))
180 return;
181
182 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
183
184 /* get unicode string */
185 name.len = ((get_req_data_size() - sizeof(*objattr) - objattr->sd_len) / sizeof(WCHAR)) * sizeof(WCHAR);
186 name.str = (const WCHAR *)get_req_data() + (sizeof(*objattr) + objattr->sd_len) / sizeof(WCHAR);
187 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
5daae3df
VM
188 return;
189
b0e5fb43 190 if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max, sd )))
43c190e7 191 {
24560e70 192 reply->handle = alloc_handle( current->process, sem, req->access, req->attributes );
5bc78089 193 release_object( sem );
43c190e7 194 }
5daae3df
VM
195
196 if (root) release_object( root );
43c190e7
AJ
197}
198
199/* open a handle to a semaphore */
200DECL_HANDLER(open_semaphore)
201{
ead9b062 202 struct unicode_str name;
5daae3df 203 struct directory *root = NULL;
3764da68 204 struct semaphore *sem;
ead9b062
AJ
205
206 get_req_unicode_str( &name );
5daae3df
VM
207 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
208 return;
209
3764da68
AJ
210 if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops )))
211 {
24560e70 212 reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes );
3764da68
AJ
213 release_object( sem );
214 }
5daae3df
VM
215
216 if (root) release_object( root );
43c190e7
AJ
217}
218
219/* release a semaphore */
220DECL_HANDLER(release_semaphore)
221{
f92fff66
MM
222 struct semaphore *sem;
223
224 if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
225 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
226 {
227 release_semaphore( sem, req->count, &reply->prev_count );
228 release_object( sem );
229 }
43c190e7 230}