d2d1: Use D3D11 interfaces to create buffers.
[wine.git] / server / mutex.c
blob75ff5117d3fa90b38044b506aa247e2d21ccffee
1 /*
2 * Server-side mutex management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
37 #include "security.h"
39 static const WCHAR mutex_name[] = {'M','u','t','a','n','t'};
41 struct type_descr mutex_type =
43 { mutex_name, sizeof(mutex_name) }, /* name */
44 MUTANT_ALL_ACCESS, /* valid_access */
45 { /* mapping */
46 STANDARD_RIGHTS_READ | MUTANT_QUERY_STATE,
47 STANDARD_RIGHTS_WRITE,
48 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
49 MUTANT_ALL_ACCESS
53 struct mutex
55 struct object obj; /* object header */
56 struct thread *owner; /* mutex owner */
57 unsigned int count; /* recursion count */
58 int abandoned; /* has it been abandoned? */
59 struct list entry; /* entry in owner thread mutex list */
62 static void mutex_dump( struct object *obj, int verbose );
63 static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry );
64 static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry );
65 static void mutex_destroy( struct object *obj );
66 static int mutex_signal( struct object *obj, unsigned int access );
68 static const struct object_ops mutex_ops =
70 sizeof(struct mutex), /* size */
71 &mutex_type, /* type */
72 mutex_dump, /* dump */
73 add_queue, /* add_queue */
74 remove_queue, /* remove_queue */
75 mutex_signaled, /* signaled */
76 mutex_satisfied, /* satisfied */
77 mutex_signal, /* signal */
78 no_get_fd, /* get_fd */
79 default_map_access, /* map_access */
80 default_get_sd, /* get_sd */
81 default_set_sd, /* set_sd */
82 default_get_full_name, /* get_full_name */
83 no_lookup_name, /* lookup_name */
84 directory_link_name, /* link_name */
85 default_unlink_name, /* unlink_name */
86 no_open_file, /* open_file */
87 no_kernel_obj_list, /* get_kernel_obj_list */
88 no_close_handle, /* close_handle */
89 mutex_destroy /* destroy */
93 /* grab a mutex for a given thread */
94 static void do_grab( struct mutex *mutex, struct thread *thread )
96 assert( !mutex->count || (mutex->owner == thread) );
98 if (!mutex->count++) /* FIXME: avoid wrap-around */
100 assert( !mutex->owner );
101 mutex->owner = thread;
102 list_add_head( &thread->mutex_list, &mutex->entry );
106 /* release a mutex once the recursion count is 0 */
107 static void do_release( struct mutex *mutex )
109 assert( !mutex->count );
110 /* remove the mutex from the thread list of owned mutexes */
111 list_remove( &mutex->entry );
112 mutex->owner = NULL;
113 wake_up( &mutex->obj, 0 );
116 static struct mutex *create_mutex( struct object *root, const struct unicode_str *name,
117 unsigned int attr, int owned, const struct security_descriptor *sd )
119 struct mutex *mutex;
121 if ((mutex = create_named_object( root, &mutex_ops, name, attr, sd )))
123 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
125 /* initialize it if it didn't already exist */
126 mutex->count = 0;
127 mutex->owner = NULL;
128 mutex->abandoned = 0;
129 if (owned) do_grab( mutex, current );
132 return mutex;
135 void abandon_mutexes( struct thread *thread )
137 struct list *ptr;
139 while ((ptr = list_head( &thread->mutex_list )) != NULL)
141 struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
142 assert( mutex->owner == thread );
143 mutex->count = 0;
144 mutex->abandoned = 1;
145 do_release( mutex );
149 static void mutex_dump( struct object *obj, int verbose )
151 struct mutex *mutex = (struct mutex *)obj;
152 assert( obj->ops == &mutex_ops );
153 fprintf( stderr, "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
156 static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry )
158 struct mutex *mutex = (struct mutex *)obj;
159 assert( obj->ops == &mutex_ops );
160 return (!mutex->count || (mutex->owner == get_wait_queue_thread( entry )));
163 static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry )
165 struct mutex *mutex = (struct mutex *)obj;
166 assert( obj->ops == &mutex_ops );
168 do_grab( mutex, get_wait_queue_thread( entry ));
169 if (mutex->abandoned) make_wait_abandoned( entry );
170 mutex->abandoned = 0;
173 static int mutex_signal( struct object *obj, unsigned int access )
175 struct mutex *mutex = (struct mutex *)obj;
176 assert( obj->ops == &mutex_ops );
178 if (!(access & SYNCHRONIZE))
180 set_error( STATUS_ACCESS_DENIED );
181 return 0;
183 if (!mutex->count || (mutex->owner != current))
185 set_error( STATUS_MUTANT_NOT_OWNED );
186 return 0;
188 if (!--mutex->count) do_release( mutex );
189 return 1;
192 static void mutex_destroy( struct object *obj )
194 struct mutex *mutex = (struct mutex *)obj;
195 assert( obj->ops == &mutex_ops );
197 if (!mutex->count) return;
198 mutex->count = 0;
199 do_release( mutex );
202 /* create a mutex */
203 DECL_HANDLER(create_mutex)
205 struct mutex *mutex;
206 struct unicode_str name;
207 struct object *root;
208 const struct security_descriptor *sd;
209 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
211 if (!objattr) return;
213 if ((mutex = create_mutex( root, &name, objattr->attributes, req->owned, sd )))
215 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
216 reply->handle = alloc_handle( current->process, mutex, req->access, objattr->attributes );
217 else
218 reply->handle = alloc_handle_no_access_check( current->process, mutex,
219 req->access, objattr->attributes );
220 release_object( mutex );
223 if (root) release_object( root );
226 /* open a handle to a mutex */
227 DECL_HANDLER(open_mutex)
229 struct unicode_str name = get_req_unicode_str();
231 reply->handle = open_object( current->process, req->rootdir, req->access,
232 &mutex_ops, &name, req->attributes );
235 /* release a mutex */
236 DECL_HANDLER(release_mutex)
238 struct mutex *mutex;
240 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
241 0, &mutex_ops )))
243 if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
244 else
246 reply->prev_count = mutex->count;
247 if (!--mutex->count) do_release( mutex );
249 release_object( mutex );
253 /* return details about the mutex */
254 DECL_HANDLER(query_mutex)
256 struct mutex *mutex;
258 if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
259 MUTANT_QUERY_STATE, &mutex_ops )))
261 reply->count = mutex->count;
262 reply->owned = (mutex->owner == current);
263 reply->abandoned = mutex->abandoned;
265 release_object( mutex );