posix-threads.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // -*- c++ -*-
  2. // posix-threads.h - Defines for using POSIX threads.
  3. /* Copyright (C) 1998, 1999, 2001, 2003, 2006 Free Software Foundation
  4. This file is part of libgcj.
  5. This software is copyrighted work licensed under the terms of the
  6. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  7. details. */
  8. #ifndef __JV_POSIX_THREADS__
  9. #define __JV_POSIX_THREADS__
  10. // NOTE: This file may only reference those pthread functions which
  11. // are known not to be overridden by the Boehm GC. If in doubt, scan
  12. // boehm-gc/gc.h. This is yucky but lets us avoid including gc.h
  13. // everywhere (which would be truly yucky).
  14. #include <pthread.h>
  15. #include <sched.h>
  16. #include <sysdep/locks.h>
  17. //
  18. // Typedefs.
  19. //
  20. typedef struct _Jv_Thread_t
  21. {
  22. // Flag values are defined in implementation.
  23. int flags;
  24. // Actual thread id.
  25. pthread_t thread;
  26. // Java Thread object.
  27. java::lang::Thread *thread_obj;
  28. // Condition variable and corresponding mutex, used to implement the
  29. // interruptable wait/notify mechanism.
  30. pthread_cond_t wait_cond;
  31. pthread_mutex_t wait_mutex;
  32. // Next thread for Condition Variable wait-list chain.
  33. _Jv_Thread_t *next;
  34. } _Jv_Thread_t;
  35. typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
  36. // Condition Variables used to implement wait/notify/sleep/interrupt.
  37. typedef struct
  38. {
  39. // Linked list of Threads that are waiting to be notified.
  40. _Jv_Thread_t *first;
  41. } _Jv_ConditionVariable_t;
  42. typedef struct
  43. {
  44. // For compatibility, simplicity, and correctness, we do not use the native
  45. // pthreads recursive mutex implementation, but simulate them instead.
  46. // Mutex the thread holds the entire time this mutex is held.
  47. pthread_mutex_t mutex;
  48. // Thread holding this mutex.
  49. pthread_t owner;
  50. // Number of times mutex is held (lock depth). If 0, the lock is not held.
  51. int count;
  52. } _Jv_Mutex_t;
  53. // This is a convenience function used only by the pthreads thread
  54. // implementation. This is slow, but that's too bad -- we need to do
  55. // the checks for correctness. It might be nice to be able to compile
  56. // this out. Returns 0 if the lock is held by the current thread, and
  57. // 1 otherwise.
  58. inline int
  59. _Jv_MutexCheckMonitor (_Jv_Mutex_t *mu)
  60. {
  61. return (pthread_equal(mu->owner, pthread_self()) == 0);
  62. }
  63. // Type identifying a POSIX thread.
  64. typedef pthread_t _Jv_ThreadDesc_t;
  65. inline _Jv_ThreadDesc_t
  66. _Jv_GetPlatformThreadID(_Jv_Thread_t *t)
  67. {
  68. return t->thread;
  69. }
  70. //
  71. // Signal helpers.
  72. //
  73. void _Jv_BlockSigchld();
  74. void _Jv_UnBlockSigchld();
  75. //
  76. // Condition variables.
  77. //
  78. int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
  79. jlong millis, jint nanos);
  80. int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu);
  81. int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu);
  82. inline void
  83. _Jv_CondInit (_Jv_ConditionVariable_t *cv)
  84. {
  85. cv->first = 0;
  86. }
  87. //
  88. // Mutexes.
  89. //
  90. #ifdef LOCK_DEBUG
  91. # include <stdio.h>
  92. #endif
  93. inline void
  94. _Jv_MutexInit (_Jv_Mutex_t *mu)
  95. {
  96. # ifdef LOCK_DEBUG /* Assumes Linuxthreads */
  97. pthread_mutexattr_t attr;
  98. pthread_mutexattr_init(&attr);
  99. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
  100. pthread_mutex_init (&mu->mutex, &attr);
  101. # else
  102. pthread_mutex_init (&mu->mutex, 0);
  103. # endif
  104. mu->count = 0;
  105. mu->owner = 0;
  106. }
  107. extern int _Jv_MutexLock (_Jv_Mutex_t *);
  108. inline int
  109. _Jv_MutexUnlock (_Jv_Mutex_t *mu)
  110. {
  111. if (_Jv_MutexCheckMonitor (mu))
  112. {
  113. # ifdef LOCK_DEBUG
  114. fprintf(stderr, "_Jv_MutexUnlock: Not owner\n");
  115. for (;;) {}
  116. # endif
  117. return 1;
  118. }
  119. mu->count--;
  120. if (mu->count == 0)
  121. {
  122. mu->owner = 0;
  123. # ifdef LOCK_DEBUG
  124. int result = pthread_mutex_unlock (&mu->mutex);
  125. if (0 != result)
  126. {
  127. fprintf(stderr, "Pthread_mutex_unlock returned %d\n", result);
  128. for (;;) {}
  129. }
  130. # else
  131. pthread_mutex_unlock (&mu->mutex);
  132. # endif
  133. }
  134. return 0;
  135. }
  136. #ifndef LINUX_THREADS
  137. // pthread_mutex_destroy does nothing on Linux and it is a win to avoid
  138. // defining this macro.
  139. #define _Jv_HaveMutexDestroy
  140. inline void
  141. _Jv_MutexDestroy (_Jv_Mutex_t *mu)
  142. {
  143. pthread_mutex_destroy (&mu->mutex);
  144. }
  145. #endif /* LINUX_THREADS */
  146. //
  147. // Thread creation and manipulation.
  148. //
  149. void _Jv_InitThreads (void);
  150. _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
  151. void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
  152. inline java::lang::Thread *
  153. _Jv_ThreadCurrent (void)
  154. {
  155. extern pthread_key_t _Jv_ThreadKey;
  156. return (java::lang::Thread *) pthread_getspecific (_Jv_ThreadKey);
  157. }
  158. #ifdef JV_HASH_SYNCHRONIZATION
  159. // Should be specialized to just load the "current thread" register
  160. // on platforms that support it. Speed is of the essence. The value
  161. // of the descriptor is not, so long as there is a one-to-one correspondence
  162. // to threads.
  163. #ifdef __ia64__
  164. typedef size_t _Jv_ThreadId_t;
  165. register size_t _Jv_self __asm__("r13");
  166. // For linux_threads this is really a pointer to its thread data
  167. // structure. We treat it as opaque. That should also work
  168. // on other operating systems that follow the ABI standard.
  169. // This should become the prototype for machines that maintain a thread
  170. // pointer in a register.
  171. inline _Jv_ThreadId_t
  172. _Jv_ThreadSelf (void)
  173. {
  174. return _Jv_self;
  175. }
  176. #define JV_SELF_DEFINED
  177. #endif /* __ia64__ */
  178. #ifdef __alpha__
  179. typedef void *_Jv_ThreadId_t;
  180. inline _Jv_ThreadId_t
  181. _Jv_ThreadSelf (void)
  182. {
  183. return __builtin_thread_pointer ();
  184. }
  185. #define JV_SELF_DEFINED
  186. #endif /* __alpha__ */
  187. #if defined(SLOW_PTHREAD_SELF)
  188. #include "sysdep/locks.h"
  189. typedef pthread_t _Jv_ThreadId_t;
  190. // E.g. on X86 Linux, pthread_self() is too slow for our purpose.
  191. // Instead we maintain a cache based on the current sp value.
  192. // This is similar to what's done for thread local allocation in the
  193. // GC, only far simpler.
  194. // This code should probably go away when Linux/X86 starts using a
  195. // segment register to hold the thread id.
  196. # define LOG_THREAD_SPACING 12
  197. // If two thread pointer values are closer than
  198. // 1 << LOG_THREAD_SPACING, we assume they belong
  199. // to the same thread.
  200. # define SELF_CACHE_SIZE 1024
  201. # define SC_INDEX(sp) (((unsigned long)(sp) >> 19) & (SELF_CACHE_SIZE-1))
  202. // Mapping from sp value to cache index.
  203. // Note that this is not in any real sense a hash
  204. // function, since we need to be able to clear
  205. // all possibly matching slots on thread startup.
  206. // Thus all entries that might correspond to
  207. // a given thread are intentionally contiguous.
  208. // Works well with anything that allocates at least
  209. // 512KB stacks.
  210. # define SC_CLEAR_MIN (-16) // When starting a new thread, we clear
  211. # define SC_CLEAR_MAX 0 // all self cache entries between
  212. // SC_INDEX(sp)+SC_CLEAR_MIN and
  213. // SC_INDEX(sp)+SC_CLEAR_MAX to ensure
  214. // we never see stale values. The
  215. // current values assume a downward
  216. // growing stack of size <= 7.5 MB.
  217. # define BAD_HIGH_SP_VALUE ((size_t)(-1))
  218. extern volatile
  219. struct self_cache_entry {
  220. size_t high_sp_bits; // sp value >> LOG_THREAD_SPACING
  221. pthread_t self; // Corresponding thread
  222. } _Jv_self_cache[];
  223. void _Jv_Self_Cache_Init();
  224. _Jv_ThreadId_t
  225. _Jv_ThreadSelf_out_of_line(volatile self_cache_entry *sce,
  226. size_t high_sp_bits);
  227. inline _Jv_ThreadId_t
  228. _Jv_ThreadSelf (void)
  229. {
  230. int dummy;
  231. size_t sp = (size_t)(&dummy);
  232. unsigned h = SC_INDEX(sp);
  233. volatile self_cache_entry *sce = _Jv_self_cache + h;
  234. pthread_t candidate_self = sce -> self; // Read must precede following one.
  235. read_barrier();
  236. if (sce -> high_sp_bits == sp >> LOG_THREAD_SPACING)
  237. {
  238. // The sce -> self value we read must be valid. An intervening
  239. // cache replacement by another thread would have first replaced
  240. // high_sp_bits by something else, and it can't possibly change
  241. // back without our intervention.
  242. return candidate_self;
  243. }
  244. else
  245. return _Jv_ThreadSelf_out_of_line(sce, sp >> LOG_THREAD_SPACING);
  246. }
  247. #define JV_SELF_DEFINED
  248. #endif /* SLOW_PTHREAD_SELF */
  249. #ifndef JV_SELF_DEFINED /* If all else fails, call pthread_self directly */
  250. typedef pthread_t _Jv_ThreadId_t;
  251. inline _Jv_ThreadId_t
  252. _Jv_ThreadSelf (void)
  253. {
  254. return pthread_self();
  255. }
  256. #endif /* !JV_SELF_DEFINED */
  257. #endif /* JV_HASH_SYNCHRONIZATION */
  258. inline _Jv_Thread_t *
  259. _Jv_ThreadCurrentData (void)
  260. {
  261. extern pthread_key_t _Jv_ThreadDataKey;
  262. return (_Jv_Thread_t *) pthread_getspecific (_Jv_ThreadDataKey);
  263. }
  264. inline void
  265. _Jv_ThreadYield (void)
  266. {
  267. #ifdef HAVE_SCHED_YIELD
  268. sched_yield ();
  269. #endif /* HAVE_SCHED_YIELD */
  270. }
  271. void _Jv_ThreadRegister (_Jv_Thread_t *data);
  272. void _Jv_ThreadUnRegister ();
  273. void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
  274. void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
  275. _Jv_ThreadStartFunc *meth);
  276. void _Jv_ThreadWait (void);
  277. void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
  278. // park() / unpark() support
  279. struct ParkHelper
  280. {
  281. volatile obj_addr_t permit;
  282. pthread_mutex_t mutex;
  283. pthread_cond_t cond;
  284. void init ();
  285. void deactivate ();
  286. void destroy ();
  287. void park (jboolean isAbsolute, jlong time);
  288. void unpark ();
  289. };
  290. inline void
  291. ParkHelper::destroy ()
  292. {
  293. pthread_mutex_destroy (&mutex);
  294. pthread_cond_destroy (&cond);
  295. }
  296. #endif /* __JV_POSIX_THREADS__ */