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