Cosmetics.
[wine.git] / server / mutex.c
blob1127de2f6b86827db89673fc0aa4943f0f343c89
1 /*
2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include "winerror.h"
12 #include "winnt.h"
13 #include "server/process.h"
14 #include "server/thread.h"
16 struct mutex
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? */
22 struct mutex *next;
23 struct mutex *prev;
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 =
33 mutex_dump,
34 add_queue,
35 remove_queue,
36 mutex_signaled,
37 mutex_satisfied,
38 no_read_fd,
39 no_write_fd,
40 no_flush,
41 no_get_file_info,
42 mutex_destroy
46 struct object *create_mutex( const char *name, int owned )
48 struct mutex *mutex;
50 if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
51 return NULL;
52 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
54 /* initialize it if it didn't already exist */
55 mutex->count = 0;
56 mutex->owner = NULL;
57 mutex->abandoned = 0;
58 mutex->next = mutex->prev = NULL;
59 if (owned) mutex_satisfied( &mutex->obj, current );
61 return &mutex->obj;
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;
77 mutex->owner = NULL;
78 mutex->next = mutex->prev = NULL;
79 wake_up( &mutex->obj, 0 );
82 int release_mutex( int handle )
84 struct mutex *mutex;
86 if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
87 MUTEX_MODIFY_STATE, &mutex_ops )))
88 return 0;
89 if (!mutex->count || (mutex->owner != current))
91 SET_ERROR( ERROR_NOT_OWNER );
92 return 0;
94 if (!--mutex->count) do_release( mutex, current );
95 release_object( mutex );
96 return 1;
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, 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;
136 mutex->prev = NULL;
137 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
138 thread->mutex = mutex;
140 if (!mutex->abandoned) return 0;
141 mutex->abandoned = 0;
142 return 1;
145 static void mutex_destroy( struct object *obj )
147 struct mutex *mutex = (struct mutex *)obj;
148 assert( obj->ops == &mutex_ops );
149 free( mutex );