Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / server / mutex.c
blob28e838b54a9d7646e9180a019567a473d79b4ac1
1 /*
2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "winnt.h"
30 #include "handle.h"
31 #include "thread.h"
32 #include "request.h"
34 struct mutex
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;
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 );
49 static const struct object_ops mutex_ops =
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 NULL, /* get_poll_events */
58 NULL, /* poll_event */
59 no_get_fd, /* get_fd */
60 no_flush, /* flush */
61 no_get_file_info, /* get_file_info */
62 NULL, /* queue_async */
63 mutex_destroy /* destroy */
67 static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned )
69 struct mutex *mutex;
71 if ((mutex = create_named_object( &mutex_ops, name, len )))
73 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
75 /* initialize it if it didn't already exist */
76 mutex->count = 0;
77 mutex->owner = NULL;
78 mutex->abandoned = 0;
79 mutex->next = mutex->prev = NULL;
80 if (owned) mutex_satisfied( &mutex->obj, current );
83 return mutex;
86 /* release a mutex once the recursion count is 0 */
87 static void do_release( struct mutex *mutex )
89 assert( !mutex->count );
90 /* remove the mutex from the thread list of owned mutexes */
91 if (mutex->next) mutex->next->prev = mutex->prev;
92 if (mutex->prev) mutex->prev->next = mutex->next;
93 else mutex->owner->mutex = mutex->next;
94 mutex->owner = NULL;
95 mutex->next = mutex->prev = NULL;
96 wake_up( &mutex->obj, 0 );
99 void abandon_mutexes( struct thread *thread )
101 while (thread->mutex)
103 struct mutex *mutex = thread->mutex;
104 assert( mutex->owner == thread );
105 mutex->count = 0;
106 mutex->abandoned = 1;
107 do_release( mutex );
111 static void mutex_dump( struct object *obj, int verbose )
113 struct mutex *mutex = (struct mutex *)obj;
114 assert( obj->ops == &mutex_ops );
115 fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
116 dump_object_name( &mutex->obj );
117 fputc( '\n', stderr );
120 static int mutex_signaled( struct object *obj, struct thread *thread )
122 struct mutex *mutex = (struct mutex *)obj;
123 assert( obj->ops == &mutex_ops );
124 return (!mutex->count || (mutex->owner == thread));
127 static int mutex_satisfied( struct object *obj, struct thread *thread )
129 struct mutex *mutex = (struct mutex *)obj;
130 assert( obj->ops == &mutex_ops );
131 assert( !mutex->count || (mutex->owner == thread) );
133 if (!mutex->count++) /* FIXME: avoid wrap-around */
135 assert( !mutex->owner );
136 mutex->owner = thread;
137 mutex->prev = NULL;
138 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
139 thread->mutex = mutex;
141 if (!mutex->abandoned) return 0;
142 mutex->abandoned = 0;
143 return 1;
146 static void mutex_destroy( struct object *obj )
148 struct mutex *mutex = (struct mutex *)obj;
149 assert( obj->ops == &mutex_ops );
151 if (!mutex->count) return;
152 mutex->count = 0;
153 do_release( mutex );
156 /* create a mutex */
157 DECL_HANDLER(create_mutex)
159 struct mutex *mutex;
161 reply->handle = 0;
162 if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
164 reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
165 release_object( mutex );
169 /* open a handle to a mutex */
170 DECL_HANDLER(open_mutex)
172 reply->handle = open_object( get_req_data(), get_req_data_size(),
173 &mutex_ops, req->access, req->inherit );
176 /* release a mutex */
177 DECL_HANDLER(release_mutex)
179 struct mutex *mutex;
181 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
182 MUTEX_MODIFY_STATE, &mutex_ops )))
184 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
185 else if (!--mutex->count) do_release( mutex );
186 release_object( mutex );