Rearrange winver detection code and cache the winver value we
[wine.git] / server / mutex.c
blob2adaacb25008d6a3a044e03f5be04d91e12dcfbf
1 /*
2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include "winerror.h"
12 #include "winnt.h"
14 #include "handle.h"
15 #include "thread.h"
17 struct mutex
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;
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 );
32 static const struct object_ops mutex_ops =
34 mutex_dump,
35 add_queue,
36 remove_queue,
37 mutex_signaled,
38 mutex_satisfied,
39 no_read_fd,
40 no_write_fd,
41 no_flush,
42 no_get_file_info,
43 mutex_destroy
47 static struct object *create_mutex( const char *name, int owned )
49 struct mutex *mutex;
51 if (!(mutex = (struct mutex *)create_named_object( name, &mutex_ops, sizeof(*mutex) )))
52 return NULL;
53 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
55 /* initialize it if it didn't already exist */
56 mutex->count = 0;
57 mutex->owner = NULL;
58 mutex->abandoned = 0;
59 mutex->next = mutex->prev = NULL;
60 if (owned) mutex_satisfied( &mutex->obj, current );
62 return &mutex->obj;
65 /* release a mutex once the recursion count is 0 */
66 static void do_release( struct mutex *mutex, struct thread *thread )
68 assert( !mutex->count );
69 /* remove the mutex from the thread list of owned mutexes */
70 if (mutex->next) mutex->next->prev = mutex->prev;
71 if (mutex->prev) mutex->prev->next = mutex->next;
72 else thread->mutex = mutex->next;
73 mutex->owner = NULL;
74 mutex->next = mutex->prev = NULL;
75 wake_up( &mutex->obj, 0 );
78 static int release_mutex( int handle )
80 struct mutex *mutex;
82 if (!(mutex = (struct mutex *)get_handle_obj( current->process, handle,
83 MUTEX_MODIFY_STATE, &mutex_ops )))
84 return 0;
85 if (!mutex->count || (mutex->owner != current))
87 SET_ERROR( ERROR_NOT_OWNER );
88 return 0;
90 if (!--mutex->count) do_release( mutex, current );
91 release_object( mutex );
92 return 1;
95 void abandon_mutexes( struct thread *thread )
97 while (thread->mutex)
99 struct mutex *mutex = thread->mutex;
100 assert( mutex->owner == thread );
101 mutex->count = 0;
102 mutex->abandoned = 1;
103 do_release( mutex, thread );
107 static void mutex_dump( struct object *obj, int verbose )
109 struct mutex *mutex = (struct mutex *)obj;
110 assert( obj->ops == &mutex_ops );
111 printf( "Mutex count=%u owner=%p name='%s'\n",
112 mutex->count, mutex->owner, get_object_name( &mutex->obj) );
115 static int mutex_signaled( struct object *obj, struct thread *thread )
117 struct mutex *mutex = (struct mutex *)obj;
118 assert( obj->ops == &mutex_ops );
119 return (!mutex->count || (mutex->owner == thread));
122 static int mutex_satisfied( struct object *obj, struct thread *thread )
124 struct mutex *mutex = (struct mutex *)obj;
125 assert( obj->ops == &mutex_ops );
126 assert( !mutex->count || (mutex->owner == thread) );
128 if (!mutex->count++) /* FIXME: avoid wrap-around */
130 assert( !mutex->owner );
131 mutex->owner = thread;
132 mutex->prev = NULL;
133 if ((mutex->next = thread->mutex)) mutex->next->prev = mutex;
134 thread->mutex = mutex;
136 if (!mutex->abandoned) return 0;
137 mutex->abandoned = 0;
138 return 1;
141 static void mutex_destroy( struct object *obj )
143 struct mutex *mutex = (struct mutex *)obj;
144 assert( obj->ops == &mutex_ops );
145 free( mutex );
148 /* create a mutex */
149 DECL_HANDLER(create_mutex)
151 struct create_mutex_reply reply = { -1 };
152 struct object *obj;
153 char *name = (char *)data;
154 if (!len) name = NULL;
155 else CHECK_STRING( "create_mutex", name, len );
157 obj = create_mutex( name, req->owned );
158 if (obj)
160 reply.handle = alloc_handle( current->process, obj, MUTEX_ALL_ACCESS, req->inherit );
161 release_object( obj );
163 send_reply( current, -1, 1, &reply, sizeof(reply) );
166 /* open a handle to a mutex */
167 DECL_HANDLER(open_mutex)
169 struct open_mutex_reply reply;
170 char *name = (char *)data;
171 if (!len) name = NULL;
172 else CHECK_STRING( "open_mutex", name, len );
174 reply.handle = open_object( name, &mutex_ops, req->access, req->inherit );
175 send_reply( current, -1, 1, &reply, sizeof(reply) );
178 /* release a mutex */
179 DECL_HANDLER(release_mutex)
181 if (release_mutex( req->handle )) CLEAR_ERROR();
182 send_reply( current, -1, 0 );