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