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