Added LGPL standard comment, and copyright notices where necessary.
[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 <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "winnt.h"
26
27 #include "handle.h"
28 #include "thread.h"
29 #include "request.h"
30
31 struct mutex
32 {
33     struct object  obj;             /* object header */
34     struct thread *owner;           /* mutex owner */
35     unsigned int   count;           /* recursion count */
36     int            abandoned;       /* has it been abandoned? */
37     struct mutex  *next;
38     struct mutex  *prev;
39 };
40
41 static void mutex_dump( struct object *obj, int verbose );
42 static int mutex_signaled( struct object *obj, struct thread *thread );
43 static int mutex_satisfied( struct object *obj, struct thread *thread );
44 static void mutex_destroy( struct object *obj );
45
46 static const struct object_ops mutex_ops =
47 {
48     sizeof(struct mutex),      /* size */
49     mutex_dump,                /* dump */
50     add_queue,                 /* add_queue */
51     remove_queue,              /* remove_queue */
52     mutex_signaled,            /* signaled */
53     mutex_satisfied,           /* satisfied */
54     NULL,                      /* get_poll_events */
55     NULL,                      /* poll_event */
56     no_get_fd,                 /* get_fd */
57     no_flush,                  /* flush */
58     no_get_file_info,          /* get_file_info */
59     NULL,                      /* queue_async */
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( &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             mutex->next = mutex->prev = NULL;
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     if (mutex->next) mutex->next->prev = mutex->prev;
89     if (mutex->prev) mutex->prev->next = mutex->next;
90     else mutex->owner->mutex = mutex->next;
91     mutex->owner = NULL;
92     mutex->next = mutex->prev = NULL;
93     wake_up( &mutex->obj, 0 );
94 }
95
96 void abandon_mutexes( struct thread *thread )
97 {
98     while (thread->mutex)
99     {
100         struct mutex *mutex = thread->mutex;
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         mutex->prev  = NULL;
135         if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
136         thread->mutex = mutex;
137     }
138     if (!mutex->abandoned) return 0;
139     mutex->abandoned = 0;
140     return 1;
141 }
142
143 static void mutex_destroy( struct object *obj )
144 {
145     struct mutex *mutex = (struct mutex *)obj;
146     assert( obj->ops == &mutex_ops );
147
148     if (!mutex->count) return;
149     mutex->count = 0;
150     do_release( mutex );
151 }
152
153 /* create a mutex */
154 DECL_HANDLER(create_mutex)
155 {
156     struct mutex *mutex;
157
158     reply->handle = 0;
159     if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
160     {
161         reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
162         release_object( mutex );
163     }
164 }
165
166 /* open a handle to a mutex */
167 DECL_HANDLER(open_mutex)
168 {
169     reply->handle = open_object( get_req_data(), get_req_data_size(),
170                                  &mutex_ops, req->access, req->inherit );
171 }
172
173 /* release a mutex */
174 DECL_HANDLER(release_mutex)
175 {
176     struct mutex *mutex;
177
178     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
179                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
180     {
181         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
182         else if (!--mutex->count) do_release( mutex );
183         release_object( mutex );
184     }
185 }