ntdll: Load .so builtin modules without using libwine.
[wine/zf.git] / server / mutex.c
blob8a8f7248eae601c4219d65c275ba0b3e7599a497
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 directory_link_name, /* link_name */
72 default_unlink_name, /* unlink_name */
73 no_open_file, /* open_file */
74 no_kernel_obj_list, /* get_kernel_obj_list */
75 no_close_handle, /* close_handle */
76 mutex_destroy /* destroy */
80 /* grab a mutex for a given thread */
81 static void do_grab( struct mutex *mutex, struct thread *thread )
83 assert( !mutex->count || (mutex->owner == thread) );
85 if (!mutex->count++) /* FIXME: avoid wrap-around */
87 assert( !mutex->owner );
88 mutex->owner = thread;
89 list_add_head( &thread->mutex_list, &mutex->entry );
93 /* release a mutex once the recursion count is 0 */
94 static void do_release( struct mutex *mutex )
96 assert( !mutex->count );
97 /* remove the mutex from the thread list of owned mutexes */
98 list_remove( &mutex->entry );
99 mutex->owner = NULL;
100 wake_up( &mutex->obj, 0 );
103 static struct mutex *create_mutex( struct object *root, const struct unicode_str *name,
104 unsigned int attr, int owned, const struct security_descriptor *sd )
106 struct mutex *mutex;
108 if ((mutex = create_named_object( root, &mutex_ops, name, attr, sd )))
110 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
112 /* initialize it if it didn't already exist */
113 mutex->count = 0;
114 mutex->owner = NULL;
115 mutex->abandoned = 0;
116 if (owned) do_grab( mutex, current );
119 return mutex;
122 void abandon_mutexes( struct thread *thread )
124 struct list *ptr;
126 while ((ptr = list_head( &thread->mutex_list )) != NULL)
128 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
129 assert( mutex->owner == thread );
130 mutex->count = 0;
131 mutex->abandoned = 1;
132 do_release( mutex );
136 static void mutex_dump( struct object *obj, int verbose )
138 struct mutex *mutex = (struct mutex *)obj;
139 assert( obj->ops == &mutex_ops );
140 fprintf( stderr, "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
143 static struct object_type *mutex_get_type( struct object *obj )
145 static const WCHAR name[] = {'M','u','t','a','n','t'};
146 static const struct unicode_str str = { name, sizeof(name) };
147 return get_object_type( &str );
150 static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry )
152 struct mutex *mutex = (struct mutex *)obj;
153 assert( obj->ops == &mutex_ops );
154 return (!mutex->count || (mutex->owner == get_wait_queue_thread( entry )));
157 static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry )
159 struct mutex *mutex = (struct mutex *)obj;
160 assert( obj->ops == &mutex_ops );
162 do_grab( mutex, get_wait_queue_thread( entry ));
163 if (mutex->abandoned) make_wait_abandoned( entry );
164 mutex->abandoned = 0;
167 static unsigned int mutex_map_access( struct object *obj, unsigned int access )
169 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | MUTANT_QUERY_STATE;
170 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
171 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
172 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
173 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
176 static int mutex_signal( struct object *obj, unsigned int access )
178 struct mutex *mutex = (struct mutex *)obj;
179 assert( obj->ops == &mutex_ops );
181 if (!(access & SYNCHRONIZE))
183 set_error( STATUS_ACCESS_DENIED );
184 return 0;
186 if (!mutex->count || (mutex->owner != current))
188 set_error( STATUS_MUTANT_NOT_OWNED );
189 return 0;
191 if (!--mutex->count) do_release( mutex );
192 return 1;
195 static void mutex_destroy( struct object *obj )
197 struct mutex *mutex = (struct mutex *)obj;
198 assert( obj->ops == &mutex_ops );
200 if (!mutex->count) return;
201 mutex->count = 0;
202 do_release( mutex );
205 /* create a mutex */
206 DECL_HANDLER(create_mutex)
208 struct mutex *mutex;
209 struct unicode_str name;
210 struct object *root;
211 const struct security_descriptor *sd;
212 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
214 if (!objattr) return;
216 if ((mutex = create_mutex( root, &name, objattr->attributes, req->owned, sd )))
218 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
219 reply->handle = alloc_handle( current->process, mutex, req->access, objattr->attributes );
220 else
221 reply->handle = alloc_handle_no_access_check( current->process, mutex,
222 req->access, objattr->attributes );
223 release_object( mutex );
226 if (root) release_object( root );
229 /* open a handle to a mutex */
230 DECL_HANDLER(open_mutex)
232 struct unicode_str name = get_req_unicode_str();
234 reply->handle = open_object( current->process, req->rootdir, req->access,
235 &mutex_ops, &name, req->attributes );
238 /* release a mutex */
239 DECL_HANDLER(release_mutex)
241 struct mutex *mutex;
243 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
244 0, &mutex_ops )))
246 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
247 else
249 reply->prev_count = mutex->count;
250 if (!--mutex->count) do_release( mutex );
252 release_object( mutex );
256 /* return details about the mutex */
257 DECL_HANDLER(query_mutex)
259 struct mutex *mutex;
261 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
262 MUTANT_QUERY_STATE, &mutex_ops )))
264 reply->count = mutex->count;
265 reply->owned = (mutex->owner == current);
266 reply->abandoned = mutex->abandoned;
268 release_object( mutex );