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