win32-threads.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // -*- c++ -*-
  2. // win32-threads.h - Defines for using Win32 threads.
  3. /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2006 Free Software
  4. Foundation
  5. This file is part of libgcj.
  6. This software is copyrighted work licensed under the terms of the
  7. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  8. details. */
  9. #ifndef __JV_WIN32_THREADS__
  10. #define __JV_WIN32_THREADS__
  11. #define WIN32_LEAN_AND_MEAN
  12. #include <windows.h>
  13. //
  14. // Typedefs.
  15. //
  16. typedef struct
  17. {
  18. // ev[0] (signal) is a Win32 auto-reset event for _Jv_CondNotify
  19. // ev[1] (broadcast) is a Win32 manual-reset event for _Jv_CondNotifyAll
  20. HANDLE ev[2];
  21. // Number of threads waiting on this condition variable
  22. int blocked_count;
  23. // Protects access to the blocked_count variable
  24. CRITICAL_SECTION count_mutex;
  25. } _Jv_ConditionVariable_t;
  26. typedef struct
  27. {
  28. // The thread-id of the owner thread if any, 0 otherwise
  29. DWORD owner;
  30. // Track nested mutex acquisitions by the same thread
  31. int refcount;
  32. // The actual Windows construct used to implement this mutex
  33. CRITICAL_SECTION cs;
  34. } _Jv_Mutex_t;
  35. typedef struct _Jv_Thread_t
  36. {
  37. int flags; // Flags are defined in implementation.
  38. HANDLE handle; // Actual handle to the thread
  39. // Protects access to the thread's interrupt_flag and
  40. // interrupt_event variables within this module.
  41. CRITICAL_SECTION interrupt_mutex;
  42. // A Win32 auto-reset event for thread interruption
  43. HANDLE interrupt_event;
  44. java::lang::Thread *thread_obj;
  45. } _Jv_Thread_t;
  46. typedef DWORD _Jv_ThreadId_t;
  47. inline _Jv_ThreadId_t
  48. _Jv_ThreadSelf (void)
  49. {
  50. return GetCurrentThreadId();
  51. }
  52. typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
  53. // Type identifying a win32 thread.
  54. typedef HANDLE _Jv_ThreadDesc_t;
  55. inline _Jv_ThreadDesc_t
  56. _Jv_GetPlatformThreadID(_Jv_Thread_t *t)
  57. {
  58. return t->handle;
  59. }
  60. //
  61. // Condition variables.
  62. //
  63. #define _Jv_HaveCondDestroy
  64. int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos);
  65. void _Jv_CondInit (_Jv_ConditionVariable_t *cv);
  66. void _Jv_CondDestroy (_Jv_ConditionVariable_t *cv);
  67. int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
  68. int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
  69. //
  70. // Mutexes.
  71. // We use CRITICAL_SECTIONs instead of CreateMutex() for better performance
  72. //
  73. // Returns 0 if the mutex lock is held by the current thread, and 1 otherwise.
  74. inline int _Jv_MutexCheckMonitor (_Jv_Mutex_t *mu)
  75. {
  76. return (mu->owner != GetCurrentThreadId ( ));
  77. }
  78. inline void _Jv_MutexInit (_Jv_Mutex_t *mu)
  79. {
  80. mu->owner = 0UL;
  81. mu->refcount = 0;
  82. InitializeCriticalSection (&(mu->cs));
  83. }
  84. #define _Jv_HaveMutexDestroy
  85. inline void _Jv_MutexDestroy (_Jv_Mutex_t *mu)
  86. {
  87. mu->owner = 0UL;
  88. mu->refcount = 0;
  89. DeleteCriticalSection (&(mu->cs));
  90. mu = NULL;
  91. }
  92. inline int _Jv_MutexUnlock (_Jv_Mutex_t *mu)
  93. {
  94. if (mu->owner == GetCurrentThreadId ( ))
  95. {
  96. mu->refcount--;
  97. if (mu->refcount == 0)
  98. mu->owner = 0UL;
  99. LeaveCriticalSection (&(mu->cs));
  100. return 0;
  101. }
  102. else
  103. return 1;
  104. }
  105. inline int _Jv_MutexLock (_Jv_Mutex_t *mu)
  106. {
  107. EnterCriticalSection (&(mu->cs));
  108. mu->owner = GetCurrentThreadId ( );
  109. mu->refcount++;
  110. return 0;
  111. }
  112. //
  113. // Thread creation and manipulation.
  114. //
  115. void _Jv_InitThreads (void);
  116. _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
  117. void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
  118. inline java::lang::Thread* _Jv_ThreadCurrent (void)
  119. {
  120. extern DWORD _Jv_ThreadKey;
  121. return (java::lang::Thread *) TlsGetValue(_Jv_ThreadKey);
  122. }
  123. inline _Jv_Thread_t *_Jv_ThreadCurrentData(void)
  124. {
  125. extern DWORD _Jv_ThreadDataKey;
  126. return (_Jv_Thread_t *) TlsGetValue(_Jv_ThreadDataKey);
  127. }
  128. inline void _Jv_ThreadYield (void)
  129. {
  130. Sleep (0);
  131. }
  132. void _Jv_ThreadRegister (_Jv_Thread_t *data);
  133. void _Jv_ThreadUnRegister ();
  134. void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
  135. void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
  136. _Jv_ThreadStartFunc *meth);
  137. void _Jv_ThreadWait (void);
  138. void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
  139. //
  140. // Thread interruption support
  141. //
  142. // Gets the auto-reset event for the current thread which is
  143. // signalled by _Jv_ThreadInterrupt. The caller can wait on this
  144. // event in addition to other waitable objects.
  145. //
  146. // NOTE: After waiting on this event with WaitForMultipleObjects,
  147. // you should ALWAYS use the return value of WaitForMultipleObjects
  148. // to test whether this event was signalled and whether thread
  149. // interruption has occurred. You should do this instead of checking
  150. // the thread's interrupted_flag, because someone could have reset
  151. // this flag in the interval of time between the return of
  152. // WaitForMultipleObjects and the time you query interrupted_flag.
  153. // See java/lang/natWin32Process.cc (waitFor) for an example.
  154. HANDLE _Jv_Win32GetInterruptEvent (void);
  155. // park() / unpark() support
  156. struct ParkHelper
  157. {
  158. // We use LONG instead of obj_addr_t to avoid pulling in locks.h,
  159. // which depends on size_t, ...
  160. volatile LONG permit;
  161. // The critical section is used for lazy initialization of our event
  162. CRITICAL_SECTION cs;
  163. HANDLE event;
  164. void init ();
  165. void deactivate ();
  166. void destroy ();
  167. void park (jboolean isAbsolute, jlong time);
  168. void unpark ();
  169. private:
  170. void init_event();
  171. };
  172. // Remove defines from <windows.h> that conflict with various things in libgcj code
  173. #undef TRUE
  174. #undef FALSE
  175. #undef MAX_PRIORITY
  176. #undef MIN_PRIORITY
  177. #undef min
  178. #undef max
  179. #undef interface
  180. #undef STRICT
  181. #undef VOID
  182. #undef TEXT
  183. #undef OUT
  184. #endif /* __JV_WIN32_THREADS__ */