mutex.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. };
  54. static void mutex_dump( struct object *obj, int verbose );
  55. static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry );
  56. static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry );
  57. static void mutex_destroy( struct object *obj );
  58. static int mutex_signal( struct object *obj, unsigned int access );
  59. static const struct object_ops mutex_ops =
  60. {
  61. sizeof(struct mutex), /* size */
  62. &mutex_type, /* type */
  63. mutex_dump, /* dump */
  64. add_queue, /* add_queue */
  65. remove_queue, /* remove_queue */
  66. mutex_signaled, /* signaled */
  67. mutex_satisfied, /* satisfied */
  68. mutex_signal, /* signal */
  69. no_get_fd, /* get_fd */
  70. default_map_access, /* map_access */
  71. default_get_sd, /* get_sd */
  72. default_set_sd, /* set_sd */
  73. default_get_full_name, /* get_full_name */
  74. no_lookup_name, /* lookup_name */
  75. directory_link_name, /* link_name */
  76. default_unlink_name, /* unlink_name */
  77. no_open_file, /* open_file */
  78. no_kernel_obj_list, /* get_kernel_obj_list */
  79. no_close_handle, /* close_handle */
  80. mutex_destroy /* destroy */
  81. };
  82. /* grab a mutex for a given thread */
  83. static void do_grab( struct mutex *mutex, struct thread *thread )
  84. {
  85. assert( !mutex->count || (mutex->owner == thread) );
  86. if (!mutex->count++) /* FIXME: avoid wrap-around */
  87. {
  88. assert( !mutex->owner );
  89. mutex->owner = thread;
  90. list_add_head( &thread->mutex_list, &mutex->entry );
  91. }
  92. }
  93. /* release a mutex once the recursion count is 0 */
  94. static void do_release( struct mutex *mutex )
  95. {
  96. assert( !mutex->count );
  97. /* remove the mutex from the thread list of owned mutexes */
  98. list_remove( &mutex->entry );
  99. mutex->owner = NULL;
  100. wake_up( &mutex->obj, 0 );
  101. }
  102. static struct mutex *create_mutex( struct object *root, const struct unicode_str *name,
  103. unsigned int attr, int owned, const struct security_descriptor *sd )
  104. {
  105. struct mutex *mutex;
  106. if ((mutex = create_named_object( root, &mutex_ops, name, attr, sd )))
  107. {
  108. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  109. {
  110. /* initialize it if it didn't already exist */
  111. mutex->count = 0;
  112. mutex->owner = NULL;
  113. mutex->abandoned = 0;
  114. if (owned) do_grab( mutex, current );
  115. }
  116. }
  117. return mutex;
  118. }
  119. void abandon_mutexes( struct thread *thread )
  120. {
  121. struct list *ptr;
  122. while ((ptr = list_head( &thread->mutex_list )) != NULL)
  123. {
  124. struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
  125. assert( mutex->owner == thread );
  126. mutex->count = 0;
  127. mutex->abandoned = 1;
  128. do_release( mutex );
  129. }
  130. }
  131. static void mutex_dump( struct object *obj, int verbose )
  132. {
  133. struct mutex *mutex = (struct mutex *)obj;
  134. assert( obj->ops == &mutex_ops );
  135. fprintf( stderr, "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
  136. }
  137. static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry )
  138. {
  139. struct mutex *mutex = (struct mutex *)obj;
  140. assert( obj->ops == &mutex_ops );
  141. return (!mutex->count || (mutex->owner == get_wait_queue_thread( entry )));
  142. }
  143. static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry )
  144. {
  145. struct mutex *mutex = (struct mutex *)obj;
  146. assert( obj->ops == &mutex_ops );
  147. do_grab( mutex, get_wait_queue_thread( entry ));
  148. if (mutex->abandoned) make_wait_abandoned( entry );
  149. mutex->abandoned = 0;
  150. }
  151. static int mutex_signal( struct object *obj, unsigned int access )
  152. {
  153. struct mutex *mutex = (struct mutex *)obj;
  154. assert( obj->ops == &mutex_ops );
  155. if (!(access & SYNCHRONIZE))
  156. {
  157. set_error( STATUS_ACCESS_DENIED );
  158. return 0;
  159. }
  160. if (!mutex->count || (mutex->owner != current))
  161. {
  162. set_error( STATUS_MUTANT_NOT_OWNED );
  163. return 0;
  164. }
  165. if (!--mutex->count) do_release( mutex );
  166. return 1;
  167. }
  168. static void mutex_destroy( struct object *obj )
  169. {
  170. struct mutex *mutex = (struct mutex *)obj;
  171. assert( obj->ops == &mutex_ops );
  172. if (!mutex->count) return;
  173. mutex->count = 0;
  174. do_release( mutex );
  175. }
  176. /* create a mutex */
  177. DECL_HANDLER(create_mutex)
  178. {
  179. struct mutex *mutex;
  180. struct unicode_str name;
  181. struct object *root;
  182. const struct security_descriptor *sd;
  183. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  184. if (!objattr) return;
  185. if ((mutex = create_mutex( root, &name, objattr->attributes, req->owned, sd )))
  186. {
  187. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  188. reply->handle = alloc_handle( current->process, mutex, req->access, objattr->attributes );
  189. else
  190. reply->handle = alloc_handle_no_access_check( current->process, mutex,
  191. req->access, objattr->attributes );
  192. release_object( mutex );
  193. }
  194. if (root) release_object( root );
  195. }
  196. /* open a handle to a mutex */
  197. DECL_HANDLER(open_mutex)
  198. {
  199. struct unicode_str name = get_req_unicode_str();
  200. reply->handle = open_object( current->process, req->rootdir, req->access,
  201. &mutex_ops, &name, req->attributes );
  202. }
  203. /* release a mutex */
  204. DECL_HANDLER(release_mutex)
  205. {
  206. struct mutex *mutex;
  207. if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
  208. 0, &mutex_ops )))
  209. {
  210. if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
  211. else
  212. {
  213. reply->prev_count = mutex->count;
  214. if (!--mutex->count) do_release( mutex );
  215. }
  216. release_object( mutex );
  217. }
  218. }
  219. /* return details about the mutex */
  220. DECL_HANDLER(query_mutex)
  221. {
  222. struct mutex *mutex;
  223. if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
  224. MUTANT_QUERY_STATE, &mutex_ops )))
  225. {
  226. reply->count = mutex->count;
  227. reply->owned = (mutex->owner == current);
  228. reply->abandoned = mutex->abandoned;
  229. release_object( mutex );
  230. }
  231. }