Started moving functions that deal with Unix file descriptors to a
[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 mutex  *next;
41     struct mutex  *prev;
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
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     no_get_fd,                 /* get_fd */
58     no_get_file_info,          /* get_file_info */
59     mutex_destroy              /* destroy */
60 };
61
62
63 static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned )
64 {
65     struct mutex *mutex;
66
67     if ((mutex = create_named_object( sync_namespace, &mutex_ops, name, len )))
68     {
69         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
70         {
71             /* initialize it if it didn't already exist */
72             mutex->count = 0;
73             mutex->owner = NULL;
74             mutex->abandoned = 0;
75             mutex->next = mutex->prev = NULL;
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     if (mutex->next) mutex->next->prev = mutex->prev;
88     if (mutex->prev) mutex->prev->next = mutex->next;
89     else mutex->owner->mutex = mutex->next;
90     mutex->owner = NULL;
91     mutex->next = mutex->prev = NULL;
92     wake_up( &mutex->obj, 0 );
93 }
94
95 void abandon_mutexes( struct thread *thread )
96 {
97     while (thread->mutex)
98     {
99         struct mutex *mutex = thread->mutex;
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         mutex->prev  = NULL;
134         if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
135         thread->mutex = mutex;
136     }
137     if (!mutex->abandoned) return 0;
138     mutex->abandoned = 0;
139     return 1;
140 }
141
142 static void mutex_destroy( struct object *obj )
143 {
144     struct mutex *mutex = (struct mutex *)obj;
145     assert( obj->ops == &mutex_ops );
146
147     if (!mutex->count) return;
148     mutex->count = 0;
149     do_release( mutex );
150 }
151
152 /* create a mutex */
153 DECL_HANDLER(create_mutex)
154 {
155     struct mutex *mutex;
156
157     reply->handle = 0;
158     if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
159     {
160         reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
161         release_object( mutex );
162     }
163 }
164
165 /* open a handle to a mutex */
166 DECL_HANDLER(open_mutex)
167 {
168     reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
169                                  &mutex_ops, req->access, req->inherit );
170 }
171
172 /* release a mutex */
173 DECL_HANDLER(release_mutex)
174 {
175     struct mutex *mutex;
176
177     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
178                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
179     {
180         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
181         else if (!--mutex->count) do_release( mutex );
182         release_object( mutex );
183     }
184 }