kernel: Add some more tests for FindFirstChangeNotification.
[wine/gsoc_dplay.git] / server / mutex.c
blob410416133a7131ee096a597d420d37839eab3504
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
38 struct mutex
40 struct object obj; /* object header */
41 struct thread *owner; /* mutex owner */
42 unsigned int count; /* recursion count */
43 int abandoned; /* has it been abandoned? */
44 struct list entry; /* entry in owner thread mutex list */
47 static void mutex_dump( struct object *obj, int verbose );
48 static int mutex_signaled( struct object *obj, struct thread *thread );
49 static int mutex_satisfied( struct object *obj, struct thread *thread );
50 static unsigned int mutex_map_access( struct object *obj, unsigned int access );
51 static void mutex_destroy( struct object *obj );
52 static int mutex_signal( struct object *obj, unsigned int access );
54 static const struct object_ops mutex_ops =
56 sizeof(struct mutex), /* size */
57 mutex_dump, /* dump */
58 add_queue, /* add_queue */
59 remove_queue, /* remove_queue */
60 mutex_signaled, /* signaled */
61 mutex_satisfied, /* satisfied */
62 mutex_signal, /* signal */
63 no_get_fd, /* get_fd */
64 mutex_map_access, /* map_access */
65 no_lookup_name, /* lookup_name */
66 no_close_handle, /* close_handle */
67 mutex_destroy /* destroy */
71 static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
72 unsigned int attr, int owned )
74 struct mutex *mutex;
76 if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
78 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
80 /* initialize it if it didn't already exist */
81 mutex->count = 0;
82 mutex->owner = NULL;
83 mutex->abandoned = 0;
84 if (owned) mutex_satisfied( &mutex->obj, current );
87 return mutex;
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 void abandon_mutexes( struct thread *thread )
102 struct list *ptr;
104 while ((ptr = list_head( &thread->mutex_list )) != NULL)
106 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
107 assert( mutex->owner == thread );
108 mutex->count = 0;
109 mutex->abandoned = 1;
110 do_release( mutex );
114 static void mutex_dump( struct object *obj, int verbose )
116 struct mutex *mutex = (struct mutex *)obj;
117 assert( obj->ops == &mutex_ops );
118 fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
119 dump_object_name( &mutex->obj );
120 fputc( '\n', stderr );
123 static int mutex_signaled( struct object *obj, struct thread *thread )
125 struct mutex *mutex = (struct mutex *)obj;
126 assert( obj->ops == &mutex_ops );
127 return (!mutex->count || (mutex->owner == thread));
130 static int mutex_satisfied( struct object *obj, struct thread *thread )
132 struct mutex *mutex = (struct mutex *)obj;
133 assert( obj->ops == &mutex_ops );
134 assert( !mutex->count || (mutex->owner == thread) );
136 if (!mutex->count++) /* FIXME: avoid wrap-around */
138 assert( !mutex->owner );
139 mutex->owner = thread;
140 list_add_head( &thread->mutex_list, &mutex->entry );
142 if (!mutex->abandoned) return 0;
143 mutex->abandoned = 0;
144 return 1;
147 static unsigned int mutex_map_access( struct object *obj, unsigned int access )
149 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
150 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | MUTEX_MODIFY_STATE;
151 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
152 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
153 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
156 static int mutex_signal( struct object *obj, unsigned int access )
158 struct mutex *mutex = (struct mutex *)obj;
159 assert( obj->ops == &mutex_ops );
161 if (!(access & SYNCHRONIZE)) /* FIXME: MUTEX_MODIFY_STATE? */
163 set_error( STATUS_ACCESS_DENIED );
164 return 0;
166 if (!mutex->count || (mutex->owner != current))
168 set_error( STATUS_MUTANT_NOT_OWNED );
169 return 0;
171 if (!--mutex->count) do_release( mutex );
172 return 1;
175 static void mutex_destroy( struct object *obj )
177 struct mutex *mutex = (struct mutex *)obj;
178 assert( obj->ops == &mutex_ops );
180 if (!mutex->count) return;
181 mutex->count = 0;
182 do_release( mutex );
185 /* create a mutex */
186 DECL_HANDLER(create_mutex)
188 struct mutex *mutex;
189 struct unicode_str name;
190 struct directory *root = NULL;
192 reply->handle = 0;
193 get_req_unicode_str( &name );
194 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
195 return;
197 if ((mutex = create_mutex( root, &name, req->attributes, req->owned )))
199 reply->handle = alloc_handle( current->process, mutex, req->access, req->attributes );
200 release_object( mutex );
203 if (root) release_object( root );
206 /* open a handle to a mutex */
207 DECL_HANDLER(open_mutex)
209 struct unicode_str name;
210 struct directory *root = NULL;
211 struct mutex *mutex;
213 get_req_unicode_str( &name );
214 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
215 return;
217 if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops )))
219 reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes );
220 release_object( mutex );
223 if (root) release_object( root );
226 /* release a mutex */
227 DECL_HANDLER(release_mutex)
229 struct mutex *mutex;
231 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
232 MUTEX_MODIFY_STATE, &mutex_ops )))
234 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
235 else
237 reply->prev_count = mutex->count;
238 if (!--mutex->count) do_release( mutex );
240 release_object( mutex );