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