Changed fd operations to take a struct fd instead of a struct object.
[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     mutex_destroy              /* destroy */
59 };
60
61
62 static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned )
63 {
64     struct mutex *mutex;
65
66     if ((mutex = create_named_object( sync_namespace, &mutex_ops, name, len )))
67     {
68         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
69         {
70             /* initialize it if it didn't already exist */
71             mutex->count = 0;
72             mutex->owner = NULL;
73             mutex->abandoned = 0;
74             mutex->next = mutex->prev = NULL;
75             if (owned) mutex_satisfied( &mutex->obj, current );
76         }
77     }
78     return mutex;
79 }
80
81 /* release a mutex once the recursion count is 0 */
82 static void do_release( struct mutex *mutex )
83 {
84     assert( !mutex->count );
85     /* remove the mutex from the thread list of owned mutexes */
86     if (mutex->next) mutex->next->prev = mutex->prev;
87     if (mutex->prev) mutex->prev->next = mutex->next;
88     else mutex->owner->mutex = mutex->next;
89     mutex->owner = NULL;
90     mutex->next = mutex->prev = NULL;
91     wake_up( &mutex->obj, 0 );
92 }
93
94 void abandon_mutexes( struct thread *thread )
95 {
96     while (thread->mutex)
97     {
98         struct mutex *mutex = thread->mutex;
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         mutex->prev  = NULL;
133         if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
134         thread->mutex = mutex;
135     }
136     if (!mutex->abandoned) return 0;
137     mutex->abandoned = 0;
138     return 1;
139 }
140
141 static void mutex_destroy( struct object *obj )
142 {
143     struct mutex *mutex = (struct mutex *)obj;
144     assert( obj->ops == &mutex_ops );
145
146     if (!mutex->count) return;
147     mutex->count = 0;
148     do_release( mutex );
149 }
150
151 /* create a mutex */
152 DECL_HANDLER(create_mutex)
153 {
154     struct mutex *mutex;
155
156     reply->handle = 0;
157     if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
158     {
159         reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
160         release_object( mutex );
161     }
162 }
163
164 /* open a handle to a mutex */
165 DECL_HANDLER(open_mutex)
166 {
167     reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
168                                  &mutex_ops, req->access, req->inherit );
169 }
170
171 /* release a mutex */
172 DECL_HANDLER(release_mutex)
173 {
174     struct mutex *mutex;
175
176     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
177                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
178     {
179         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
180         else if (!--mutex->count) do_release( mutex );
181         release_object( mutex );
182     }
183 }