user32: Add more tests for GetUpdateRect.
[wine/multimedia.git] / server / mutex.c
blob7d330e2fe530ff7900ce71c6e4117eccff6bce00
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"
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_open_file, /* open_file */
67 no_close_handle, /* close_handle */
68 mutex_destroy /* destroy */
72 static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
73 unsigned int attr, int owned )
75 struct mutex *mutex;
77 if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
79 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
81 /* initialize it if it didn't already exist */
82 mutex->count = 0;
83 mutex->owner = NULL;
84 mutex->abandoned = 0;
85 if (owned) mutex_satisfied( &mutex->obj, current );
88 return mutex;
91 /* release a mutex once the recursion count is 0 */
92 static void do_release( struct mutex *mutex )
94 assert( !mutex->count );
95 /* remove the mutex from the thread list of owned mutexes */
96 list_remove( &mutex->entry );
97 mutex->owner = NULL;
98 wake_up( &mutex->obj, 0 );
101 void abandon_mutexes( struct thread *thread )
103 struct list *ptr;
105 while ((ptr = list_head( &thread->mutex_list )) != NULL)
107 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
108 assert( mutex->owner == thread );
109 mutex->count = 0;
110 mutex->abandoned = 1;
111 do_release( mutex );
115 static void mutex_dump( struct object *obj, int verbose )
117 struct mutex *mutex = (struct mutex *)obj;
118 assert( obj->ops == &mutex_ops );
119 fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
120 dump_object_name( &mutex->obj );
121 fputc( '\n', stderr );
124 static int mutex_signaled( struct object *obj, struct thread *thread )
126 struct mutex *mutex = (struct mutex *)obj;
127 assert( obj->ops == &mutex_ops );
128 return (!mutex->count || (mutex->owner == thread));
131 static int mutex_satisfied( struct object *obj, struct thread *thread )
133 struct mutex *mutex = (struct mutex *)obj;
134 assert( obj->ops == &mutex_ops );
135 assert( !mutex->count || (mutex->owner == thread) );
137 if (!mutex->count++) /* FIXME: avoid wrap-around */
139 assert( !mutex->owner );
140 mutex->owner = thread;
141 list_add_head( &thread->mutex_list, &mutex->entry );
143 if (!mutex->abandoned) return 0;
144 mutex->abandoned = 0;
145 return 1;
148 static unsigned int mutex_map_access( struct object *obj, unsigned int access )
150 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
151 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | MUTEX_MODIFY_STATE;
152 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
153 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
154 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
157 static int mutex_signal( struct object *obj, unsigned int access )
159 struct mutex *mutex = (struct mutex *)obj;
160 assert( obj->ops == &mutex_ops );
162 if (!(access & SYNCHRONIZE)) /* FIXME: MUTEX_MODIFY_STATE? */
164 set_error( STATUS_ACCESS_DENIED );
165 return 0;
167 if (!mutex->count || (mutex->owner != current))
169 set_error( STATUS_MUTANT_NOT_OWNED );
170 return 0;
172 if (!--mutex->count) do_release( mutex );
173 return 1;
176 static void mutex_destroy( struct object *obj )
178 struct mutex *mutex = (struct mutex *)obj;
179 assert( obj->ops == &mutex_ops );
181 if (!mutex->count) return;
182 mutex->count = 0;
183 do_release( mutex );
186 /* create a mutex */
187 DECL_HANDLER(create_mutex)
189 struct mutex *mutex;
190 struct unicode_str name;
191 struct directory *root = NULL;
193 reply->handle = 0;
194 get_req_unicode_str( &name );
195 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
196 return;
198 if ((mutex = create_mutex( root, &name, req->attributes, req->owned )))
200 reply->handle = alloc_handle( current->process, mutex, req->access, req->attributes );
201 release_object( mutex );
204 if (root) release_object( root );
207 /* open a handle to a mutex */
208 DECL_HANDLER(open_mutex)
210 struct unicode_str name;
211 struct directory *root = NULL;
212 struct mutex *mutex;
214 get_req_unicode_str( &name );
215 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
216 return;
218 if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops )))
220 reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes );
221 release_object( mutex );
224 if (root) release_object( root );
227 /* release a mutex */
228 DECL_HANDLER(release_mutex)
230 struct mutex *mutex;
232 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
233 MUTEX_MODIFY_STATE, &mutex_ops )))
235 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
236 else
238 reply->prev_count = mutex->count;
239 if (!--mutex->count) do_release( mutex );
241 release_object( mutex );