Added add_queue/remove_queue to server object operations.
[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     add_queue,
34     remove_queue,
35     mutex_signaled,
36     mutex_satisfied,
37     destroy_mutex
38 };
39
40
41 struct object *create_mutex( const char *name, int owned )
42 {
43     struct mutex *mutex;
44
45     if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
46         return NULL;
47     if (GET_ERROR() != ERROR_ALREADY_EXISTS)
48     {
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 );
55     }
56     return &mutex->obj;
57 }
58
59 int open_mutex( unsigned int access, int inherit, const char *name )
60 {
61     return open_object( name, &mutex_ops, access, inherit );
62 }
63
64 /* release a mutex once the recursion count is 0 */
65 static void do_release( struct mutex *mutex, struct thread *thread )
66 {
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 );
75 }
76
77 int release_mutex( int handle )
78 {
79     struct mutex *mutex;
80
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))
85     {
86         SET_ERROR( ERROR_NOT_OWNER );
87         return 0;
88     }
89     if (!--mutex->count) do_release( mutex, current );
90     release_object( mutex );
91     return 1;
92 }
93
94 void abandon_mutexes( struct thread *thread )
95 {
96     while (thread->mutex)
97     {
98         struct mutex *mutex = thread->mutex;
99         assert( mutex->owner == thread );
100         mutex->count = 0;
101         mutex->abandoned = 1;
102         do_release( mutex, thread );
103     }
104 }
105
106 static void dump_mutex( struct object *obj, int verbose )
107 {
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 );
111 }
112
113 static int mutex_signaled( struct object *obj, struct thread *thread )
114 {
115     struct mutex *mutex = (struct mutex *)obj;
116     assert( obj->ops == &mutex_ops );
117     return (!mutex->count || (mutex->owner == thread));
118 }
119
120 static int mutex_satisfied( struct object *obj, struct thread *thread )
121 {
122     struct mutex *mutex = (struct mutex *)obj;
123     assert( obj->ops == &mutex_ops );
124     assert( !mutex->count || (mutex->owner == thread) );
125
126     if (!mutex->count++)  /* FIXME: avoid wrap-around */
127     {
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;
133     }
134     if (!mutex->abandoned) return 0;
135     mutex->abandoned = 0;
136     return 1;
137 }
138
139 static void destroy_mutex( struct object *obj )
140 {
141     struct mutex *mutex = (struct mutex *)obj;
142     assert( obj->ops == &mutex_ops );
143     free( mutex );
144 }