blob: 5b9ea212866f2ac3af3e7a29ae2eac6c99c24424 [file] [log] [blame]
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001/*
2 * Server-side mutex management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000019 */
20
21#include <assert.h>
22#include <stdio.h>
23#include <stdlib.h>
24
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000025#include "winnt.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000026
27#include "handle.h"
28#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000029#include "request.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000030
31struct mutex
32{
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? */
37 struct mutex *next;
38 struct mutex *prev;
39};
40
Alexandre Julliard338e7571998-12-27 15:28:54 +000041static void mutex_dump( struct object *obj, int verbose );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000042static int mutex_signaled( struct object *obj, struct thread *thread );
43static int mutex_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliarddbab5e21999-08-04 09:52:33 +000044static void mutex_destroy( struct object *obj );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000045
46static const struct object_ops mutex_ops =
47{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000048 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 */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000056 no_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000057 no_flush, /* flush */
58 no_get_file_info, /* get_file_info */
Mike McCormack6f011c02001-12-20 00:07:05 +000059 NULL, /* queue_async */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000060 mutex_destroy /* destroy */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000061};
62
63
Alexandre Julliardd16319c1999-11-25 21:30:24 +000064static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000065{
66 struct mutex *mutex;
67
Alexandre Julliard5bc78081999-06-22 17:26:53 +000068 if ((mutex = create_named_object( &mutex_ops, name, len )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000069 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000070 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
Alexandre Julliard5bc78081999-06-22 17:26:53 +000071 {
72 /* initialize it if it didn't already exist */
73 mutex->count = 0;
74 mutex->owner = NULL;
75 mutex->abandoned = 0;
76 mutex->next = mutex->prev = NULL;
77 if (owned) mutex_satisfied( &mutex->obj, current );
78 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000079 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +000080 return mutex;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000081}
82
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000083/* release a mutex once the recursion count is 0 */
Alexandre Julliarddbab5e21999-08-04 09:52:33 +000084static void do_release( struct mutex *mutex )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000085{
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;
Alexandre Julliarddbab5e21999-08-04 09:52:33 +000090 else mutex->owner->mutex = mutex->next;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000091 mutex->owner = NULL;
92 mutex->next = mutex->prev = NULL;
93 wake_up( &mutex->obj, 0 );
94}
95
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000096void abandon_mutexes( struct thread *thread )
97{
98 while (thread->mutex)
99 {
100 struct mutex *mutex = thread->mutex;
101 assert( mutex->owner == thread );
102 mutex->count = 0;
103 mutex->abandoned = 1;
Alexandre Julliarddbab5e21999-08-04 09:52:33 +0000104 do_release( mutex );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000105 }
106}
107
Alexandre Julliard338e7571998-12-27 15:28:54 +0000108static void mutex_dump( struct object *obj, int verbose )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000109{
110 struct mutex *mutex = (struct mutex *)obj;
111 assert( obj->ops == &mutex_ops );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000112 fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
113 dump_object_name( &mutex->obj );
114 fputc( '\n', stderr );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000115}
116
117static int mutex_signaled( struct object *obj, struct thread *thread )
118{
119 struct mutex *mutex = (struct mutex *)obj;
120 assert( obj->ops == &mutex_ops );
121 return (!mutex->count || (mutex->owner == thread));
122}
123
124static int mutex_satisfied( struct object *obj, struct thread *thread )
125{
126 struct mutex *mutex = (struct mutex *)obj;
127 assert( obj->ops == &mutex_ops );
128 assert( !mutex->count || (mutex->owner == thread) );
129
130 if (!mutex->count++) /* FIXME: avoid wrap-around */
131 {
132 assert( !mutex->owner );
133 mutex->owner = thread;
134 mutex->prev = NULL;
135 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
136 thread->mutex = mutex;
137 }
138 if (!mutex->abandoned) return 0;
139 mutex->abandoned = 0;
140 return 1;
141}
142
Alexandre Julliarddbab5e21999-08-04 09:52:33 +0000143static void mutex_destroy( struct object *obj )
144{
145 struct mutex *mutex = (struct mutex *)obj;
146 assert( obj->ops == &mutex_ops );
147
148 if (!mutex->count) return;
149 mutex->count = 0;
150 do_release( mutex );
151}
152
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000153/* create a mutex */
154DECL_HANDLER(create_mutex)
155{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000156 struct mutex *mutex;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000157
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000158 reply->handle = 0;
159 if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000160 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000161 reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000162 release_object( mutex );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000163 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000164}
165
166/* open a handle to a mutex */
167DECL_HANDLER(open_mutex)
168{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000169 reply->handle = open_object( get_req_data(), get_req_data_size(),
170 &mutex_ops, req->access, req->inherit );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000171}
172
173/* release a mutex */
174DECL_HANDLER(release_mutex)
175{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000176 struct mutex *mutex;
177
178 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
179 MUTEX_MODIFY_STATE, &mutex_ops )))
180 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000181 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
Alexandre Julliarddbab5e21999-08-04 09:52:33 +0000182 else if (!--mutex->count) do_release( mutex );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000183 release_object( mutex );
184 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000185}