mutex.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Server-side mutex management
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <sys/types.h>
  26. #include "ntstatus.h"
  27. #define WIN32_NO_STATUS
  28. #include "windef.h"
  29. #include "winternl.h"
  30. #include "handle.h"
  31. #include "thread.h"
  32. #include "request.h"
  33. #include "security.h"
  34. static const WCHAR mutex_name[] = {'M','u','t','a','n','t'};
  35. struct type_descr mutex_type =
  36. {
  37. { mutex_name, sizeof(mutex_name) }, /* name */
  38. MUTANT_ALL_ACCESS, /* valid_access */
  39. { /* mapping */
  40. STANDARD_RIGHTS_READ | MUTANT_QUERY_STATE,
  41. STANDARD_RIGHTS_WRITE,
  42. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
  43. MUTANT_ALL_ACCESS
  44. },
  45. };
  46. struct mutex
  47. {
  48. struct object obj; /* object header */
  49. struct thread *owner; /* mutex owner */
  50. unsigned int count; /* recursion count */
  51. int abandoned; /* has it been abandoned? */
  52. struct list entry; /* entry in owner thread mutex list */
  53. struct fast_sync *fast_sync; /* fast synchronization object */
  54. };
  55. static void mutex_dump( struct object *obj, int verbose );
  56. static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry );
  57. static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry );
  58. static void mutex_destroy( struct object *obj );
  59. static int mutex_signal( struct object *obj, unsigned int access );
  60. static struct fast_sync *mutex_get_fast_sync( struct object *obj );
  61. static const struct object_ops mutex_ops =
  62. {
  63. sizeof(struct mutex), /* size */
  64. &mutex_type, /* type */
  65. mutex_dump, /* dump */
  66. add_queue, /* add_queue */
  67. remove_queue, /* remove_queue */
  68. mutex_signaled, /* signaled */
  69. mutex_satisfied, /* satisfied */
  70. mutex_signal, /* signal */
  71. no_get_fd, /* get_fd */
  72. default_map_access, /* map_access */
  73. default_get_sd, /* get_sd */
  74. default_set_sd, /* set_sd */
  75. default_get_full_name, /* get_full_name */
  76. no_lookup_name, /* lookup_name */
  77. directory_link_name, /* link_name */
  78. default_unlink_name, /* unlink_name */
  79. no_open_file, /* open_file */
  80. no_kernel_obj_list, /* get_kernel_obj_list */
  81. mutex_get_fast_sync, /* get_fast_sync */
  82. no_close_handle, /* close_handle */
  83. mutex_destroy /* destroy */
  84. };
  85. /* grab a mutex for a given thread */
  86. static void do_grab( struct mutex *mutex, struct thread *thread )
  87. {
  88. assert( !mutex->count || (mutex->owner == thread) );
  89. if (!mutex->count++) /* FIXME: avoid wrap-around */
  90. {
  91. assert( !mutex->owner );
  92. mutex->owner = thread;
  93. list_add_head( &thread->mutex_list, &mutex->entry );
  94. }
  95. }
  96. /* release a mutex once the recursion count is 0 */
  97. static void do_release( struct mutex *mutex )
  98. {
  99. assert( !mutex->count );
  100. /* remove the mutex from the thread list of owned mutexes */
  101. list_remove( &mutex->entry );
  102. mutex->owner = NULL;
  103. wake_up( &mutex->obj, 0 );
  104. }
  105. static struct mutex *create_mutex( struct object *root, const struct unicode_str *name,
  106. unsigned int attr, int owned, const struct security_descriptor *sd )
  107. {
  108. struct mutex *mutex;
  109. if ((mutex = create_named_object( root, &mutex_ops, name, attr, sd )))
  110. {
  111. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  112. {
  113. /* initialize it if it didn't already exist */
  114. mutex->count = 0;
  115. mutex->owner = NULL;
  116. mutex->abandoned = 0;
  117. if (owned) do_grab( mutex, current );
  118. mutex->fast_sync = NULL;
  119. }
  120. }
  121. return mutex;
  122. }
  123. void abandon_mutexes( struct thread *thread )
  124. {
  125. struct list *ptr;
  126. while ((ptr = list_head( &thread->mutex_list )) != NULL)
  127. {
  128. struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
  129. assert( mutex->owner == thread );
  130. mutex->count = 0;
  131. mutex->abandoned = 1;
  132. do_release( mutex );
  133. }
  134. }
  135. static void mutex_dump( struct object *obj, int verbose )
  136. {
  137. struct mutex *mutex = (struct mutex *)obj;
  138. assert( obj->ops == &mutex_ops );
  139. fprintf( stderr, "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
  140. }
  141. static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry )
  142. {
  143. struct mutex *mutex = (struct mutex *)obj;
  144. assert( obj->ops == &mutex_ops );
  145. return (!mutex->count || (mutex->owner == get_wait_queue_thread( entry )));
  146. }
  147. static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry )
  148. {
  149. struct mutex *mutex = (struct mutex *)obj;
  150. assert( obj->ops == &mutex_ops );
  151. do_grab( mutex, get_wait_queue_thread( entry ));
  152. if (mutex->abandoned) make_wait_abandoned( entry );
  153. mutex->abandoned = 0;
  154. }
  155. static int mutex_signal( struct object *obj, unsigned int access )
  156. {
  157. struct mutex *mutex = (struct mutex *)obj;
  158. assert( obj->ops == &mutex_ops );
  159. if (!(access & SYNCHRONIZE))
  160. {
  161. set_error( STATUS_ACCESS_DENIED );
  162. return 0;
  163. }
  164. if (!mutex->count || (mutex->owner != current))
  165. {
  166. set_error( STATUS_MUTANT_NOT_OWNED );
  167. return 0;
  168. }
  169. if (!--mutex->count) do_release( mutex );
  170. return 1;
  171. }
  172. static struct fast_sync *mutex_get_fast_sync( struct object *obj )
  173. {
  174. struct mutex *mutex = (struct mutex *)obj;
  175. if (!mutex->fast_sync)
  176. mutex->fast_sync = fast_create_mutex( mutex->owner ? mutex->owner->id : 0, mutex->count );
  177. if (mutex->fast_sync) grab_object( mutex->fast_sync );
  178. return mutex->fast_sync;
  179. }
  180. static void mutex_destroy( struct object *obj )
  181. {
  182. struct mutex *mutex = (struct mutex *)obj;
  183. assert( obj->ops == &mutex_ops );
  184. if (mutex->count)
  185. {
  186. mutex->count = 0;
  187. do_release( mutex );
  188. }
  189. if (mutex->fast_sync) release_object( mutex->fast_sync );
  190. }
  191. /* create a mutex */
  192. DECL_HANDLER(create_mutex)
  193. {
  194. struct mutex *mutex;
  195. struct unicode_str name;
  196. struct object *root;
  197. const struct security_descriptor *sd;
  198. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  199. if (!objattr) return;
  200. if ((mutex = create_mutex( root, &name, objattr->attributes, req->owned, sd )))
  201. {
  202. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  203. reply->handle = alloc_handle( current->process, mutex, req->access, objattr->attributes );
  204. else
  205. reply->handle = alloc_handle_no_access_check( current->process, mutex,
  206. req->access, objattr->attributes );
  207. release_object( mutex );
  208. }
  209. if (root) release_object( root );
  210. }
  211. /* open a handle to a mutex */
  212. DECL_HANDLER(open_mutex)
  213. {
  214. struct unicode_str name = get_req_unicode_str();
  215. reply->handle = open_object( current->process, req->rootdir, req->access,
  216. &mutex_ops, &name, req->attributes );
  217. }
  218. /* release a mutex */
  219. DECL_HANDLER(release_mutex)
  220. {
  221. struct mutex *mutex;
  222. if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
  223. 0, &mutex_ops )))
  224. {
  225. if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
  226. else
  227. {
  228. reply->prev_count = mutex->count;
  229. if (!--mutex->count) do_release( mutex );
  230. }
  231. release_object( mutex );
  232. }
  233. }
  234. /* return details about the mutex */
  235. DECL_HANDLER(query_mutex)
  236. {
  237. struct mutex *mutex;
  238. if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
  239. MUTANT_QUERY_STATE, &mutex_ops )))
  240. {
  241. reply->count = mutex->count;
  242. reply->owned = (mutex->owner == current);
  243. reply->abandoned = mutex->abandoned;
  244. release_object( mutex );
  245. }
  246. }