Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.6 KiB

  1. #ifndef _WIN32THREAD_H
  2. #define _WIN32THREAD_H
  3. #include "win32.h"
  4. typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
  5. typedef DWORD perl_key;
  6. typedef HANDLE perl_os_thread;
  7. #ifndef DONT_USE_CRITICAL_SECTION
  8. /* Critical Sections used instead of mutexes: lightweight,
  9. * but can't be communicated to child processes, and can't get
  10. * HANDLE to it for use elsewhere.
  11. */
  12. typedef CRITICAL_SECTION perl_mutex;
  13. #define MUTEX_INIT(m) InitializeCriticalSection(m)
  14. #define MUTEX_LOCK(m) EnterCriticalSection(m)
  15. #define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
  16. #define MUTEX_DESTROY(m) DeleteCriticalSection(m)
  17. #else
  18. typedef HANDLE perl_mutex;
  19. # define MUTEX_INIT(m) \
  20. STMT_START { \
  21. if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
  22. Perl_croak_nocontext("panic: MUTEX_INIT"); \
  23. } STMT_END
  24. # define MUTEX_LOCK(m) \
  25. STMT_START { \
  26. if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
  27. Perl_croak_nocontext("panic: MUTEX_LOCK"); \
  28. } STMT_END
  29. # define MUTEX_UNLOCK(m) \
  30. STMT_START { \
  31. if (ReleaseMutex(*(m)) == 0) \
  32. Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
  33. } STMT_END
  34. # define MUTEX_DESTROY(m) \
  35. STMT_START { \
  36. if (CloseHandle(*(m)) == 0) \
  37. Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
  38. } STMT_END
  39. #endif
  40. /* These macros assume that the mutex associated with the condition
  41. * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
  42. * so there's no separate mutex protecting access to (c)->waiters
  43. */
  44. #define COND_INIT(c) \
  45. STMT_START { \
  46. (c)->waiters = 0; \
  47. (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
  48. if ((c)->sem == NULL) \
  49. Perl_croak_nocontext("panic: COND_INIT (%ld)",GetLastError()); \
  50. } STMT_END
  51. #define COND_SIGNAL(c) \
  52. STMT_START { \
  53. if ((c)->waiters > 0 && \
  54. ReleaseSemaphore((c)->sem,1,NULL) == 0) \
  55. Perl_croak_nocontext("panic: COND_SIGNAL (%ld)",GetLastError()); \
  56. } STMT_END
  57. #define COND_BROADCAST(c) \
  58. STMT_START { \
  59. if ((c)->waiters > 0 && \
  60. ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
  61. Perl_croak_nocontext("panic: COND_BROADCAST (%ld)",GetLastError());\
  62. } STMT_END
  63. #define COND_WAIT(c, m) \
  64. STMT_START { \
  65. (c)->waiters++; \
  66. MUTEX_UNLOCK(m); \
  67. /* Note that there's no race here, since a \
  68. * COND_BROADCAST() on another thread will have seen the\
  69. * right number of waiters (i.e. including this one) */ \
  70. if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
  71. Perl_croak_nocontext("panic: COND_WAIT (%ld)",GetLastError()); \
  72. /* XXX there may be an inconsequential race here */ \
  73. MUTEX_LOCK(m); \
  74. (c)->waiters--; \
  75. } STMT_END
  76. #define COND_DESTROY(c) \
  77. STMT_START { \
  78. (c)->waiters = 0; \
  79. if (CloseHandle((c)->sem) == 0) \
  80. Perl_croak_nocontext("panic: COND_DESTROY (%ld)",GetLastError()); \
  81. } STMT_END
  82. #define DETACH(t) \
  83. STMT_START { \
  84. if (CloseHandle((t)->self) == 0) { \
  85. MUTEX_UNLOCK(&(t)->mutex); \
  86. Perl_croak_nocontext("panic: DETACH"); \
  87. } \
  88. } STMT_END
  89. #define THREAD_CREATE(t, f) Perl_thread_create(t, f)
  90. #define THREAD_POST_CREATE(t) NOOP
  91. /* XXX Docs mention that the RTL versions of thread creation routines
  92. * should be used, but that advice only seems applicable when the RTL
  93. * is not in a DLL. RTL DLLs in both Borland and VC seem to do all of
  94. * the init/deinit required upon DLL_THREAD_ATTACH/DETACH. So we seem
  95. * to be completely safe using straight Win32 API calls, rather than
  96. * the much braindamaged RTL calls.
  97. *
  98. * _beginthread() in the RTLs call CloseHandle() just after the thread
  99. * function returns, which means: 1) we have a race on our hands
  100. * 2) it is impossible to implement join() semantics.
  101. *
  102. * IOW, do *NOT* turn on USE_RTL_THREAD_API! It is here
  103. * for experimental purposes only. GSAR 98-01-02
  104. */
  105. #ifdef USE_RTL_THREAD_API
  106. # include <process.h>
  107. # if defined(__BORLANDC__)
  108. /* Borland RTL doesn't allow a return value from thread function! */
  109. # define THREAD_RET_TYPE void _USERENTRY
  110. # define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
  111. # elif defined (_MSC_VER)
  112. # define THREAD_RET_TYPE unsigned __stdcall
  113. # define THREAD_RET_CAST(p) ((unsigned)(p))
  114. # else
  115. /* CRTDLL.DLL doesn't allow a return value from thread function! */
  116. # define THREAD_RET_TYPE void __cdecl
  117. # define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
  118. # endif
  119. #else /* !USE_RTL_THREAD_API */
  120. # define THREAD_RET_TYPE DWORD WINAPI
  121. # define THREAD_RET_CAST(p) ((DWORD)(p))
  122. #endif /* !USE_RTL_THREAD_API */
  123. typedef THREAD_RET_TYPE thread_func_t(void *);
  124. START_EXTERN_C
  125. #if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL))
  126. extern __declspec(thread) void *PL_current_context;
  127. #define PERL_SET_CONTEXT(t) (PL_current_context = t)
  128. #define PERL_GET_CONTEXT PL_current_context
  129. #else
  130. #define PERL_GET_CONTEXT Perl_get_context()
  131. #define PERL_SET_CONTEXT(t) Perl_set_context(t)
  132. #endif
  133. #if defined(USE_THREADS)
  134. struct perl_thread;
  135. int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn);
  136. void Perl_set_thread_self (struct perl_thread *thr);
  137. void Perl_init_thread_intern (struct perl_thread *t);
  138. #define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
  139. #endif /* USE_THREADS */
  140. END_EXTERN_C
  141. #define INIT_THREADS NOOP
  142. #define ALLOC_THREAD_KEY \
  143. STMT_START { \
  144. if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { \
  145. fprintf(stderr,"panic: TlsAlloc"); \
  146. exit(1); \
  147. } \
  148. } STMT_END
  149. #define FREE_THREAD_KEY \
  150. STMT_START { \
  151. TlsFree(PL_thr_key); \
  152. } STMT_END
  153. #define PTHREAD_ATFORK(prepare,parent,child) NOOP
  154. #if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
  155. #define JOIN(t, avp) \
  156. STMT_START { \
  157. if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
  158. || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
  159. || (CloseHandle((t)->self) == 0)) \
  160. Perl_croak_nocontext("panic: JOIN"); \
  161. *avp = (AV *)((t)->i.retv); \
  162. } STMT_END
  163. #else /* !USE_RTL_THREAD_API || _MSC_VER */
  164. #define JOIN(t, avp) \
  165. STMT_START { \
  166. if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
  167. || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
  168. || (CloseHandle((t)->self) == 0)) \
  169. Perl_croak_nocontext("panic: JOIN"); \
  170. } STMT_END
  171. #endif /* !USE_RTL_THREAD_API || _MSC_VER */
  172. #define YIELD Sleep(0)
  173. #endif /* _WIN32THREAD_H */