server: Added infrastructure for access rights mapping.
[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 #include <stdarg.h>
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
37
38 struct mutex
39 {
40     struct object  obj;             /* object header */
41     struct thread *owner;           /* mutex owner */
42     unsigned int   count;           /* recursion count */
43     int            abandoned;       /* has it been abandoned? */
44     struct list    entry;           /* entry in owner thread mutex list */
45 };
46
47 static void mutex_dump( struct object *obj, int verbose );
48 static int mutex_signaled( struct object *obj, struct thread *thread );
49 static int mutex_satisfied( struct object *obj, struct thread *thread );
50 static void mutex_destroy( struct object *obj );
51 static int mutex_signal( struct object *obj, unsigned int access );
52
53 static const struct object_ops mutex_ops =
54 {
55     sizeof(struct mutex),      /* size */
56     mutex_dump,                /* dump */
57     add_queue,                 /* add_queue */
58     remove_queue,              /* remove_queue */
59     mutex_signaled,            /* signaled */
60     mutex_satisfied,           /* satisfied */
61     mutex_signal,              /* signal */
62     no_get_fd,                 /* get_fd */
63     no_map_access,             /* map_access */
64     no_lookup_name,            /* lookup_name */
65     no_close_handle,           /* close_handle */
66     mutex_destroy              /* destroy */
67 };
68
69
70 static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
71                                    unsigned int attr, int owned )
72 {
73     struct mutex *mutex;
74
75     if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
76     {
77         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
78         {
79             /* initialize it if it didn't already exist */
80             mutex->count = 0;
81             mutex->owner = NULL;
82             mutex->abandoned = 0;
83             if (owned) mutex_satisfied( &mutex->obj, current );
84         }
85     }
86     return mutex;
87 }
88
89 /* release a mutex once the recursion count is 0 */
90 static void do_release( struct mutex *mutex )
91 {
92     assert( !mutex->count );
93     /* remove the mutex from the thread list of owned mutexes */
94     list_remove( &mutex->entry );
95     mutex->owner = NULL;
96     wake_up( &mutex->obj, 0 );
97 }
98
99 void abandon_mutexes( struct thread *thread )
100 {
101     struct list *ptr;
102
103     while ((ptr = list_head( &thread->mutex_list )) != NULL)
104     {
105         struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
106         assert( mutex->owner == thread );
107         mutex->count = 0;
108         mutex->abandoned = 1;
109         do_release( mutex );
110     }
111 }
112
113 static void mutex_dump( struct object *obj, int verbose )
114 {
115     struct mutex *mutex = (struct mutex *)obj;
116     assert( obj->ops == &mutex_ops );
117     fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
118     dump_object_name( &mutex->obj );
119     fputc( '\n', stderr );
120 }
121
122 static int mutex_signaled( struct object *obj, struct thread *thread )
123 {
124     struct mutex *mutex = (struct mutex *)obj;
125     assert( obj->ops == &mutex_ops );
126     return (!mutex->count || (mutex->owner == thread));
127 }
128
129 static int mutex_satisfied( struct object *obj, struct thread *thread )
130 {
131     struct mutex *mutex = (struct mutex *)obj;
132     assert( obj->ops == &mutex_ops );
133     assert( !mutex->count || (mutex->owner == thread) );
134
135     if (!mutex->count++)  /* FIXME: avoid wrap-around */
136     {
137         assert( !mutex->owner );
138         mutex->owner = thread;
139         list_add_head( &thread->mutex_list, &mutex->entry );
140     }
141     if (!mutex->abandoned) return 0;
142     mutex->abandoned = 0;
143     return 1;
144 }
145
146 static int mutex_signal( struct object *obj, unsigned int access )
147 {
148     struct mutex *mutex = (struct mutex *)obj;
149     assert( obj->ops == &mutex_ops );
150
151     if (!(access & SYNCHRONIZE))  /* FIXME: MUTEX_MODIFY_STATE? */
152     {
153         set_error( STATUS_ACCESS_DENIED );
154         return 0;
155     }
156     if (!mutex->count || (mutex->owner != current))
157     {
158         set_error( STATUS_MUTANT_NOT_OWNED );
159         return 0;
160     }
161     if (!--mutex->count) do_release( mutex );
162     return 1;
163 }
164
165 static void mutex_destroy( struct object *obj )
166 {
167     struct mutex *mutex = (struct mutex *)obj;
168     assert( obj->ops == &mutex_ops );
169
170     if (!mutex->count) return;
171     mutex->count = 0;
172     do_release( mutex );
173 }
174
175 /* create a mutex */
176 DECL_HANDLER(create_mutex)
177 {
178     struct mutex *mutex;
179     struct unicode_str name;
180     struct directory *root = NULL;
181
182     reply->handle = 0;
183     get_req_unicode_str( &name );
184     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
185         return;
186
187     if ((mutex = create_mutex( root, &name, req->attributes, req->owned )))
188     {
189         reply->handle = alloc_handle( current->process, mutex, req->access, req->attributes );
190         release_object( mutex );
191     }
192
193     if (root) release_object( root );
194 }
195
196 /* open a handle to a mutex */
197 DECL_HANDLER(open_mutex)
198 {
199     struct unicode_str name;
200     struct directory *root = NULL;
201     struct mutex *mutex;
202
203     get_req_unicode_str( &name );
204     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
205         return;
206
207     if ((mutex = open_object_dir( root, &name, req->attributes, &mutex_ops )))
208     {
209         reply->handle = alloc_handle( current->process, &mutex->obj, req->access, req->attributes );
210         release_object( mutex );
211     }
212
213     if (root) release_object( root );
214 }
215
216 /* release a mutex */
217 DECL_HANDLER(release_mutex)
218 {
219     struct mutex *mutex;
220
221     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
222                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
223     {
224         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
225         else
226         {
227             reply->prev_count = mutex->count;
228             if (!--mutex->count) do_release( mutex );
229         }
230         release_object( mutex );
231     }
232 }