2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
13 #include "server/process.h"
14 #include "server/thread.h"
18 struct object obj; /* object header */
19 struct thread *owner; /* mutex owner */
20 unsigned int count; /* recursion count */
21 int abandoned; /* has it been abandoned? */
26 static void mutex_dump( struct object *obj, int verbose );
27 static int mutex_signaled( struct object *obj, struct thread *thread );
28 static int mutex_satisfied( struct object *obj, struct thread *thread );
29 static void mutex_destroy( struct object *obj );
31 static const struct object_ops mutex_ops =
46 struct object *create_mutex( const char *name, int owned )
50 if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
52 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
54 /* initialize it if it didn't already exist */
58 mutex->next = mutex->prev = NULL;
59 if (owned) mutex_satisfied( &mutex->obj, current );
64 int open_mutex( unsigned int access, int inherit, const char *name )
66 return open_object( name, &mutex_ops, access, inherit );
69 /* release a mutex once the recursion count is 0 */
70 static void do_release( struct mutex *mutex, struct thread *thread )
72 assert( !mutex->count );
73 /* remove the mutex from the thread list of owned mutexes */
74 if (mutex->next) mutex->next->prev = mutex->prev;
75 if (mutex->prev) mutex->prev->next = mutex->next;
76 else thread->mutex = mutex->next;
78 mutex->next = mutex->prev = NULL;
79 wake_up( &mutex->obj, 0 );
82 int release_mutex( int handle )
86 if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
87 MUTEX_MODIFY_STATE, &mutex_ops )))
89 if (!mutex->count || (mutex->owner != current))
91 SET_ERROR( ERROR_NOT_OWNER );
94 if (!--mutex->count) do_release( mutex, current );
95 release_object( mutex );
99 void abandon_mutexes( struct thread *thread )
101 while (thread->mutex)
103 struct mutex *mutex = thread->mutex;
104 assert( mutex->owner == thread );
106 mutex->abandoned = 1;
107 do_release( mutex, thread );
111 static void mutex_dump( struct object *obj, int verbose )
113 struct mutex *mutex = (struct mutex *)obj;
114 assert( obj->ops == &mutex_ops );
115 printf( "Mutex count=%u owner=%p name='%s'\n",
116 mutex->count, mutex->owner, get_object_name( &mutex->obj) );
119 static int mutex_signaled( struct object *obj, struct thread *thread )
121 struct mutex *mutex = (struct mutex *)obj;
122 assert( obj->ops == &mutex_ops );
123 return (!mutex->count || (mutex->owner == thread));
126 static int mutex_satisfied( struct object *obj, struct thread *thread )
128 struct mutex *mutex = (struct mutex *)obj;
129 assert( obj->ops == &mutex_ops );
130 assert( !mutex->count || (mutex->owner == thread) );
132 if (!mutex->count++) /* FIXME: avoid wrap-around */
134 assert( !mutex->owner );
135 mutex->owner = thread;
137 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
138 thread->mutex = mutex;
140 if (!mutex->abandoned) return 0;
141 mutex->abandoned = 0;
145 static void mutex_destroy( struct object *obj )
147 struct mutex *mutex = (struct mutex *)obj;
148 assert( obj->ops == &mutex_ops );