Release 980927
[wine] / server / mutex.c
1 /*
2  * Server-side mutex management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "winerror.h"
12 #include "winnt.h"
13 #include "server/thread.h"
14
15 struct mutex
16 {
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;
23 };
24
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 );
29
30 static const struct object_ops mutex_ops =
31 {
32     dump_mutex,
33     mutex_signaled,
34     mutex_satisfied,
35     destroy_mutex
36 };
37
38
39 struct object *create_mutex( const char *name, int owned )
40 {
41     struct mutex *mutex;
42
43     if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
44         return NULL;
45     if (GET_ERROR() != ERROR_ALREADY_EXISTS)
46     {
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 );
53     }
54     return &mutex->obj;
55 }
56
57 int open_mutex( unsigned int access, int inherit, const char *name )
58 {
59     return open_object( name, &mutex_ops, access, inherit );
60 }
61
62 /* release a mutex once the recursion count is 0 */
63 static void do_release( struct mutex *mutex, struct thread *thread )
64 {
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 );
73 }
74
75 int release_mutex( int handle )
76 {
77     struct mutex *mutex;
78
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))
83     {
84         SET_ERROR( ERROR_NOT_OWNER );
85         return 0;
86     }
87     if (!--mutex->count) do_release( mutex, current );
88     release_object( mutex );
89     return 1;
90 }
91
92 void abandon_mutexes( struct thread *thread )
93 {
94     while (thread->mutex)
95     {
96         struct mutex *mutex = thread->mutex;
97         assert( mutex->owner == thread );
98         mutex->count = 0;
99         mutex->abandoned = 1;
100         do_release( mutex, thread );
101     }
102 }
103
104 static void dump_mutex( struct object *obj, int verbose )
105 {
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 );
109 }
110
111 static int mutex_signaled( struct object *obj, struct thread *thread )
112 {
113     struct mutex *mutex = (struct mutex *)obj;
114     assert( obj->ops == &mutex_ops );
115     return (!mutex->count || (mutex->owner == thread));
116 }
117
118 static int mutex_satisfied( struct object *obj, struct thread *thread )
119 {
120     struct mutex *mutex = (struct mutex *)obj;
121     assert( obj->ops == &mutex_ops );
122     assert( !mutex->count || (mutex->owner == thread) );
123
124     if (!mutex->count++)  /* FIXME: avoid wrap-around */
125     {
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;
131     }
132     if (!mutex->abandoned) return 0;
133     mutex->abandoned = 0;
134     return 1;
135 }
136
137 static void destroy_mutex( struct object *obj )
138 {
139     struct mutex *mutex = (struct mutex *)obj;
140     assert( obj->ops == &mutex_ops );
141     free( mutex );
142 }