semaphore.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "windef.h"
  26. #include "handle.h"
  27. #include "thread.h"
  28. #include "request.h"
  29. struct semaphore
  30. {
  31. struct object obj; /* object header */
  32. unsigned int count; /* current count */
  33. unsigned int max; /* maximum possible count */
  34. };
  35. static void semaphore_dump( struct object *obj, int verbose );
  36. static int semaphore_signaled( struct object *obj, struct thread *thread );
  37. static int semaphore_satisfied( struct object *obj, struct thread *thread );
  38. static const struct object_ops semaphore_ops =
  39. {
  40. sizeof(struct semaphore), /* size */
  41. semaphore_dump, /* dump */
  42. add_queue, /* add_queue */
  43. remove_queue, /* remove_queue */
  44. semaphore_signaled, /* signaled */
  45. semaphore_satisfied, /* satisfied */
  46. no_get_fd, /* get_fd */
  47. no_destroy /* destroy */
  48. };
  49. static struct semaphore *create_semaphore( const WCHAR *name, size_t len,
  50. unsigned int initial, unsigned int max )
  51. {
  52. struct semaphore *sem;
  53. if (!max || (initial > max))
  54. {
  55. set_error( STATUS_INVALID_PARAMETER );
  56. return NULL;
  57. }
  58. if ((sem = create_named_object( sync_namespace, &semaphore_ops, name, len )))
  59. {
  60. if (get_error() != STATUS_OBJECT_NAME_COLLISION)
  61. {
  62. /* initialize it if it didn't already exist */
  63. sem->count = initial;
  64. sem->max = max;
  65. }
  66. }
  67. return sem;
  68. }
  69. static unsigned int release_semaphore( obj_handle_t handle, unsigned int count )
  70. {
  71. struct semaphore *sem;
  72. unsigned int prev = 0;
  73. if ((sem = (struct semaphore *)get_handle_obj( current->process, handle,
  74. SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
  75. {
  76. prev = sem->count;
  77. if (sem->count + count < sem->count || sem->count + count > sem->max)
  78. {
  79. set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
  80. }
  81. else if (sem->count)
  82. {
  83. /* there cannot be any thread to wake up if the count is != 0 */
  84. sem->count += count;
  85. }
  86. else
  87. {
  88. sem->count = count;
  89. wake_up( &sem->obj, count );
  90. }
  91. release_object( sem );
  92. }
  93. return prev;
  94. }
  95. static void semaphore_dump( struct object *obj, int verbose )
  96. {
  97. struct semaphore *sem = (struct semaphore *)obj;
  98. assert( obj->ops == &semaphore_ops );
  99. fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
  100. dump_object_name( &sem->obj );
  101. fputc( '\n', stderr );
  102. }
  103. static int semaphore_signaled( struct object *obj, struct thread *thread )
  104. {
  105. struct semaphore *sem = (struct semaphore *)obj;
  106. assert( obj->ops == &semaphore_ops );
  107. return (sem->count > 0);
  108. }
  109. static int semaphore_satisfied( struct object *obj, struct thread *thread )
  110. {
  111. struct semaphore *sem = (struct semaphore *)obj;
  112. assert( obj->ops == &semaphore_ops );
  113. assert( sem->count );
  114. sem->count--;
  115. return 0; /* not abandoned */
  116. }
  117. /* create a semaphore */
  118. DECL_HANDLER(create_semaphore)
  119. {
  120. struct semaphore *sem;
  121. reply->handle = 0;
  122. if ((sem = create_semaphore( get_req_data(), get_req_data_size(),
  123. req->initial, req->max )))
  124. {
  125. reply->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit );
  126. release_object( sem );
  127. }
  128. }
  129. /* open a handle to a semaphore */
  130. DECL_HANDLER(open_semaphore)
  131. {
  132. reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
  133. &semaphore_ops, req->access, req->inherit );
  134. }
  135. /* release a semaphore */
  136. DECL_HANDLER(release_semaphore)
  137. {
  138. reply->prev_count = release_semaphore( req->handle, req->count );
  139. }