gp_wsync.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (C) 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gp_wsync.c,v 1.2 2000/09/19 19:00:25 lpd Exp $ */
  16. /* MS Windows (Win32) thread / semaphore / monitor implementation */
  17. /* original multi-threading code by John Desrosiers */
  18. #include "malloc_.h"
  19. #include "gserror.h"
  20. #include "gserrors.h"
  21. #include "gpsync.h"
  22. #include "windows_.h"
  23. #include <process.h>
  24. /* ------- Synchronization primitives -------- */
  25. /* Semaphore supports wait/signal semantics */
  26. typedef struct win32_semaphore_s {
  27. HANDLE handle; /* returned from CreateSemaphore */
  28. } win32_semaphore;
  29. uint
  30. gp_semaphore_sizeof(void)
  31. {
  32. return sizeof(win32_semaphore);
  33. }
  34. int /* if sema <> 0 rets -ve error, 0 ok; if sema == 0, 0 movable, 1 fixed */
  35. gp_semaphore_open(
  36. gp_semaphore * sema /* create semaphore here */
  37. )
  38. {
  39. win32_semaphore *const winSema = (win32_semaphore *)sema;
  40. if (winSema) {
  41. winSema->handle = CreateSemaphore(NULL, 0, max_int, NULL);
  42. return
  43. (winSema->handle != NULL ? 0 :
  44. gs_note_error(gs_error_unknownerror));
  45. } else
  46. return 0; /* Win32 semaphores handles may be moved */
  47. }
  48. int
  49. gp_semaphore_close(
  50. gp_semaphore * sema /* semaphore to affect */
  51. )
  52. {
  53. win32_semaphore *const winSema = (win32_semaphore *)sema;
  54. if (winSema->handle != NULL)
  55. CloseHandle(winSema->handle);
  56. winSema->handle = NULL;
  57. return 0;
  58. }
  59. int /* rets 0 ok, -ve error */
  60. gp_semaphore_wait(
  61. gp_semaphore * sema /* semaphore to affect */
  62. )
  63. {
  64. win32_semaphore *const winSema = (win32_semaphore *)sema;
  65. return
  66. (WaitForSingleObject(winSema->handle, INFINITE) == WAIT_OBJECT_0
  67. ? 0 : gs_error_unknownerror);
  68. }
  69. int /* rets 0 ok, -ve error */
  70. gp_semaphore_signal(
  71. gp_semaphore * sema /* semaphore to affect */
  72. )
  73. {
  74. win32_semaphore *const winSema = (win32_semaphore *)sema;
  75. return
  76. (ReleaseSemaphore(winSema->handle, 1, NULL) ? 0 :
  77. gs_error_unknownerror);
  78. }
  79. /* Monitor supports enter/leave semantics */
  80. typedef struct win32_monitor_s {
  81. CRITICAL_SECTION lock; /* critical section lock */
  82. } win32_monitor;
  83. uint
  84. gp_monitor_sizeof(void)
  85. {
  86. return sizeof(win32_monitor);
  87. }
  88. int /* if sema <> 0 rets -ve error, 0 ok; if sema == 0, 0 movable, 1 fixed */
  89. gp_monitor_open(
  90. gp_monitor * mon /* create monitor here */
  91. )
  92. {
  93. win32_monitor *const winMon = (win32_monitor *)mon;
  94. if (mon) {
  95. InitializeCriticalSection(&winMon->lock); /* returns no status */
  96. return 0;
  97. } else
  98. return 1; /* Win32 critical sections mutsn't be moved */
  99. }
  100. int
  101. gp_monitor_close(
  102. gp_monitor * mon /* monitor to affect */
  103. )
  104. {
  105. win32_monitor *const winMon = (win32_monitor *)mon;
  106. DeleteCriticalSection(&winMon->lock); /* rets no status */
  107. return 0;
  108. }
  109. int /* rets 0 ok, -ve error */
  110. gp_monitor_enter(
  111. gp_monitor * mon /* monitor to affect */
  112. )
  113. {
  114. win32_monitor *const winMon = (win32_monitor *)mon;
  115. EnterCriticalSection(&winMon->lock); /* rets no status */
  116. return 0;
  117. }
  118. int /* rets 0 ok, -ve error */
  119. gp_monitor_leave(
  120. gp_monitor * mon /* monitor to affect */
  121. )
  122. {
  123. win32_monitor *const winMon = (win32_monitor *)mon;
  124. LeaveCriticalSection(&winMon->lock); /* rets no status */
  125. return 0;
  126. }
  127. /* --------- Thread primitives ---------- */
  128. typedef struct gp_thread_creation_closure_s {
  129. gp_thread_creation_callback_t function; /* function to start */
  130. void *data; /* magic data to pass to thread */
  131. } gp_thread_creation_closure;
  132. /* Origin of new threads started by gp_create_thread */
  133. private void
  134. gp_thread_begin_wrapper(
  135. void *thread_data /* gp_thread_creation_closure passed as magic data */
  136. )
  137. {
  138. gp_thread_creation_closure closure;
  139. closure = *(gp_thread_creation_closure *)thread_data;
  140. free(thread_data);
  141. (*closure.function)(closure.data);
  142. _endthread();
  143. }
  144. /* Call a function on a brand new thread */
  145. int /* 0 ok, -ve error */
  146. gp_create_thread(
  147. gp_thread_creation_callback_t function, /* function to start */
  148. void *data /* magic data to pass to thread fn */
  149. )
  150. {
  151. /* Create the magic closure that thread_wrapper gets passed */
  152. gp_thread_creation_closure *closure =
  153. (gp_thread_creation_closure *)malloc(sizeof(*closure));
  154. if (!closure)
  155. return gs_error_VMerror;
  156. closure->function = function;
  157. closure->data = data;
  158. /*
  159. * Start thread_wrapper. The Watcom _beginthread returns (int)(-1) if
  160. * the call fails. The Microsoft _beginthread returns -1 (according to
  161. * the doc, even though the return type is "unsigned long" !!!) if the
  162. * call fails; we aren't sure what the Borland _beginthread returns.
  163. * The hack with ~ avoids a source code commitment as to whether the
  164. * return type is [u]int or [u]long.
  165. *
  166. * BEGIN_THREAD is a macro (defined in windows_.h) because _beginthread
  167. * takes different arguments in Watcom C.
  168. */
  169. if (~BEGIN_THREAD(gp_thread_begin_wrapper, 0, closure) != 0)
  170. return 0;
  171. return_error(gs_error_unknownerror);
  172. }