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
22 #include "wine/port.h"
38 struct object obj; /* object header */
39 struct thread *owner; /* mutex owner */
40 unsigned int count; /* recursion count */
41 int abandoned; /* has it been abandoned? */
42 struct list entry; /* entry in owner thread mutex list */
45 static void mutex_dump( struct object *obj, int verbose );
46 static int mutex_signaled( struct object *obj, struct thread *thread );
47 static int mutex_satisfied( struct object *obj, struct thread *thread );
48 static void mutex_destroy( struct object *obj );
49 static int mutex_signal( struct object *obj, unsigned int access );
51 static const struct object_ops mutex_ops =
53 sizeof(struct mutex), /* size */
54 mutex_dump, /* dump */
55 add_queue, /* add_queue */
56 remove_queue, /* remove_queue */
57 mutex_signaled, /* signaled */
58 mutex_satisfied, /* satisfied */
59 mutex_signal, /* signal */
60 no_get_fd, /* get_fd */
61 no_close_handle, /* close_handle */
62 mutex_destroy /* destroy */
66 static struct mutex *create_mutex( const struct unicode_str *name, unsigned int attr, int owned )
70 if ((mutex = create_named_object( sync_namespace, &mutex_ops, name, attr )))
72 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
74 /* initialize it if it didn't already exist */
78 if (owned) mutex_satisfied( &mutex->obj, current );
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 );
91 wake_up( &mutex->obj, 0 );
94 void abandon_mutexes( struct thread *thread )
98 while ((ptr = list_head( &thread->mutex_list )) != NULL)
100 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
101 assert( mutex->owner == thread );
103 mutex->abandoned = 1;
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;
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 );
151 if (!mutex->count || (mutex->owner != current))
153 set_error( STATUS_MUTANT_NOT_OWNED );
156 if (!--mutex->count) do_release( mutex );
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;
171 DECL_HANDLER(create_mutex)
174 struct unicode_str name;
177 get_req_unicode_str( &name );
178 if ((mutex = create_mutex( &name, req->attributes, req->owned )))
180 reply->handle = alloc_handle( current->process, mutex, req->access,
181 req->attributes & OBJ_INHERIT );
182 release_object( mutex );
186 /* open a handle to a mutex */
187 DECL_HANDLER(open_mutex)
189 struct unicode_str name;
191 get_req_unicode_str( &name );
192 reply->handle = open_object( sync_namespace, &name, &mutex_ops, req->access, req->attributes );
195 /* release a mutex */
196 DECL_HANDLER(release_mutex)
200 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
201 MUTEX_MODIFY_STATE, &mutex_ops )))
203 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
206 reply->prev_count = mutex->count;
207 if (!--mutex->count) do_release( mutex );
209 release_object( mutex );