Redesign of the server communication protocol to allow arbitrary sized
[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 "winnt.h"
12
13 #include "handle.h"
14 #include "thread.h"
15 #include "request.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     sizeof(struct mutex),      /* size */
35     mutex_dump,                /* dump */
36     add_queue,                 /* add_queue */
37     remove_queue,              /* remove_queue */
38     mutex_signaled,            /* signaled */
39     mutex_satisfied,           /* satisfied */
40     NULL,                      /* get_poll_events */
41     NULL,                      /* poll_event */
42     no_get_fd,                 /* get_fd */
43     no_flush,                  /* flush */
44     no_get_file_info,          /* get_file_info */
45     mutex_destroy              /* destroy */
46 };
47
48
49 static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned )
50 {
51     struct mutex *mutex;
52
53     if ((mutex = create_named_object( &mutex_ops, name, len )))
54     {
55         if (get_error() != STATUS_OBJECT_NAME_COLLISION)
56         {
57             /* initialize it if it didn't already exist */
58             mutex->count = 0;
59             mutex->owner = NULL;
60             mutex->abandoned = 0;
61             mutex->next = mutex->prev = NULL;
62             if (owned) mutex_satisfied( &mutex->obj, current );
63         }
64     }
65     return mutex;
66 }
67
68 /* release a mutex once the recursion count is 0 */
69 static void do_release( struct mutex *mutex )
70 {
71     assert( !mutex->count );
72     /* remove the mutex from the thread list of owned mutexes */
73     if (mutex->next) mutex->next->prev = mutex->prev;
74     if (mutex->prev) mutex->prev->next = mutex->next;
75     else mutex->owner->mutex = mutex->next;
76     mutex->owner = NULL;
77     mutex->next = mutex->prev = NULL;
78     wake_up( &mutex->obj, 0 );
79 }
80
81 void abandon_mutexes( struct thread *thread )
82 {
83     while (thread->mutex)
84     {
85         struct mutex *mutex = thread->mutex;
86         assert( mutex->owner == thread );
87         mutex->count = 0;
88         mutex->abandoned = 1;
89         do_release( mutex );
90     }
91 }
92
93 static void mutex_dump( struct object *obj, int verbose )
94 {
95     struct mutex *mutex = (struct mutex *)obj;
96     assert( obj->ops == &mutex_ops );
97     fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner );
98     dump_object_name( &mutex->obj );
99     fputc( '\n', stderr );
100 }
101
102 static int mutex_signaled( struct object *obj, struct thread *thread )
103 {
104     struct mutex *mutex = (struct mutex *)obj;
105     assert( obj->ops == &mutex_ops );
106     return (!mutex->count || (mutex->owner == thread));
107 }
108
109 static int mutex_satisfied( struct object *obj, struct thread *thread )
110 {
111     struct mutex *mutex = (struct mutex *)obj;
112     assert( obj->ops == &mutex_ops );
113     assert( !mutex->count || (mutex->owner == thread) );
114
115     if (!mutex->count++)  /* FIXME: avoid wrap-around */
116     {
117         assert( !mutex->owner );
118         mutex->owner = thread;
119         mutex->prev  = NULL;
120         if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
121         thread->mutex = mutex;
122     }
123     if (!mutex->abandoned) return 0;
124     mutex->abandoned = 0;
125     return 1;
126 }
127
128 static void mutex_destroy( struct object *obj )
129 {
130     struct mutex *mutex = (struct mutex *)obj;
131     assert( obj->ops == &mutex_ops );
132
133     if (!mutex->count) return;
134     mutex->count = 0;
135     do_release( mutex );
136 }
137
138 /* create a mutex */
139 DECL_HANDLER(create_mutex)
140 {
141     struct mutex *mutex;
142
143     reply->handle = 0;
144     if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned )))
145     {
146         reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit );
147         release_object( mutex );
148     }
149 }
150
151 /* open a handle to a mutex */
152 DECL_HANDLER(open_mutex)
153 {
154     reply->handle = open_object( get_req_data(), get_req_data_size(),
155                                  &mutex_ops, req->access, req->inherit );
156 }
157
158 /* release a mutex */
159 DECL_HANDLER(release_mutex)
160 {
161     struct mutex *mutex;
162
163     if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
164                                                  MUTEX_MODIFY_STATE, &mutex_ops )))
165     {
166         if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
167         else if (!--mutex->count) do_release( mutex );
168         release_object( mutex );
169     }
170 }