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