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
33 struct object obj; /* object header */
34 struct thread *owner; /* mutex owner */
35 unsigned int count; /* recursion count */
36 int abandoned; /* has it been abandoned? */
41 static void mutex_dump( struct object *obj, int verbose );
42 static int mutex_signaled( struct object *obj, struct thread *thread );
43 static int mutex_satisfied( struct object *obj, struct thread *thread );
44 static void mutex_destroy( struct object *obj );
46 static const struct object_ops mutex_ops =
48 sizeof(struct mutex), /* size */
49 mutex_dump, /* dump */
50 add_queue, /* add_queue */
51 remove_queue, /* remove_queue */
52 mutex_signaled, /* signaled */
53 mutex_satisfied, /* satisfied */
54 NULL, /* get_poll_events */
55 NULL, /* poll_event */
56 no_get_fd, /* get_fd */
58 no_get_file_info, /* get_file_info */
59 NULL, /* queue_async */
60 mutex_destroy /* destroy */
64 static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned )
68 if ((mutex = create_named_object( &mutex_ops, name, len )))
70 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
72 /* initialize it if it didn't already exist */
76 mutex->next = mutex->prev = NULL;
77 if (owned) mutex_satisfied( &mutex->obj, current );
83 /* release a mutex once the recursion count is 0 */
84 static void do_release( struct mutex *mutex )
86 assert( !mutex->count );
87 /* remove the mutex from the thread list of owned mutexes */
88 if (mutex->next) mutex->next->prev = mutex->prev;
89 if (mutex->prev) mutex->prev->next = mutex->next;
90 else mutex->owner->mutex = mutex->next;
92 mutex->next = mutex->prev = NULL;
93 wake_up( &mutex->obj, 0 );
96 void abandon_mutexes( struct thread *thread )
100 struct mutex *mutex = thread->mutex;
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;
135 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
136 thread->mutex = mutex;
138 if (!mutex->abandoned) return 0;
139 mutex->abandoned = 0;
143 static void mutex_destroy( struct object *obj )
145 struct mutex *mutex = (struct mutex *)obj;
146 assert( obj->ops == &mutex_ops );
148 if (!mutex->count) return;
154 DECL_HANDLER(create_mutex)
159 if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
161 reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
162 release_object( mutex );
166 /* open a handle to a mutex */
167 DECL_HANDLER(open_mutex)
169 reply->handle = open_object( get_req_data(), get_req_data_size(),
170 &mutex_ops, req->access, req->inherit );
173 /* release a mutex */
174 DECL_HANDLER(release_mutex)
178 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
179 MUTEX_MODIFY_STATE, &mutex_ops )))
181 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
182 else if (!--mutex->count) do_release( mutex );
183 release_object( mutex );