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