Add name_lookup function in object_ops.
[wine.git] / server / mutex.c
blob52e6d8cbd8cb9802c3f1d6b5ba8e242f44b1d004
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>
27 #include <stdarg.h>
29 #include "windef.h"
30 #include "winternl.h"
32 #include "handle.h"
33 #include "thread.h"
34 #include "request.h"
36 struct mutex
38 struct object obj; /* object header */
39 struct thread *owner; /* mutex owner */
40 unsigned int count; /* recursion count */
41 int abandoned; /* has it been abandoned? */
42 struct list entry; /* entry in owner thread mutex list */
45 static void mutex_dump( struct object *obj, int verbose );
46 static int mutex_signaled( struct object *obj, struct thread *thread );
47 static int mutex_satisfied( struct object *obj, struct thread *thread );
48 static void mutex_destroy( struct object *obj );
49 static int mutex_signal( struct object *obj, unsigned int access );
51 static const struct object_ops mutex_ops =
53 sizeof(struct mutex), /* size */
54 mutex_dump, /* dump */
55 add_queue, /* add_queue */
56 remove_queue, /* remove_queue */
57 mutex_signaled, /* signaled */
58 mutex_satisfied, /* satisfied */
59 mutex_signal, /* signal */
60 no_get_fd, /* get_fd */
61 no_lookup_name, /* lookup_name */
62 no_close_handle, /* close_handle */
63 mutex_destroy /* destroy */
67 static struct mutex *create_mutex( const struct unicode_str *name, unsigned int attr, int owned )
69 struct mutex *mutex;
71 if ((mutex = create_named_object( sync_namespace, &mutex_ops, name, attr )))
73 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
75 /* initialize it if it didn't already exist */
76 mutex->count = 0;
77 mutex->owner = NULL;
78 mutex->abandoned = 0;
79 if (owned) mutex_satisfied( &mutex->obj, current );
82 return mutex;
85 /* release a mutex once the recursion count is 0 */
86 static void do_release( struct mutex *mutex )
88 assert( !mutex->count );
89 /* remove the mutex from the thread list of owned mutexes */
90 list_remove( &mutex->entry );
91 mutex->owner = NULL;
92 wake_up( &mutex->obj, 0 );
95 void abandon_mutexes( struct thread *thread )
97 struct list *ptr;
99 while ((ptr = list_head( &thread->mutex_list )) != NULL)
101 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
102 assert( mutex->owner == thread );
103 mutex->count = 0;
104 mutex->abandoned = 1;
105 do_release( mutex );
109 static void mutex_dump( struct object *obj, int verbose )
111 struct mutex *mutex = (struct mutex *)obj;
112 assert( obj->ops == &mutex_ops );
113 fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
114 dump_object_name( &mutex->obj );
115 fputc( '\n', stderr );
118 static int mutex_signaled( struct object *obj, struct thread *thread )
120 struct mutex *mutex = (struct mutex *)obj;
121 assert( obj->ops == &mutex_ops );
122 return (!mutex->count || (mutex->owner == thread));
125 static int mutex_satisfied( struct object *obj, struct thread *thread )
127 struct mutex *mutex = (struct mutex *)obj;
128 assert( obj->ops == &mutex_ops );
129 assert( !mutex->count || (mutex->owner == thread) );
131 if (!mutex->count++) /* FIXME: avoid wrap-around */
133 assert( !mutex->owner );
134 mutex->owner = thread;
135 list_add_head( &thread->mutex_list, &mutex->entry );
137 if (!mutex->abandoned) return 0;
138 mutex->abandoned = 0;
139 return 1;
142 static int mutex_signal( struct object *obj, unsigned int access )
144 struct mutex *mutex = (struct mutex *)obj;
145 assert( obj->ops == &mutex_ops );
147 if (!(access & SYNCHRONIZE)) /* FIXME: MUTEX_MODIFY_STATE? */
149 set_error( STATUS_ACCESS_DENIED );
150 return 0;
152 if (!mutex->count || (mutex->owner != current))
154 set_error( STATUS_MUTANT_NOT_OWNED );
155 return 0;
157 if (!--mutex->count) do_release( mutex );
158 return 1;
161 static void mutex_destroy( struct object *obj )
163 struct mutex *mutex = (struct mutex *)obj;
164 assert( obj->ops == &mutex_ops );
166 if (!mutex->count) return;
167 mutex->count = 0;
168 do_release( mutex );
171 /* create a mutex */
172 DECL_HANDLER(create_mutex)
174 struct mutex *mutex;
175 struct unicode_str name;
177 reply->handle = 0;
178 get_req_unicode_str( &name );
179 if ((mutex = create_mutex( &name, req->attributes, req->owned )))
181 reply->handle = alloc_handle( current->process, mutex, req->access,
182 req->attributes & OBJ_INHERIT );
183 release_object( mutex );
187 /* open a handle to a mutex */
188 DECL_HANDLER(open_mutex)
190 struct unicode_str name;
192 get_req_unicode_str( &name );
193 reply->handle = open_object( sync_namespace, &name, &mutex_ops, req->access, req->attributes );
196 /* release a mutex */
197 DECL_HANDLER(release_mutex)
199 struct mutex *mutex;
201 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
202 MUTEX_MODIFY_STATE, &mutex_ops )))
204 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
205 else
207 reply->prev_count = mutex->count;
208 if (!--mutex->count) do_release( mutex );
210 release_object( mutex );