Server reorganization:
[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
14 #include "handle.h"
15 #include "thread.h"
16
17 struct mutex
18 {
19     struct object  obj;             /* object header */
20     struct thread *owner;           /* mutex owner */
21     unsigned int   count;           /* recursion count */
22     int            abandoned;       /* has it been abandoned? */
23     struct mutex  *next;
24     struct mutex  *prev;
25 };
26
27 static void mutex_dump( struct object *obj, int verbose );
28 static int mutex_signaled( struct object *obj, struct thread *thread );
29 static int mutex_satisfied( struct object *obj, struct thread *thread );
30 static void mutex_destroy( struct object *obj );
31
32 static const struct object_ops mutex_ops =
33 {
34     mutex_dump,
35     add_queue,
36     remove_queue,
37     mutex_signaled,
38     mutex_satisfied,
39     no_read_fd,
40     no_write_fd,
41     no_flush,
42     no_get_file_info,
43     mutex_destroy
44 };
45
46
47 static struct object *create_mutex( const char *name, int owned )
48 {
49     struct mutex *mutex;
50
51     if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
52         return NULL;
53     if (GET_ERROR() != ERROR_ALREADY_EXISTS)
54     {
55         /* initialize it if it didn't already exist */
56         mutex->count = 0;
57         mutex->owner = NULL;
58         mutex->abandoned = 0;
59         mutex->next = mutex->prev = NULL;
60         if (owned) mutex_satisfied( &mutex->obj, current );
61     }
62     return &mutex->obj;
63 }
64
65 /* release a mutex once the recursion count is 0 */
66 static void do_release( struct mutex *mutex, struct thread *thread )
67 {
68     assert( !mutex->count );
69     /* remove the mutex from the thread list of owned mutexes */
70     if (mutex->next) mutex->next->prev = mutex->prev;
71     if (mutex->prev) mutex->prev->next = mutex->next;
72     else thread->mutex = mutex->next;
73     mutex->owner = NULL;
74     mutex->next = mutex->prev = NULL;
75     wake_up( &mutex->obj, 0 );
76 }
77
78 static int release_mutex( int handle )
79 {
80     struct mutex *mutex;
81
82     if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
83                                                   MUTEX_MODIFY_STATE, &mutex_ops )))
84         return 0;
85     if (!mutex->count || (mutex->owner != current))
86     {
87         SET_ERROR( ERROR_NOT_OWNER );
88         return 0;
89     }
90     if (!--mutex->count) do_release( mutex, current );
91     release_object( mutex );
92     return 1;
93 }
94
95 void abandon_mutexes( struct thread *thread )
96 {
97     while (thread->mutex)
98     {
99         struct mutex *mutex = thread->mutex;
100         assert( mutex->owner == thread );
101         mutex->count = 0;
102         mutex->abandoned = 1;
103         do_release( mutex, thread );
104     }
105 }
106
107 static void mutex_dump( struct object *obj, int verbose )
108 {
109     struct mutex *mutex = (struct mutex *)obj;
110     assert( obj->ops == &mutex_ops );
111     printf( "Mutex count=%u owner=%p name='%s'\n",
112             mutex->count, mutex->owner, get_object_name( &mutex->obj) );
113 }
114
115 static int mutex_signaled( struct object *obj, struct thread *thread )
116 {
117     struct mutex *mutex = (struct mutex *)obj;
118     assert( obj->ops == &mutex_ops );
119     return (!mutex->count || (mutex->owner == thread));
120 }
121
122 static int mutex_satisfied( struct object *obj, struct thread *thread )
123 {
124     struct mutex *mutex = (struct mutex *)obj;
125     assert( obj->ops == &mutex_ops );
126     assert( !mutex->count || (mutex->owner == thread) );
127
128     if (!mutex->count++)  /* FIXME: avoid wrap-around */
129     {
130         assert( !mutex->owner );
131         mutex->owner = thread;
132         mutex->prev  = NULL;
133         if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
134         thread->mutex = mutex;
135     }
136     if (!mutex->abandoned) return 0;
137     mutex->abandoned = 0;
138     return 1;
139 }
140
141 static void mutex_destroy( struct object *obj )
142 {
143     struct mutex *mutex = (struct mutex *)obj;
144     assert( obj->ops == &mutex_ops );
145     free( mutex );
146 }
147
148 /* create a mutex */
149 DECL_HANDLER(create_mutex)
150 {
151     struct create_mutex_reply reply = { -1 };
152     struct object *obj;
153     char *name = (char *)data;
154     if (!len) name = NULL;
155     else CHECK_STRING( "create_mutex", name, len );
156
157     obj = create_mutex( name, req->owned );
158     if (obj)
159     {
160         reply.handle = alloc_handle( current->process, obj, MUTEX_ALL_ACCESS, req->inherit );
161         release_object( obj );
162     }
163     send_reply( current, -1, 1, &reply, sizeof(reply) );
164 }
165
166 /* open a handle to a mutex */
167 DECL_HANDLER(open_mutex)
168 {
169     struct open_mutex_reply reply;
170     char *name = (char *)data;
171     if (!len) name = NULL;
172     else CHECK_STRING( "open_mutex", name, len );
173
174     reply.handle = open_object( name, &mutex_ops, req->access, req->inherit );
175     send_reply( current, -1, 1, &reply, sizeof(reply) );
176 }
177
178 /* release a mutex */
179 DECL_HANDLER(release_mutex)
180 {
181     if (release_mutex( req->handle )) CLEAR_ERROR();
182     send_reply( current, -1, 0 );
183 }