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