msdrm: Add stub dll.
[wine.git] / server / mutex.c
blobfe94674e65f7ff2750b3ad8f9aa0e452e9a2990a
1 /*
2 * Server-side mutex 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 mutex
41 struct object obj; /* object header */
42 struct thread *owner; /* mutex owner */
43 unsigned int count; /* recursion count */
44 int abandoned; /* has it been abandoned? */
45 struct list entry; /* entry in owner thread mutex list */
48 static void mutex_dump( struct object *obj, int verbose );
49 static struct object_type *mutex_get_type( struct object *obj );
50 static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry );
51 static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry );
52 static unsigned int mutex_map_access( struct object *obj, unsigned int access );
53 static void mutex_destroy( struct object *obj );
54 static int mutex_signal( struct object *obj, unsigned int access );
56 static const struct object_ops mutex_ops =
58 sizeof(struct mutex), /* size */
59 mutex_dump, /* dump */
60 mutex_get_type, /* get_type */
61 add_queue, /* add_queue */
62 remove_queue, /* remove_queue */
63 mutex_signaled, /* signaled */
64 mutex_satisfied, /* satisfied */
65 mutex_signal, /* signal */
66 no_get_fd, /* get_fd */
67 mutex_map_access, /* map_access */
68 default_get_sd, /* get_sd */
69 default_set_sd, /* set_sd */
70 no_lookup_name, /* lookup_name */
71 no_open_file, /* open_file */
72 no_close_handle, /* close_handle */
73 mutex_destroy /* destroy */
77 /* grab a mutex for a given thread */
78 static void do_grab( struct mutex *mutex, struct thread *thread )
80 assert( !mutex->count || (mutex->owner == thread) );
82 if (!mutex->count++) /* FIXME: avoid wrap-around */
84 assert( !mutex->owner );
85 mutex->owner = thread;
86 list_add_head( &thread->mutex_list, &mutex->entry );
90 /* release a mutex once the recursion count is 0 */
91 static void do_release( struct mutex *mutex )
93 assert( !mutex->count );
94 /* remove the mutex from the thread list of owned mutexes */
95 list_remove( &mutex->entry );
96 mutex->owner = NULL;
97 wake_up( &mutex->obj, 0 );
100 static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
101 unsigned int attr, int owned, const struct security_descriptor *sd )
103 struct mutex *mutex;
105 if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
107 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
109 /* initialize it if it didn't already exist */
110 mutex->count = 0;
111 mutex->owner = NULL;
112 mutex->abandoned = 0;
113 if (owned) do_grab( mutex, current );
114 if (sd) default_set_sd( &mutex->obj, sd, OWNER_SECURITY_INFORMATION|
115 GROUP_SECURITY_INFORMATION|
116 DACL_SECURITY_INFORMATION|
117 SACL_SECURITY_INFORMATION );
120 return mutex;
123 void abandon_mutexes( struct thread *thread )
125 struct list *ptr;
127 while ((ptr = list_head( &thread->mutex_list )) != NULL)
129 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
130 assert( mutex->owner == thread );
131 mutex->count = 0;
132 mutex->abandoned = 1;
133 do_release( mutex );
137 static void mutex_dump( struct object *obj, int verbose )
139 struct mutex *mutex = (struct mutex *)obj;
140 assert( obj->ops == &mutex_ops );
141 fprintf( stderr, "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
144 static struct object_type *mutex_get_type( struct object *obj )
146 static const WCHAR name[] = {'M','u','t','a','n','t'};
147 static const struct unicode_str str = { name, sizeof(name) };
148 return get_object_type( &str );
151 static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry )
153 struct mutex *mutex = (struct mutex *)obj;
154 assert( obj->ops == &mutex_ops );
155 return (!mutex->count || (mutex->owner == get_wait_queue_thread( entry )));
158 static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry )
160 struct mutex *mutex = (struct mutex *)obj;
161 assert( obj->ops == &mutex_ops );
163 do_grab( mutex, get_wait_queue_thread( entry ));
164 if (mutex->abandoned) make_wait_abandoned( entry );
165 mutex->abandoned = 0;
168 static unsigned int mutex_map_access( struct object *obj, unsigned int access )
170 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | MUTANT_QUERY_STATE;
171 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
172 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
173 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
174 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
177 static int mutex_signal( struct object *obj, unsigned int access )
179 struct mutex *mutex = (struct mutex *)obj;
180 assert( obj->ops == &mutex_ops );
182 if (!(access & SYNCHRONIZE))
184 set_error( STATUS_ACCESS_DENIED );
185 return 0;
187 if (!mutex->count || (mutex->owner != current))
189 set_error( STATUS_MUTANT_NOT_OWNED );
190 return 0;
192 if (!--mutex->count) do_release( mutex );
193 return 1;
196 static void mutex_destroy( struct object *obj )
198 struct mutex *mutex = (struct mutex *)obj;
199 assert( obj->ops == &mutex_ops );
201 if (!mutex->count) return;
202 mutex->count = 0;
203 do_release( mutex );
206 /* create a mutex */
207 DECL_HANDLER(create_mutex)
209 struct mutex *mutex;
210 struct unicode_str name;
211 struct directory *root = NULL;
212 const struct security_descriptor *sd;
213 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name );
215 if (!objattr) return;
217 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
218 return;
220 if ((mutex = create_mutex( root, &name, objattr->attributes, req->owned, sd )))
222 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
223 reply->handle = alloc_handle( current->process, mutex, req->access, objattr->attributes );
224 else
225 reply->handle = alloc_handle_no_access_check( current->process, mutex,
226 req->access, objattr->attributes );
227 release_object( mutex );
230 if (root) release_object( root );
233 /* open a handle to a mutex */
234 DECL_HANDLER(open_mutex)
236 struct unicode_str name;
237 struct directory *root = NULL;
238 struct mutex *mutex;
240 get_req_unicode_str( &name );
241 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
242 return;
244 if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops )))
246 reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes );
247 release_object( mutex );
250 if (root) release_object( root );
253 /* release a mutex */
254 DECL_HANDLER(release_mutex)
256 struct mutex *mutex;
258 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
259 0, &mutex_ops )))
261 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
262 else
264 reply->prev_count = mutex->count;
265 if (!--mutex->count) do_release( mutex );
267 release_object( mutex );