CreateFile32A was only allowing console opens on CONIN$/OUT$. This
[wine.git] / server / mutex.c
blobdf5fc9f8f4a1b49df1bb1ceb5389e9a8415c9eaf
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 dump_mutex( 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 destroy_mutex( struct object *obj );
30 static const struct object_ops mutex_ops =
32 dump_mutex,
33 mutex_signaled,
34 mutex_satisfied,
35 destroy_mutex
39 struct object *create_mutex( const char *name, int owned )
41 struct mutex *mutex;
43 if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
44 return NULL;
45 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
47 /* initialize it if it didn't already exist */
48 mutex->count = 0;
49 mutex->owner = NULL;
50 mutex->abandoned = 0;
51 mutex->next = mutex->prev = NULL;
52 if (owned) mutex_satisfied( &mutex->obj, current );
54 return &mutex->obj;
57 int open_mutex( unsigned int access, int inherit, const char *name )
59 return open_object( name, &mutex_ops, access, inherit );
62 /* release a mutex once the recursion count is 0 */
63 static void do_release( struct mutex *mutex, struct thread *thread )
65 assert( !mutex->count );
66 /* remove the mutex from the thread list of owned mutexes */
67 if (mutex->next) mutex->next->prev = mutex->prev;
68 if (mutex->prev) mutex->prev->next = mutex->next;
69 else thread->mutex = mutex->next;
70 mutex->owner = NULL;
71 mutex->next = mutex->prev = NULL;
72 wake_up( &mutex->obj, 0 );
75 int release_mutex( int handle )
77 struct mutex *mutex;
79 if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
80 MUTEX_MODIFY_STATE, &mutex_ops )))
81 return 0;
82 if (!mutex->count || (mutex->owner != current))
84 SET_ERROR( ERROR_NOT_OWNER );
85 return 0;
87 if (!--mutex->count) do_release( mutex, current );
88 release_object( mutex );
89 return 1;
92 void abandon_mutexes( struct thread *thread )
94 while (thread->mutex)
96 struct mutex *mutex = thread->mutex;
97 assert( mutex->owner == thread );
98 mutex->count = 0;
99 mutex->abandoned = 1;
100 do_release( mutex, thread );
104 static void dump_mutex( struct object *obj, int verbose )
106 struct mutex *mutex = (struct mutex *)obj;
107 assert( obj->ops == &mutex_ops );
108 printf( "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
111 static int mutex_signaled( struct object *obj, struct thread *thread )
113 struct mutex *mutex = (struct mutex *)obj;
114 assert( obj->ops == &mutex_ops );
115 return (!mutex->count || (mutex->owner == thread));
118 static int mutex_satisfied( struct object *obj, struct thread *thread )
120 struct mutex *mutex = (struct mutex *)obj;
121 assert( obj->ops == &mutex_ops );
122 assert( !mutex->count || (mutex->owner == thread) );
124 if (!mutex->count++) /* FIXME: avoid wrap-around */
126 assert( !mutex->owner );
127 mutex->owner = thread;
128 mutex->prev = NULL;
129 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
130 thread->mutex = mutex;
132 if (!mutex->abandoned) return 0;
133 mutex->abandoned = 0;
134 return 1;
137 static void destroy_mutex( struct object *obj )
139 struct mutex *mutex = (struct mutex *)obj;
140 assert( obj->ops == &mutex_ops );
141 free( mutex );