posix-threads.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. // posix-threads.cc - interface between libjava and POSIX threads.
  2. /* Copyright (C) 1998, 1999, 2000, 2001, 2004, 2006 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. // TO DO:
  8. // * Document signal handling limitations
  9. #include <config.h>
  10. #include "posix.h"
  11. #include "posix-threads.h"
  12. // If we're using the Boehm GC, then we need to override some of the
  13. // thread primitives. This is fairly gross.
  14. #ifdef HAVE_BOEHM_GC
  15. #include <gc.h>
  16. #endif /* HAVE_BOEHM_GC */
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <signal.h>
  20. #include <errno.h>
  21. #include <limits.h>
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
  24. #endif
  25. #include <gcj/cni.h>
  26. #include <jvm.h>
  27. #include <java/lang/Thread.h>
  28. #include <java/lang/System.h>
  29. #include <java/lang/Long.h>
  30. #include <java/lang/OutOfMemoryError.h>
  31. #include <java/lang/InternalError.h>
  32. // This is used to implement thread startup.
  33. struct starter
  34. {
  35. _Jv_ThreadStartFunc *method;
  36. _Jv_Thread_t *data;
  37. };
  38. // This is the key used to map from the POSIX thread value back to the
  39. // Java object representing the thread. The key is global to all
  40. // threads, so it is ok to make it a global here.
  41. pthread_key_t _Jv_ThreadKey;
  42. // This is the key used to map from the POSIX thread value back to the
  43. // _Jv_Thread_t* representing the thread.
  44. pthread_key_t _Jv_ThreadDataKey;
  45. // We keep a count of all non-daemon threads which are running. When
  46. // this reaches zero, _Jv_ThreadWait returns.
  47. static pthread_mutex_t daemon_mutex;
  48. static pthread_cond_t daemon_cond;
  49. static int non_daemon_count;
  50. // The signal to use when interrupting a thread.
  51. #if defined(LINUX_THREADS) || defined(FREEBSD_THREADS)
  52. // LinuxThreads (prior to glibc 2.1) usurps both SIGUSR1 and SIGUSR2.
  53. // GC on FreeBSD uses both SIGUSR1 and SIGUSR2.
  54. # define INTR SIGHUP
  55. #else /* LINUX_THREADS */
  56. # define INTR SIGUSR2
  57. #endif /* LINUX_THREADS */
  58. //
  59. // These are the flags that can appear in _Jv_Thread_t.
  60. //
  61. // Thread started.
  62. #define FLAG_START 0x01
  63. // Thread is daemon.
  64. #define FLAG_DAEMON 0x02
  65. int
  66. _Jv_MutexLock (_Jv_Mutex_t *mu)
  67. {
  68. pthread_t self = pthread_self ();
  69. if (mu->owner == self)
  70. {
  71. mu->count++;
  72. }
  73. else
  74. {
  75. JvSetThreadState holder (_Jv_ThreadCurrent(), JV_BLOCKED);
  76. # ifdef LOCK_DEBUG
  77. int result = pthread_mutex_lock (&mu->mutex);
  78. if (0 != result)
  79. {
  80. fprintf(stderr, "Pthread_mutex_lock returned %d\n", result);
  81. for (;;) {}
  82. }
  83. # else
  84. pthread_mutex_lock (&mu->mutex);
  85. # endif
  86. mu->count = 1;
  87. mu->owner = self;
  88. }
  89. return 0;
  90. }
  91. // Wait for the condition variable "CV" to be notified.
  92. // Return values:
  93. // 0: the condition was notified, or the timeout expired.
  94. // _JV_NOT_OWNER: the thread does not own the mutex "MU".
  95. // _JV_INTERRUPTED: the thread was interrupted. Its interrupted flag is set.
  96. int
  97. _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
  98. jlong millis, jint nanos)
  99. {
  100. pthread_t self = pthread_self();
  101. if (mu->owner != self)
  102. return _JV_NOT_OWNER;
  103. struct timespec ts;
  104. JvThreadState new_state = JV_WAITING;
  105. if (millis > 0 || nanos > 0)
  106. {
  107. // Calculate the abstime corresponding to the timeout.
  108. unsigned long long seconds;
  109. unsigned long usec;
  110. // For better accuracy, should use pthread_condattr_setclock
  111. // and clock_gettime.
  112. #ifdef HAVE_GETTIMEOFDAY
  113. timeval tv;
  114. gettimeofday (&tv, NULL);
  115. usec = tv.tv_usec;
  116. seconds = tv.tv_sec;
  117. #else
  118. unsigned long long startTime = java::lang::System::currentTimeMillis();
  119. seconds = startTime / 1000;
  120. /* Assume we're about half-way through this millisecond. */
  121. usec = (startTime % 1000) * 1000 + 500;
  122. #endif
  123. /* These next two statements cannot overflow. */
  124. usec += nanos / 1000;
  125. usec += (millis % 1000) * 1000;
  126. /* These two statements could overflow only if tv.tv_sec was
  127. insanely large. */
  128. seconds += millis / 1000;
  129. seconds += usec / 1000000;
  130. ts.tv_sec = seconds;
  131. if (ts.tv_sec < 0 || (unsigned long long)ts.tv_sec != seconds)
  132. {
  133. // We treat a timeout that won't fit into a struct timespec
  134. // as a wait forever.
  135. millis = nanos = 0;
  136. }
  137. else
  138. /* This next statement also cannot overflow. */
  139. ts.tv_nsec = (usec % 1000000) * 1000 + (nanos % 1000);
  140. }
  141. _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
  142. java::lang::Thread *current_obj = _Jv_ThreadCurrent ();
  143. pthread_mutex_lock (&current->wait_mutex);
  144. // Now that we hold the wait mutex, check if this thread has been
  145. // interrupted already.
  146. if (current_obj->interrupt_flag)
  147. {
  148. pthread_mutex_unlock (&current->wait_mutex);
  149. return _JV_INTERRUPTED;
  150. }
  151. // Set the thread's state.
  152. JvSetThreadState holder (current_obj, new_state);
  153. // Add this thread to the cv's wait set.
  154. current->next = NULL;
  155. if (cv->first == NULL)
  156. cv->first = current;
  157. else
  158. for (_Jv_Thread_t *t = cv->first;; t = t->next)
  159. {
  160. if (t->next == NULL)
  161. {
  162. t->next = current;
  163. break;
  164. }
  165. }
  166. // Record the current lock depth, so it can be restored when we re-aquire it.
  167. int count = mu->count;
  168. // Release the monitor mutex.
  169. mu->count = 0;
  170. mu->owner = 0;
  171. pthread_mutex_unlock (&mu->mutex);
  172. int r = 0;
  173. bool done_sleeping = false;
  174. while (! done_sleeping)
  175. {
  176. if (millis == 0 && nanos == 0)
  177. r = pthread_cond_wait (&current->wait_cond, &current->wait_mutex);
  178. else
  179. r = pthread_cond_timedwait (&current->wait_cond, &current->wait_mutex,
  180. &ts);
  181. // In older glibc's (prior to 2.1.3), the cond_wait functions may
  182. // spuriously wake up on a signal. Catch that here.
  183. if (r != EINTR)
  184. done_sleeping = true;
  185. }
  186. // Check for an interrupt *before* releasing the wait mutex.
  187. jboolean interrupted = current_obj->interrupt_flag;
  188. pthread_mutex_unlock (&current->wait_mutex);
  189. // Reaquire the monitor mutex, and restore the lock count.
  190. pthread_mutex_lock (&mu->mutex);
  191. mu->owner = self;
  192. mu->count = count;
  193. // If we were interrupted, or if a timeout occurred, remove ourself from
  194. // the cv wait list now. (If we were notified normally, notify() will have
  195. // already taken care of this)
  196. if (r == ETIMEDOUT || interrupted)
  197. {
  198. _Jv_Thread_t *prev = NULL;
  199. for (_Jv_Thread_t *t = cv->first; t != NULL; t = t->next)
  200. {
  201. if (t == current)
  202. {
  203. if (prev != NULL)
  204. prev->next = t->next;
  205. else
  206. cv->first = t->next;
  207. t->next = NULL;
  208. break;
  209. }
  210. prev = t;
  211. }
  212. if (interrupted)
  213. return _JV_INTERRUPTED;
  214. }
  215. return 0;
  216. }
  217. int
  218. _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
  219. {
  220. if (_Jv_MutexCheckMonitor (mu))
  221. return _JV_NOT_OWNER;
  222. _Jv_Thread_t *target;
  223. _Jv_Thread_t *prev = NULL;
  224. for (target = cv->first; target != NULL; target = target->next)
  225. {
  226. pthread_mutex_lock (&target->wait_mutex);
  227. if (target->thread_obj->interrupt_flag)
  228. {
  229. // Don't notify a thread that has already been interrupted.
  230. pthread_mutex_unlock (&target->wait_mutex);
  231. prev = target;
  232. continue;
  233. }
  234. pthread_cond_signal (&target->wait_cond);
  235. pthread_mutex_unlock (&target->wait_mutex);
  236. // Two concurrent notify() calls must not be delivered to the same
  237. // thread, so remove the target thread from the cv wait list now.
  238. if (prev == NULL)
  239. cv->first = target->next;
  240. else
  241. prev->next = target->next;
  242. target->next = NULL;
  243. break;
  244. }
  245. return 0;
  246. }
  247. int
  248. _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
  249. {
  250. if (_Jv_MutexCheckMonitor (mu))
  251. return _JV_NOT_OWNER;
  252. _Jv_Thread_t *target;
  253. _Jv_Thread_t *prev = NULL;
  254. for (target = cv->first; target != NULL; target = target->next)
  255. {
  256. pthread_mutex_lock (&target->wait_mutex);
  257. pthread_cond_signal (&target->wait_cond);
  258. pthread_mutex_unlock (&target->wait_mutex);
  259. if (prev != NULL)
  260. prev->next = NULL;
  261. prev = target;
  262. }
  263. if (prev != NULL)
  264. prev->next = NULL;
  265. cv->first = NULL;
  266. return 0;
  267. }
  268. void
  269. _Jv_ThreadInterrupt (_Jv_Thread_t *data)
  270. {
  271. pthread_mutex_lock (&data->wait_mutex);
  272. // Set the thread's interrupted flag *after* aquiring its wait_mutex. This
  273. // ensures that there are no races with the interrupt flag being set after
  274. // the waiting thread checks it and before pthread_cond_wait is entered.
  275. data->thread_obj->interrupt_flag = true;
  276. // Interrupt blocking system calls using a signal.
  277. pthread_kill (data->thread, INTR);
  278. pthread_cond_signal (&data->wait_cond);
  279. pthread_mutex_unlock (&data->wait_mutex);
  280. }
  281. /**
  282. * Releases the block on a thread created by _Jv_ThreadPark(). This
  283. * method can also be used to terminate a blockage caused by a prior
  284. * call to park. This operation is unsafe, as the thread must be
  285. * guaranteed to be live.
  286. *
  287. * @param thread the thread to unblock.
  288. */
  289. void
  290. ParkHelper::unpark ()
  291. {
  292. using namespace ::java::lang;
  293. volatile obj_addr_t *ptr = &permit;
  294. /* If this thread is in state RUNNING, give it a permit and return
  295. immediately. */
  296. if (compare_and_swap
  297. (ptr, Thread::THREAD_PARK_RUNNING, Thread::THREAD_PARK_PERMIT))
  298. return;
  299. /* If this thread is parked, put it into state RUNNING and send it a
  300. signal. */
  301. if (compare_and_swap
  302. (ptr, Thread::THREAD_PARK_PARKED, Thread::THREAD_PARK_RUNNING))
  303. {
  304. int result;
  305. pthread_mutex_lock (&mutex);
  306. result = pthread_cond_signal (&cond);
  307. pthread_mutex_unlock (&mutex);
  308. JvAssert (result == 0);
  309. }
  310. }
  311. /**
  312. * Sets our state to dead.
  313. */
  314. void
  315. ParkHelper::deactivate ()
  316. {
  317. permit = ::java::lang::Thread::THREAD_PARK_DEAD;
  318. }
  319. void
  320. ParkHelper::init ()
  321. {
  322. pthread_mutex_init (&mutex, NULL);
  323. pthread_cond_init (&cond, NULL);
  324. permit = ::java::lang::Thread::THREAD_PARK_RUNNING;
  325. }
  326. /**
  327. * Blocks the thread until a matching _Jv_ThreadUnpark() occurs, the
  328. * thread is interrupted or the optional timeout expires. If an
  329. * unpark call has already occurred, this also counts. A timeout
  330. * value of zero is defined as no timeout. When isAbsolute is true,
  331. * the timeout is in milliseconds relative to the epoch. Otherwise,
  332. * the value is the number of nanoseconds which must occur before
  333. * timeout. This call may also return spuriously (i.e. for no
  334. * apparent reason).
  335. *
  336. * @param isAbsolute true if the timeout is specified in milliseconds from
  337. * the epoch.
  338. * @param time either the number of nanoseconds to wait, or a time in
  339. * milliseconds from the epoch to wait for.
  340. */
  341. void
  342. ParkHelper::park (jboolean isAbsolute, jlong time)
  343. {
  344. using namespace ::java::lang;
  345. volatile obj_addr_t *ptr = &permit;
  346. /* If we have a permit, return immediately. */
  347. if (compare_and_swap
  348. (ptr, Thread::THREAD_PARK_PERMIT, Thread::THREAD_PARK_RUNNING))
  349. return;
  350. struct timespec ts;
  351. if (time)
  352. {
  353. unsigned long long seconds;
  354. unsigned long usec;
  355. if (isAbsolute)
  356. {
  357. ts.tv_sec = time / 1000;
  358. ts.tv_nsec = (time % 1000) * 1000 * 1000;
  359. }
  360. else
  361. {
  362. // Calculate the abstime corresponding to the timeout.
  363. jlong nanos = time;
  364. jlong millis = 0;
  365. // For better accuracy, should use pthread_condattr_setclock
  366. // and clock_gettime.
  367. #ifdef HAVE_GETTIMEOFDAY
  368. timeval tv;
  369. gettimeofday (&tv, NULL);
  370. usec = tv.tv_usec;
  371. seconds = tv.tv_sec;
  372. #else
  373. unsigned long long startTime
  374. = java::lang::System::currentTimeMillis();
  375. seconds = startTime / 1000;
  376. /* Assume we're about half-way through this millisecond. */
  377. usec = (startTime % 1000) * 1000 + 500;
  378. #endif
  379. /* These next two statements cannot overflow. */
  380. usec += nanos / 1000;
  381. usec += (millis % 1000) * 1000;
  382. /* These two statements could overflow only if tv.tv_sec was
  383. insanely large. */
  384. seconds += millis / 1000;
  385. seconds += usec / 1000000;
  386. ts.tv_sec = seconds;
  387. if (ts.tv_sec < 0 || (unsigned long long)ts.tv_sec != seconds)
  388. {
  389. // We treat a timeout that won't fit into a struct timespec
  390. // as a wait forever.
  391. millis = nanos = 0;
  392. }
  393. else
  394. /* This next statement also cannot overflow. */
  395. ts.tv_nsec = (usec % 1000000) * 1000 + (nanos % 1000);
  396. }
  397. }
  398. pthread_mutex_lock (&mutex);
  399. if (compare_and_swap
  400. (ptr, Thread::THREAD_PARK_RUNNING, Thread::THREAD_PARK_PARKED))
  401. {
  402. int result = 0;
  403. if (! time)
  404. result = pthread_cond_wait (&cond, &mutex);
  405. else
  406. result = pthread_cond_timedwait (&cond, &mutex, &ts);
  407. JvAssert (result == 0 || result == ETIMEDOUT);
  408. /* If we were unparked by some other thread, this will already
  409. be in state THREAD_PARK_RUNNING. If we timed out or were
  410. interrupted, we have to do it ourself. */
  411. permit = Thread::THREAD_PARK_RUNNING;
  412. }
  413. pthread_mutex_unlock (&mutex);
  414. }
  415. static void
  416. handle_intr (int)
  417. {
  418. // Do nothing.
  419. }
  420. void
  421. _Jv_BlockSigchld()
  422. {
  423. sigset_t mask;
  424. sigemptyset (&mask);
  425. sigaddset (&mask, SIGCHLD);
  426. int c = pthread_sigmask (SIG_BLOCK, &mask, NULL);
  427. if (c != 0)
  428. JvFail (strerror (c));
  429. }
  430. void
  431. _Jv_UnBlockSigchld()
  432. {
  433. sigset_t mask;
  434. sigemptyset (&mask);
  435. sigaddset (&mask, SIGCHLD);
  436. int c = pthread_sigmask (SIG_UNBLOCK, &mask, NULL);
  437. if (c != 0)
  438. JvFail (strerror (c));
  439. }
  440. void
  441. _Jv_InitThreads (void)
  442. {
  443. pthread_key_create (&_Jv_ThreadKey, NULL);
  444. pthread_key_create (&_Jv_ThreadDataKey, NULL);
  445. pthread_mutex_init (&daemon_mutex, NULL);
  446. pthread_cond_init (&daemon_cond, 0);
  447. non_daemon_count = 0;
  448. // Arrange for the interrupt signal to interrupt system calls.
  449. struct sigaction act;
  450. act.sa_handler = handle_intr;
  451. sigemptyset (&act.sa_mask);
  452. act.sa_flags = 0;
  453. sigaction (INTR, &act, NULL);
  454. // Block SIGCHLD here to ensure that any non-Java threads inherit the new
  455. // signal mask.
  456. _Jv_BlockSigchld();
  457. // Check/set the thread stack size.
  458. size_t min_ss = 32 * 1024;
  459. if (sizeof (void *) == 8)
  460. // Bigger default on 64-bit systems.
  461. min_ss *= 2;
  462. #ifdef PTHREAD_STACK_MIN
  463. if (min_ss < PTHREAD_STACK_MIN)
  464. min_ss = PTHREAD_STACK_MIN;
  465. #endif
  466. if (gcj::stack_size > 0 && gcj::stack_size < min_ss)
  467. gcj::stack_size = min_ss;
  468. }
  469. _Jv_Thread_t *
  470. _Jv_ThreadInitData (java::lang::Thread *obj)
  471. {
  472. _Jv_Thread_t *data = (_Jv_Thread_t *) _Jv_Malloc (sizeof (_Jv_Thread_t));
  473. data->flags = 0;
  474. data->thread_obj = obj;
  475. pthread_mutex_init (&data->wait_mutex, NULL);
  476. pthread_cond_init (&data->wait_cond, NULL);
  477. return data;
  478. }
  479. void
  480. _Jv_ThreadDestroyData (_Jv_Thread_t *data)
  481. {
  482. pthread_mutex_destroy (&data->wait_mutex);
  483. pthread_cond_destroy (&data->wait_cond);
  484. _Jv_Free ((void *)data);
  485. }
  486. void
  487. _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
  488. {
  489. #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
  490. if (data->flags & FLAG_START)
  491. {
  492. struct sched_param param;
  493. param.sched_priority = prio;
  494. pthread_setschedparam (data->thread, SCHED_OTHER, &param);
  495. }
  496. #endif
  497. }
  498. void
  499. _Jv_ThreadRegister (_Jv_Thread_t *data)
  500. {
  501. pthread_setspecific (_Jv_ThreadKey, data->thread_obj);
  502. pthread_setspecific (_Jv_ThreadDataKey, data);
  503. // glibc 2.1.3 doesn't set the value of `thread' until after start_routine
  504. // is called. Since it may need to be accessed from the new thread, work
  505. // around the potential race here by explicitly setting it again.
  506. data->thread = pthread_self ();
  507. # ifdef SLOW_PTHREAD_SELF
  508. // Clear all self cache slots that might be needed by this thread.
  509. int dummy;
  510. int low_index = SC_INDEX(&dummy) + SC_CLEAR_MIN;
  511. int high_index = SC_INDEX(&dummy) + SC_CLEAR_MAX;
  512. for (int i = low_index; i <= high_index; ++i)
  513. {
  514. int current_index = i;
  515. if (current_index < 0)
  516. current_index += SELF_CACHE_SIZE;
  517. if (current_index >= SELF_CACHE_SIZE)
  518. current_index -= SELF_CACHE_SIZE;
  519. _Jv_self_cache[current_index].high_sp_bits = BAD_HIGH_SP_VALUE;
  520. }
  521. # endif
  522. // Block SIGCHLD which is used in natPosixProcess.cc.
  523. _Jv_BlockSigchld();
  524. }
  525. void
  526. _Jv_ThreadUnRegister ()
  527. {
  528. pthread_setspecific (_Jv_ThreadKey, NULL);
  529. pthread_setspecific (_Jv_ThreadDataKey, NULL);
  530. }
  531. // This function is called when a thread is started. We don't arrange
  532. // to call the `run' method directly, because this function must
  533. // return a value.
  534. static void *
  535. really_start (void *x)
  536. {
  537. struct starter *info = (struct starter *) x;
  538. _Jv_ThreadRegister (info->data);
  539. info->method (info->data->thread_obj);
  540. if (! (info->data->flags & FLAG_DAEMON))
  541. {
  542. pthread_mutex_lock (&daemon_mutex);
  543. --non_daemon_count;
  544. if (! non_daemon_count)
  545. pthread_cond_signal (&daemon_cond);
  546. pthread_mutex_unlock (&daemon_mutex);
  547. }
  548. return NULL;
  549. }
  550. void
  551. _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
  552. _Jv_ThreadStartFunc *meth)
  553. {
  554. struct sched_param param;
  555. pthread_attr_t attr;
  556. struct starter *info;
  557. if (data->flags & FLAG_START)
  558. return;
  559. data->flags |= FLAG_START;
  560. // Block SIGCHLD which is used in natPosixProcess.cc.
  561. // The current mask is inherited by the child thread.
  562. _Jv_BlockSigchld();
  563. param.sched_priority = thread->getPriority();
  564. pthread_attr_init (&attr);
  565. pthread_attr_setschedparam (&attr, &param);
  566. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  567. // Set stack size if -Xss option was given.
  568. if (gcj::stack_size > 0)
  569. {
  570. int e = pthread_attr_setstacksize (&attr, gcj::stack_size);
  571. if (e != 0)
  572. JvFail (strerror (e));
  573. }
  574. info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
  575. info->method = meth;
  576. info->data = data;
  577. if (! thread->isDaemon())
  578. {
  579. pthread_mutex_lock (&daemon_mutex);
  580. ++non_daemon_count;
  581. pthread_mutex_unlock (&daemon_mutex);
  582. }
  583. else
  584. data->flags |= FLAG_DAEMON;
  585. int r = pthread_create (&data->thread, &attr, really_start, (void *) info);
  586. pthread_attr_destroy (&attr);
  587. if (r)
  588. {
  589. const char* msg = "Cannot create additional threads";
  590. throw new java::lang::OutOfMemoryError (JvNewStringUTF (msg));
  591. }
  592. }
  593. void
  594. _Jv_ThreadWait (void)
  595. {
  596. pthread_mutex_lock (&daemon_mutex);
  597. if (non_daemon_count)
  598. pthread_cond_wait (&daemon_cond, &daemon_mutex);
  599. pthread_mutex_unlock (&daemon_mutex);
  600. }
  601. #if defined(SLOW_PTHREAD_SELF)
  602. #include "sysdep/locks.h"
  603. // Support for pthread_self() lookup cache.
  604. volatile self_cache_entry _Jv_self_cache[SELF_CACHE_SIZE];
  605. _Jv_ThreadId_t
  606. _Jv_ThreadSelf_out_of_line(volatile self_cache_entry *sce, size_t high_sp_bits)
  607. {
  608. pthread_t self = pthread_self();
  609. sce -> high_sp_bits = high_sp_bits;
  610. write_barrier();
  611. sce -> self = self;
  612. return self;
  613. }
  614. #endif /* SLOW_PTHREAD_SELF */