Added support for the QS_ALLPOSTMESSAGE flag.
[wine/multimedia.git] / server / mutex.c
blob3388cc5e43c8c04a6fdd3f814808b2d49e5d3233
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>
28 #include "windef.h"
29 #include "winternl.h"
31 #include "handle.h"
32 #include "thread.h"
33 #include "request.h"
35 struct mutex
37 struct object obj; /* object header */
38 struct thread *owner; /* mutex owner */
39 unsigned int count; /* recursion count */
40 int abandoned; /* has it been abandoned? */
41 struct list entry; /* entry in owner thread mutex list */
44 static void mutex_dump( struct object *obj, int verbose );
45 static int mutex_signaled( struct object *obj, struct thread *thread );
46 static int mutex_satisfied( struct object *obj, struct thread *thread );
47 static void mutex_destroy( struct object *obj );
48 static int mutex_signal( struct object *obj, unsigned int access );
50 static const struct object_ops mutex_ops =
52 sizeof(struct mutex), /* size */
53 mutex_dump, /* dump */
54 add_queue, /* add_queue */
55 remove_queue, /* remove_queue */
56 mutex_signaled, /* signaled */
57 mutex_satisfied, /* satisfied */
58 mutex_signal, /* signal */
59 no_get_fd, /* get_fd */
60 no_close_handle, /* close_handle */
61 mutex_destroy /* destroy */
65 static struct mutex *create_mutex( const WCHAR *name, size_t len, unsigned int attr,
66 int owned )
68 struct mutex *mutex;
70 if ((mutex = create_named_object( sync_namespace, &mutex_ops, name, len, attr )))
72 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
74 /* initialize it if it didn't already exist */
75 mutex->count = 0;
76 mutex->owner = NULL;
77 mutex->abandoned = 0;
78 if (owned) mutex_satisfied( &mutex->obj, current );
81 return mutex;
84 /* release a mutex once the recursion count is 0 */
85 static void do_release( struct mutex *mutex )
87 assert( !mutex->count );
88 /* remove the mutex from the thread list of owned mutexes */
89 list_remove( &mutex->entry );
90 mutex->owner = NULL;
91 wake_up( &mutex->obj, 0 );
94 void abandon_mutexes( struct thread *thread )
96 struct list *ptr;
98 while ((ptr = list_head( &thread->mutex_list )) != NULL)
100 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
101 assert( mutex->owner == thread );
102 mutex->count = 0;
103 mutex->abandoned = 1;
104 do_release( mutex );
108 static void mutex_dump( struct object *obj, int verbose )
110 struct mutex *mutex = (struct mutex *)obj;
111 assert( obj->ops == &mutex_ops );
112 fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
113 dump_object_name( &mutex->obj );
114 fputc( '\n', stderr );
117 static int mutex_signaled( struct object *obj, struct thread *thread )
119 struct mutex *mutex = (struct mutex *)obj;
120 assert( obj->ops == &mutex_ops );
121 return (!mutex->count || (mutex->owner == thread));
124 static int mutex_satisfied( struct object *obj, struct thread *thread )
126 struct mutex *mutex = (struct mutex *)obj;
127 assert( obj->ops == &mutex_ops );
128 assert( !mutex->count || (mutex->owner == thread) );
130 if (!mutex->count++) /* FIXME: avoid wrap-around */
132 assert( !mutex->owner );
133 mutex->owner = thread;
134 list_add_head( &thread->mutex_list, &mutex->entry );
136 if (!mutex->abandoned) return 0;
137 mutex->abandoned = 0;
138 return 1;
141 static int mutex_signal( struct object *obj, unsigned int access )
143 struct mutex *mutex = (struct mutex *)obj;
144 assert( obj->ops == &mutex_ops );
146 if (!(access & SYNCHRONIZE)) /* FIXME: MUTEX_MODIFY_STATE? */
148 set_error( STATUS_ACCESS_DENIED );
149 return 0;
151 if (!mutex->count || (mutex->owner != current))
153 set_error( STATUS_MUTANT_NOT_OWNED );
154 return 0;
156 if (!--mutex->count) do_release( mutex );
157 return 1;
160 static void mutex_destroy( struct object *obj )
162 struct mutex *mutex = (struct mutex *)obj;
163 assert( obj->ops == &mutex_ops );
165 if (!mutex->count) return;
166 mutex->count = 0;
167 do_release( mutex );
170 /* create a mutex */
171 DECL_HANDLER(create_mutex)
173 struct mutex *mutex;
175 reply->handle = 0;
176 if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->attributes,
177 req->owned )))
179 reply->handle = alloc_handle( current->process, mutex, req->access,
180 req->attributes & OBJ_INHERIT );
181 release_object( mutex );
185 /* open a handle to a mutex */
186 DECL_HANDLER(open_mutex)
188 reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
189 &mutex_ops, req->access, req->attributes );
192 /* release a mutex */
193 DECL_HANDLER(release_mutex)
195 struct mutex *mutex;
197 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
198 MUTEX_MODIFY_STATE, &mutex_ops )))
200 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
201 else
203 reply->prev_count = mutex->count;
204 if (!--mutex->count) do_release( mutex );
206 release_object( mutex );