semaphore.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Server-side semaphore 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. static const WCHAR semaphore_name[] = {'S','e','m','a','p','h','o','r','e'};
  35. struct type_descr semaphore_type =
  36. {
  37. { semaphore_name, sizeof(semaphore_name) }, /* name */
  38. SEMAPHORE_ALL_ACCESS, /* valid_access */
  39. { /* mapping */
  40. STANDARD_RIGHTS_READ | SEMAPHORE_QUERY_STATE,
  41. STANDARD_RIGHTS_WRITE | SEMAPHORE_MODIFY_STATE,
  42. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
  43. SEMAPHORE_ALL_ACCESS
  44. },
  45. };
  46. struct semaphore
  47. {
  48. struct object obj; /* object header */
  49. unsigned int count; /* current count */
  50. unsigned int max; /* maximum possible count */
  51. };
  52. static void semaphore_dump( struct object *obj, int verbose );
  53. static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry );
  54. static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry );
  55. static int semaphore_signal( struct object *obj, unsigned int access );
  56. static const struct object_ops semaphore_ops =
  57. {
  58. sizeof(struct semaphore), /* size */
  59. &semaphore_type, /* type */
  60. semaphore_dump, /* dump */
  61. add_queue, /* add_queue */
  62. remove_queue, /* remove_queue */
  63. semaphore_signaled, /* signaled */
  64. semaphore_satisfied, /* satisfied */
  65. semaphore_signal, /* signal */
  66. no_get_fd, /* get_fd */
  67. default_map_access, /* map_access */
  68. default_get_sd, /* get_sd */
  69. default_set_sd, /* set_sd */
  70. default_get_full_name, /* get_full_name */
  71. no_lookup_name, /* lookup_name */
  72. directory_link_name, /* link_name */
  73. default_unlink_name, /* unlink_name */
  74. no_open_file, /* open_file */
  75. no_kernel_obj_list, /* get_kernel_obj_list */
  76. no_close_handle, /* close_handle */
  77. no_destroy /* destroy */
  78. };
  79. static struct semaphore *create_semaphore( struct object *root, const struct unicode_str *name,
  80. unsigned int attr, unsigned int initial, unsigned int max,
  81. const struct security_descriptor *sd )
  82. {
  83. struct semaphore *sem;
  84. if (!max || (initial > max))
  85. {
  86. set_error( STATUS_INVALID_PARAMETER );
  87. return NULL;
  88. }
  89. if ((sem = create_named_object( root, &semaphore_ops, name, attr, sd )))
  90. {
  91. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  92. {
  93. /* initialize it if it didn't already exist */
  94. sem->count = initial;
  95. sem->max = max;
  96. }
  97. }
  98. return sem;
  99. }
  100. static int release_semaphore( struct semaphore *sem, unsigned int count,
  101. unsigned int *prev )
  102. {
  103. if (prev) *prev = sem->count;
  104. if (sem->count + count < sem->count || sem->count + count > sem->max)
  105. {
  106. set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
  107. return 0;
  108. }
  109. else if (sem->count)
  110. {
  111. /* there cannot be any thread to wake up if the count is != 0 */
  112. sem->count += count;
  113. }
  114. else
  115. {
  116. sem->count = count;
  117. wake_up( &sem->obj, count );
  118. }
  119. return 1;
  120. }
  121. static void semaphore_dump( struct object *obj, int verbose )
  122. {
  123. struct semaphore *sem = (struct semaphore *)obj;
  124. assert( obj->ops == &semaphore_ops );
  125. fprintf( stderr, "Semaphore count=%d max=%d\n", sem->count, sem->max );
  126. }
  127. static int semaphore_signaled( struct object *obj, struct wait_queue_entry *entry )
  128. {
  129. struct semaphore *sem = (struct semaphore *)obj;
  130. assert( obj->ops == &semaphore_ops );
  131. return (sem->count > 0);
  132. }
  133. static void semaphore_satisfied( struct object *obj, struct wait_queue_entry *entry )
  134. {
  135. struct semaphore *sem = (struct semaphore *)obj;
  136. assert( obj->ops == &semaphore_ops );
  137. assert( sem->count );
  138. sem->count--;
  139. }
  140. static int semaphore_signal( struct object *obj, unsigned int access )
  141. {
  142. struct semaphore *sem = (struct semaphore *)obj;
  143. assert( obj->ops == &semaphore_ops );
  144. if (!(access & SEMAPHORE_MODIFY_STATE))
  145. {
  146. set_error( STATUS_ACCESS_DENIED );
  147. return 0;
  148. }
  149. return release_semaphore( sem, 1, NULL );
  150. }
  151. /* create a semaphore */
  152. DECL_HANDLER(create_semaphore)
  153. {
  154. struct semaphore *sem;
  155. struct unicode_str name;
  156. struct object *root;
  157. const struct security_descriptor *sd;
  158. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  159. if (!objattr) return;
  160. if ((sem = create_semaphore( root, &name, objattr->attributes, req->initial, req->max, sd )))
  161. {
  162. if (get_error() == STATUS_OBJECT_NAME_EXISTS)
  163. reply->handle = alloc_handle( current->process, sem, req->access, objattr->attributes );
  164. else
  165. reply->handle = alloc_handle_no_access_check( current->process, sem,
  166. req->access, objattr->attributes );
  167. release_object( sem );
  168. }
  169. if (root) release_object( root );
  170. }
  171. /* open a handle to a semaphore */
  172. DECL_HANDLER(open_semaphore)
  173. {
  174. struct unicode_str name = get_req_unicode_str();
  175. reply->handle = open_object( current->process, req->rootdir, req->access,
  176. &semaphore_ops, &name, req->attributes );
  177. }
  178. /* release a semaphore */
  179. DECL_HANDLER(release_semaphore)
  180. {
  181. struct semaphore *sem;
  182. if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
  183. SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
  184. {
  185. release_semaphore( sem, req->count, &reply->prev_count );
  186. release_object( sem );
  187. }
  188. }
  189. /* query details about the semaphore */
  190. DECL_HANDLER(query_semaphore)
  191. {
  192. struct semaphore *sem;
  193. if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle,
  194. SEMAPHORE_QUERY_STATE, &semaphore_ops )))
  195. {
  196. reply->current = sem->count;
  197. reply->max = sem->max;
  198. release_object( sem );
  199. }
  200. }