Added several file 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 "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 mutex_dump( 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 mutex_destroy( struct object *obj );
29
30 static const struct object_ops mutex_ops =
31 {
32     mutex_dump,
33     add_queue,
34     remove_queue,
35     mutex_signaled,
36     mutex_satisfied,
37     no_read_fd,
38     no_write_fd,
39     no_flush,
40     mutex_destroy
41 };
42
43
44 struct object *create_mutex( const char *name, int owned )
45 {
46     struct mutex *mutex;
47
48     if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
49         return NULL;
50     if (GET_ERROR() != ERROR_ALREADY_EXISTS)
51     {
52         /* initialize it if it didn't already exist */
53         mutex->count = 0;
54         mutex->owner = NULL;
55         mutex->abandoned = 0;
56         mutex->next = mutex->prev = NULL;
57         if (owned) mutex_satisfied( &mutex->obj, current );
58     }
59     return &mutex->obj;
60 }
61
62 int open_mutex( unsigned int access, int inherit, const char *name )
63 {
64     return open_object( name, &mutex_ops, access, inherit );
65 }
66
67 /* release a mutex once the recursion count is 0 */
68 static void do_release( struct mutex *mutex, struct thread *thread )
69 {
70     assert( !mutex->count );
71     /* remove the mutex from the thread list of owned mutexes */
72     if (mutex->next) mutex->next->prev = mutex->prev;
73     if (mutex->prev) mutex->prev->next = mutex->next;
74     else thread->mutex = mutex->next;
75     mutex->owner = NULL;
76     mutex->next = mutex->prev = NULL;
77     wake_up( &mutex->obj, 0 );
78 }
79
80 int release_mutex( int handle )
81 {
82     struct mutex *mutex;
83
84     if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
85                                                   MUTEX_MODIFY_STATE, &mutex_ops )))
86         return 0;
87     if (!mutex->count || (mutex->owner != current))
88     {
89         SET_ERROR( ERROR_NOT_OWNER );
90         return 0;
91     }
92     if (!--mutex->count) do_release( mutex, current );
93     release_object( mutex );
94     return 1;
95 }
96
97 void abandon_mutexes( struct thread *thread )
98 {
99     while (thread->mutex)
100     {
101         struct mutex *mutex = thread->mutex;
102         assert( mutex->owner == thread );
103         mutex->count = 0;
104         mutex->abandoned = 1;
105         do_release( mutex, thread );
106     }
107 }
108
109 static void mutex_dump( struct object *obj, int verbose )
110 {
111     struct mutex *mutex = (struct mutex *)obj;
112     assert( obj->ops == &mutex_ops );
113     printf( "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
114 }
115
116 static int mutex_signaled( struct object *obj, struct thread *thread )
117 {
118     struct mutex *mutex = (struct mutex *)obj;
119     assert( obj->ops == &mutex_ops );
120     return (!mutex->count || (mutex->owner == thread));
121 }
122
123 static int mutex_satisfied( struct object *obj, struct thread *thread )
124 {
125     struct mutex *mutex = (struct mutex *)obj;
126     assert( obj->ops == &mutex_ops );
127     assert( !mutex->count || (mutex->owner == thread) );
128
129     if (!mutex->count++)  /* FIXME: avoid wrap-around */
130     {
131         assert( !mutex->owner );
132         mutex->owner = thread;
133         mutex->prev  = NULL;
134         if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
135         thread->mutex = mutex;
136     }
137     if (!mutex->abandoned) return 0;
138     mutex->abandoned = 0;
139     return 1;
140 }
141
142 static void mutex_destroy( struct object *obj )
143 {
144     struct mutex *mutex = (struct mutex *)obj;
145     assert( obj->ops == &mutex_ops );
146     free( mutex );
147 }