Added beginnings of server-side file handling.
[wine/multimedia.git] / server / mutex.c
blob375294f2814f678741e89175dd4575c558f2c2be
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/thread.h"
15 struct mutex
17 struct object obj; /* object header */
18 struct thread *owner; /* mutex owner */
19 unsigned int count; /* recursion count */
20 int abandoned; /* has it been abandoned? */
21 struct mutex *next;
22 struct mutex *prev;
25 static void mutex_dump( struct object *obj, int verbose );
26 static int mutex_signaled( struct object *obj, struct thread *thread );
27 static int mutex_satisfied( struct object *obj, struct thread *thread );
28 static void mutex_destroy( struct object *obj );
30 static const struct object_ops mutex_ops =
32 mutex_dump,
33 add_queue,
34 remove_queue,
35 mutex_signaled,
36 mutex_satisfied,
37 mutex_destroy
41 struct object *create_mutex( const char *name, int owned )
43 struct mutex *mutex;
45 if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
46 return NULL;
47 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
49 /* initialize it if it didn't already exist */
50 mutex->count = 0;
51 mutex->owner = NULL;
52 mutex->abandoned = 0;
53 mutex->next = mutex->prev = NULL;
54 if (owned) mutex_satisfied( &mutex->obj, current );
56 return &mutex->obj;
59 int open_mutex( unsigned int access, int inherit, const char *name )
61 return open_object( name, &mutex_ops, access, inherit );
64 /* release a mutex once the recursion count is 0 */
65 static void do_release( struct mutex *mutex, struct thread *thread )
67 assert( !mutex->count );
68 /* remove the mutex from the thread list of owned mutexes */
69 if (mutex->next) mutex->next->prev = mutex->prev;
70 if (mutex->prev) mutex->prev->next = mutex->next;
71 else thread->mutex = mutex->next;
72 mutex->owner = NULL;
73 mutex->next = mutex->prev = NULL;
74 wake_up( &mutex->obj, 0 );
77 int release_mutex( int handle )
79 struct mutex *mutex;
81 if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
82 MUTEX_MODIFY_STATE, &mutex_ops )))
83 return 0;
84 if (!mutex->count || (mutex->owner != current))
86 SET_ERROR( ERROR_NOT_OWNER );
87 return 0;
89 if (!--mutex->count) do_release( mutex, current );
90 release_object( mutex );
91 return 1;
94 void abandon_mutexes( struct thread *thread )
96 while (thread->mutex)
98 struct mutex *mutex = thread->mutex;
99 assert( mutex->owner == thread );
100 mutex->count = 0;
101 mutex->abandoned = 1;
102 do_release( mutex, thread );
106 static void mutex_dump( struct object *obj, int verbose )
108 struct mutex *mutex = (struct mutex *)obj;
109 assert( obj->ops == &mutex_ops );
110 printf( "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
113 static int mutex_signaled( struct object *obj, struct thread *thread )
115 struct mutex *mutex = (struct mutex *)obj;
116 assert( obj->ops == &mutex_ops );
117 return (!mutex->count || (mutex->owner == thread));
120 static int mutex_satisfied( struct object *obj, struct thread *thread )
122 struct mutex *mutex = (struct mutex *)obj;
123 assert( obj->ops == &mutex_ops );
124 assert( !mutex->count || (mutex->owner == thread) );
126 if (!mutex->count++) /* FIXME: avoid wrap-around */
128 assert( !mutex->owner );
129 mutex->owner = thread;
130 mutex->prev = NULL;
131 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
132 thread->mutex = mutex;
134 if (!mutex->abandoned) return 0;
135 mutex->abandoned = 0;
136 return 1;
139 static void mutex_destroy( struct object *obj )
141 struct mutex *mutex = (struct mutex *)obj;
142 assert( obj->ops == &mutex_ops );
143 free( mutex );