mutex.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "wine/port.h"
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.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. struct mutex
  35. {
  36. struct object obj; /* object header */
  37. struct thread *owner; /* mutex owner */
  38. unsigned int count; /* recursion count */
  39. int abandoned; /* has it been abandoned? */
  40. struct list entry; /* entry in owner thread mutex list */
  41. };
  42. static void mutex_dump( struct object *obj, int verbose );
  43. static struct object_type *mutex_get_type( struct object *obj );
  44. static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry );
  45. static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry );
  46. static unsigned int mutex_map_access( struct object *obj, unsigned int access );
  47. static void mutex_destroy( struct object *obj );
  48. static int mutex_signal( struct object *obj, unsigned int access );
  49. static const struct object_ops mutex_ops =
  50. {
  51. sizeof(struct mutex), /* size */
  52. mutex_dump, /* dump */
  53. mutex_get_type, /* get_type */
  54. add_queue, /* add_queue */
  55. remove_queue, /* remove_queue */
  56. mutex_signaled, /* signaled */
  57. NULL, /* get_esync_fd */
  58. mutex_satisfied, /* satisfied */
  59. mutex_signal, /* signal */
  60. no_get_fd, /* get_fd */
  61. mutex_map_access, /* map_access */
  62. default_get_sd, /* get_sd */
  63. default_set_sd, /* set_sd */
  64. no_lookup_name, /* lookup_name */
  65. directory_link_name, /* link_name */
  66. default_unlink_name, /* unlink_name */
  67. no_open_file, /* open_file */
  68. no_kernel_obj_list, /* get_kernel_obj_list */
  69. no_alloc_handle, /* alloc_handle */
  70. no_close_handle, /* close_handle */
  71. mutex_destroy /* destroy */
  72. };
  73. /* grab a mutex for a given thread */
  74. static void do_grab( struct mutex *mutex, struct thread *thread )
  75. {
  76. assert( !mutex->count || (mutex->owner == thread) );
  77. if (!mutex->count++) /* FIXME: avoid wrap-around */
  78. {
  79. assert( !mutex->owner );
  80. mutex->owner = thread;
  81. list_add_head( &thread->mutex_list, &mutex->entry );
  82. }
  83. }
  84. /* release a mutex once the recursion count is 0 */
  85. static void do_release( struct mutex *mutex )
  86. {
  87. assert( !mutex->count );
  88. /* remove the mutex from the thread list of owned mutexes */
  89. list_remove( &mutex->entry );
  90. mutex->owner = NULL;
  91. wake_up( &mutex->obj, 0 );
  92. }
  93. static struct mutex *create_mutex( struct object *root, const struct unicode_str *name,
  94. unsigned int attr, int owned, const struct security_descriptor *sd )
  95. {
  96. struct mutex *mutex;
  97. if ((mutex = create_named_object( root, &mutex_ops, name, attr, sd )))
  98. {
  99. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  100. {
  101. /* initialize it if it didn't already exist */
  102. mutex->count = 0;
  103. mutex->owner = NULL;
  104. mutex->abandoned = 0;
  105. if (owned) do_grab( mutex, current );
  106. }
  107. }
  108. return mutex;
  109. }
  110. void abandon_mutexes( struct thread *thread )
  111. {
  112. struct list *ptr;
  113. while ((ptr = list_head( &thread->mutex_list )) != NULL)
  114. {
  115. struct mutex *mutex = LIST_ENTRY( ptr, struct mutex, entry );
  116. assert( mutex->owner == thread );
  117. mutex->count = 0;
  118. mutex->abandoned = 1;
  119. do_release( mutex );
  120. }
  121. }
  122. static void mutex_dump( struct object *obj, int verbose )
  123. {
  124. struct mutex *mutex = (struct mutex *)obj;
  125. assert( obj->ops == &mutex_ops );
  126. fprintf( stderr, "Mutex count=%u owner=%p\n", mutex->count, mutex->owner );
  127. }
  128. static struct object_type *mutex_get_type( struct object *obj )
  129. {
  130. static const struct unicode_str str = { type_Mutant, sizeof(type_Mutant) };
  131. return get_object_type( &str );
  132. }
  133. static int mutex_signaled( struct object *obj, struct wait_queue_entry *entry )
  134. {
  135. struct mutex *mutex = (struct mutex *)obj;
  136. assert( obj->ops == &mutex_ops );
  137. return (!mutex->count || (mutex->owner == get_wait_queue_thread( entry )));
  138. }
  139. static void mutex_satisfied( struct object *obj, struct wait_queue_entry *entry )
  140. {
  141. struct mutex *mutex = (struct mutex *)obj;
  142. assert( obj->ops == &mutex_ops );
  143. do_grab( mutex, get_wait_queue_thread( entry ));
  144. if (mutex->abandoned) make_wait_abandoned( entry );
  145. mutex->abandoned = 0;
  146. }
  147. static unsigned int mutex_map_access( struct object *obj, unsigned int access )
  148. {
  149. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | MUTANT_QUERY_STATE;
  150. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
  151. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE;
  152. if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | MUTEX_ALL_ACCESS;
  153. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  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 void mutex_destroy( struct object *obj )
  173. {
  174. struct mutex *mutex = (struct mutex *)obj;
  175. assert( obj->ops == &mutex_ops );
  176. if (!mutex->count) return;
  177. mutex->count = 0;
  178. do_release( mutex );
  179. }
  180. /* create a mutex */
  181. DECL_HANDLER(create_mutex)
  182. {
  183. struct mutex *mutex;
  184. struct unicode_str name;
  185. struct object *root;
  186. const struct security_descriptor *sd;
  187. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  188. if (!objattr) return;
  189. if ((mutex = create_mutex( root, &name, objattr->attributes, req->owned, sd )))
  190. {
  191. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  192. reply->handle = alloc_handle( current->process, mutex, req->access, objattr->attributes );
  193. else
  194. reply->handle = alloc_handle_no_access_check( current->process, mutex,
  195. req->access, objattr->attributes );
  196. release_object( mutex );
  197. }
  198. if (root) release_object( root );
  199. }
  200. /* open a handle to a mutex */
  201. DECL_HANDLER(open_mutex)
  202. {
  203. struct unicode_str name = get_req_unicode_str();
  204. reply->handle = open_object( current->process, req->rootdir, req->access,
  205. &mutex_ops, &name, req->attributes );
  206. }
  207. /* release a mutex */
  208. DECL_HANDLER(release_mutex)
  209. {
  210. struct mutex *mutex;
  211. if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
  212. 0, &mutex_ops )))
  213. {
  214. if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED );
  215. else
  216. {
  217. reply->prev_count = mutex->count;
  218. if (!--mutex->count) do_release( mutex );
  219. }
  220. release_object( mutex );
  221. }
  222. }
  223. /* return details about the mutex */
  224. DECL_HANDLER(query_mutex)
  225. {
  226. struct mutex *mutex;
  227. if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle,
  228. MUTANT_QUERY_STATE, &mutex_ops )))
  229. {
  230. reply->count = mutex->count;
  231. reply->owned = (mutex->owner == current);
  232. reply->abandoned = mutex->abandoned;
  233. release_object( mutex );
  234. }
  235. }