Move event and mutex objects into directory name space.
[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_lookup_name,            /* lookup_name */
64     no_close_handle,           /* close_handle */
65     mutex_destroy              /* destroy */
66 };
67
68
69 static struct mutex *create_mutex( struct directory *root, const struct unicode_str *name,
70                                    unsigned int attr, int owned )
71 {
72     struct mutex *mutex;
73
74     if ((mutex = create_named_object_dir( root, name, attr, &mutex_ops )))
75     {
76         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
77         {
78             /* initialize it if it didn't already exist */
79             mutex->count = 0;
80             mutex->owner = NULL;
81             mutex->abandoned = 0;
82             if (owned) mutex_satisfied( &mutex->obj, current );
83         }
84     }
85     return mutex;
86 }
87
88 /* release a mutex once the recursion count is 0 */
89 static void do_release( struct mutex *mutex )
90 {
91     assert( !mutex->count );
92     /* remove the mutex from the thread list of owned mutexes */
93     list_remove( &mutex->entry );
94     mutex->owner = NULL;
95     wake_up( &mutex->obj, 0 );
96 }
97
98 void abandon_mutexes( struct thread *thread )
99 {
100     struct list *ptr;
101
102     while ((ptr = list_head( &thread->mutex_list )) != NULL)
103     {
104         struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
105         assert( mutex->owner == thread );
106         mutex->count = 0;
107         mutex->abandoned = 1;
108         do_release( mutex );
109     }
110 }
111
112 static void mutex_dump( struct object *obj, int verbose )
113 {
114     struct mutex *mutex = (struct mutex *)obj;
115     assert( obj->ops == &mutex_ops );
116     fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
117     dump_object_name( &mutex->obj );
118     fputc( '\n', stderr );
119 }
120
121 static int mutex_signaled( struct object *obj, struct thread *thread )
122 {
123     struct mutex *mutex = (struct mutex *)obj;
124     assert( obj->ops == &mutex_ops );
125     return (!mutex->count || (mutex->owner == thread));
126 }
127
128 static int mutex_satisfied( struct object *obj, struct thread *thread )
129 {
130     struct mutex *mutex = (struct mutex *)obj;
131     assert( obj->ops == &mutex_ops );
132     assert( !mutex->count || (mutex->owner == thread) );
133
134     if (!mutex->count++)  /* FIXME: avoid wrap-around */
135     {
136         assert( !mutex->owner );
137         mutex->owner = thread;
138         list_add_head( &thread->mutex_list, &mutex->entry );
139     }
140     if (!mutex->abandoned) return 0;
141     mutex->abandoned = 0;
142     return 1;
143 }
144
145 static int mutex_signal( struct object *obj, unsigned int access )
146 {
147     struct mutex *mutex = (struct mutex *)obj;
148     assert( obj->ops == &mutex_ops );
149
150     if (!(access & SYNCHRONIZE))  /* FIXME: MUTEX_MODIFY_STATE? */
151     {
152         set_error( STATUS_ACCESS_DENIED );
153         return 0;
154     }
155     if (!mutex->count || (mutex->owner != current))
156     {
157         set_error( STATUS_MUTANT_NOT_OWNED );
158         return 0;
159     }
160     if (!--mutex->count) do_release( mutex );
161     return 1;
162 }
163
164 static void mutex_destroy( struct object *obj )
165 {
166     struct mutex *mutex = (struct mutex *)obj;
167     assert( obj->ops == &mutex_ops );
168
169     if (!mutex->count) return;
170     mutex->count = 0;
171     do_release( mutex );
172 }
173
174 /* create a mutex */
175 DECL_HANDLER(create_mutex)
176 {
177     struct mutex *mutex;
178     struct unicode_str name;
179     struct directory *root = NULL;
180
181     reply->handle = 0;
182     get_req_unicode_str( &name );
183     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
184         return;
185
186     if ((mutex = create_mutex( root, &name, req->attributes, req->owned )))
187     {
188         reply->handle = alloc_handle( current->process, mutex, req->access,
189                                       req->attributes & OBJ_INHERIT );
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
202     get_req_unicode_str( &name );
203     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
204         return;
205
206     reply->handle = open_object_dir( root, &name, req->attributes, &mutex_ops, req->access );
207
208     if (root) release_object( root );
209 }
210
211 /* release a mutex */
212 DECL_HANDLER(release_mutex)
213 {
214     struct mutex *mutex;
215
216     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
217                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
218     {
219         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
220         else
221         {
222             reply->prev_count = mutex->count;
223             if (!--mutex->count) do_release( mutex );
224         }
225         release_object( mutex );
226     }
227 }