Convert the per-thread mutex list to a standard list.
[wine] / server / mutex.c
1 /*
2  * Server-side mutex management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "windef.h"
29
30 #include "handle.h"
31 #include "thread.h"
32 #include "request.h"
33
34 struct mutex
35 {
36     struct object  obj;             /* object header */
37     struct thread *owner;           /* mutex owner */
38     unsigned int   count;           /* recursion count */
39     int            abandoned;       /* has it been abandoned? */
40     struct list    entry;           /* entry in owner thread mutex list */
41 };
42
43 static void mutex_dump( struct object *obj, int verbose );
44 static int mutex_signaled( struct object *obj, struct thread *thread );
45 static int mutex_satisfied( struct object *obj, struct thread *thread );
46 static void mutex_destroy( struct object *obj );
47
48 static const struct object_ops mutex_ops =
49 {
50     sizeof(struct mutex),      /* size */
51     mutex_dump,                /* dump */
52     add_queue,                 /* add_queue */
53     remove_queue,              /* remove_queue */
54     mutex_signaled,            /* signaled */
55     mutex_satisfied,           /* satisfied */
56     no_get_fd,                 /* get_fd */
57     mutex_destroy              /* destroy */
58 };
59
60
61 static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned )
62 {
63     struct mutex *mutex;
64
65     if ((mutex = create_named_object( sync_namespace, &mutex_ops, name, len )))
66     {
67         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
68         {
69             /* initialize it if it didn't already exist */
70             mutex->count = 0;
71             mutex->owner = NULL;
72             mutex->abandoned = 0;
73             if (owned) mutex_satisfied( &mutex->obj, current );
74         }
75     }
76     return mutex;
77 }
78
79 /* release a mutex once the recursion count is 0 */
80 static void do_release( struct mutex *mutex )
81 {
82     assert( !mutex->count );
83     /* remove the mutex from the thread list of owned mutexes */
84     list_remove( &mutex->entry );
85     mutex->owner = NULL;
86     wake_up( &mutex->obj, 0 );
87 }
88
89 void abandon_mutexes( struct thread *thread )
90 {
91     struct list *ptr;
92
93     while ((ptr = list_head( &thread->mutex_list )) != NULL)
94     {
95         struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
96         assert( mutex->owner == thread );
97         mutex->count = 0;
98         mutex->abandoned = 1;
99         do_release( mutex );
100     }
101 }
102
103 static void mutex_dump( struct object *obj, int verbose )
104 {
105     struct mutex *mutex = (struct mutex *)obj;
106     assert( obj->ops == &mutex_ops );
107     fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
108     dump_object_name( &mutex->obj );
109     fputc( '\n', stderr );
110 }
111
112 static int mutex_signaled( struct object *obj, struct thread *thread )
113 {
114     struct mutex *mutex = (struct mutex *)obj;
115     assert( obj->ops == &mutex_ops );
116     return (!mutex->count || (mutex->owner == thread));
117 }
118
119 static int mutex_satisfied( struct object *obj, struct thread *thread )
120 {
121     struct mutex *mutex = (struct mutex *)obj;
122     assert( obj->ops == &mutex_ops );
123     assert( !mutex->count || (mutex->owner == thread) );
124
125     if (!mutex->count++)  /* FIXME: avoid wrap-around */
126     {
127         assert( !mutex->owner );
128         mutex->owner = thread;
129         list_add_head( &thread->mutex_list, &mutex->entry );
130     }
131     if (!mutex->abandoned) return 0;
132     mutex->abandoned = 0;
133     return 1;
134 }
135
136 static void mutex_destroy( struct object *obj )
137 {
138     struct mutex *mutex = (struct mutex *)obj;
139     assert( obj->ops == &mutex_ops );
140
141     if (!mutex->count) return;
142     mutex->count = 0;
143     do_release( mutex );
144 }
145
146 /* create a mutex */
147 DECL_HANDLER(create_mutex)
148 {
149     struct mutex *mutex;
150
151     reply->handle = 0;
152     if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
153     {
154         reply->handle = alloc_handle( current->process, mutex, req->access, req->inherit );
155         release_object( mutex );
156     }
157 }
158
159 /* open a handle to a mutex */
160 DECL_HANDLER(open_mutex)
161 {
162     reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
163                                  &mutex_ops, req->access, req->inherit );
164 }
165
166 /* release a mutex */
167 DECL_HANDLER(release_mutex)
168 {
169     struct mutex *mutex;
170
171     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
172                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
173     {
174         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
175         else
176         {
177             reply->prev_count = mutex->count;
178             if (!--mutex->count) do_release( mutex );
179         }
180         release_object( mutex );
181     }
182 }