natThread.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // natThread.cc - Native part of Thread class.
  2. /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2006, 2007 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. #include <config.h>
  8. #include <stdlib.h>
  9. #include <gcj/cni.h>
  10. #include <jvm.h>
  11. #include <java-threads.h>
  12. #include <gnu/gcj/RawDataManaged.h>
  13. #include <java/lang/Thread.h>
  14. #include <java/lang/Thread$State.h>
  15. #include <java/lang/Thread$UncaughtExceptionHandler.h>
  16. #include <java/lang/ThreadGroup.h>
  17. #include <java/lang/IllegalArgumentException.h>
  18. #include <java/lang/IllegalThreadStateException.h>
  19. #include <java/lang/InterruptedException.h>
  20. #include <java/lang/NullPointerException.h>
  21. #include <jni.h>
  22. #ifdef INTERPRETER
  23. #include <jvmti.h>
  24. #include "jvmti-int.h"
  25. #endif
  26. #ifdef ENABLE_JVMPI
  27. #include <jvmpi.h>
  28. #endif
  29. static void finalize_native (jobject ptr);
  30. // This is called from the constructor to initialize the native side
  31. // of the Thread.
  32. void
  33. java::lang::Thread::initialize_native (void)
  34. {
  35. natThread *nt = (natThread *) _Jv_AllocBytes (sizeof (natThread));
  36. state = JV_NEW;
  37. nt->alive_flag = THREAD_DEAD;
  38. data = (gnu::gcj::RawDataManaged *) nt;
  39. // Register a finalizer to clean up the native thread resources.
  40. _Jv_RegisterFinalizer (data, finalize_native);
  41. _Jv_MutexInit (&nt->join_mutex);
  42. _Jv_CondInit (&nt->join_cond);
  43. nt->park_helper.init();
  44. nt->thread = _Jv_ThreadInitData (this);
  45. // FIXME: if JNI_ENV is set we will want to free it. It is
  46. // malloc()d.
  47. nt->jni_env = NULL;
  48. }
  49. static void
  50. finalize_native (jobject ptr)
  51. {
  52. natThread *nt = (natThread *) ptr;
  53. _Jv_ThreadDestroyData (nt->thread);
  54. #ifdef _Jv_HaveCondDestroy
  55. _Jv_CondDestroy (&nt->join_cond);
  56. #endif
  57. #ifdef _Jv_HaveMutexDestroy
  58. _Jv_MutexDestroy (&nt->join_mutex);
  59. #endif
  60. _Jv_FreeJNIEnv((JNIEnv*)nt->jni_env);
  61. nt->park_helper.destroy();
  62. }
  63. jint
  64. java::lang::Thread::countStackFrames (void)
  65. {
  66. // NOTE: This is deprecated in JDK 1.2.
  67. // Old applets still call this method. Rather than throwing
  68. // UnsupportedOperationException we simply fail silently.
  69. return 0;
  70. }
  71. java::lang::Thread *
  72. java::lang::Thread::currentThread (void)
  73. {
  74. return _Jv_ThreadCurrent ();
  75. }
  76. jboolean
  77. java::lang::Thread::holdsLock (jobject obj)
  78. {
  79. if (!obj)
  80. throw new NullPointerException;
  81. return !_Jv_ObjectCheckMonitor (obj);
  82. }
  83. jboolean
  84. java::lang::Thread::isAlive (void)
  85. {
  86. natThread *nt = (natThread *) data;
  87. return nt->alive_flag != (obj_addr_t)THREAD_DEAD;
  88. }
  89. void
  90. java::lang::Thread::interrupt (void)
  91. {
  92. checkAccess ();
  93. natThread *nt = (natThread *) data;
  94. // If a thread is in state ALIVE, we atomically set it to state
  95. // SIGNALED and send it a signal. Once we've sent it the signal, we
  96. // set its state back to ALIVE.
  97. if (compare_and_swap
  98. (&nt->alive_flag, Thread::THREAD_ALIVE, Thread::THREAD_SIGNALED))
  99. {
  100. _Jv_ThreadInterrupt (nt->thread);
  101. compare_and_swap
  102. (&nt->alive_flag, THREAD_SIGNALED, Thread::THREAD_ALIVE);
  103. // Even though we've interrupted this thread, it might still be
  104. // parked.
  105. nt->park_helper.unpark ();
  106. }
  107. }
  108. void
  109. java::lang::Thread::join (jlong millis, jint nanos)
  110. {
  111. if (millis < 0 || nanos < 0 || nanos > 999999)
  112. throw new IllegalArgumentException;
  113. Thread *current = currentThread ();
  114. // Here `NT' is the native structure for the thread we are trying to join.
  115. natThread *nt = (natThread *) data;
  116. // Now wait for: (1) an interrupt, (2) the thread to exit, or (3)
  117. // the timeout to occur.
  118. _Jv_MutexLock (&nt->join_mutex);
  119. if (! isAlive ())
  120. {
  121. _Jv_MutexUnlock (&nt->join_mutex);
  122. return;
  123. }
  124. _Jv_CondWait (&nt->join_cond, &nt->join_mutex, millis, nanos);
  125. _Jv_MutexUnlock (&nt->join_mutex);
  126. if (current->isInterrupted (true))
  127. throw new InterruptedException;
  128. }
  129. void
  130. java::lang::Thread::resume (void)
  131. {
  132. checkAccess ();
  133. // Old applets still call this method. Rather than throwing
  134. // UnsupportedOperationException we simply fail silently.
  135. }
  136. void
  137. java::lang::Thread::setPriority (jint newPriority)
  138. {
  139. checkAccess ();
  140. if (newPriority < MIN_PRIORITY || newPriority > MAX_PRIORITY)
  141. throw new IllegalArgumentException;
  142. jint gmax = group->getMaxPriority();
  143. if (newPriority > gmax)
  144. newPriority = gmax;
  145. priority = newPriority;
  146. natThread *nt = (natThread *) data;
  147. _Jv_ThreadSetPriority (nt->thread, priority);
  148. }
  149. void
  150. java::lang::Thread::sleep (jlong millis, jint nanos)
  151. {
  152. if (millis < 0 || nanos < 0 || nanos > 999999)
  153. throw new IllegalArgumentException;
  154. if (millis == 0 && nanos == 0)
  155. ++nanos;
  156. Thread *current = currentThread ();
  157. // We use a condition variable to implement sleeping so that an
  158. // interrupt can wake us up.
  159. natThread *nt = (natThread *) current->data;
  160. _Jv_MutexLock (&nt->join_mutex);
  161. _Jv_CondWait (&nt->join_cond, &nt->join_mutex, millis, nanos);
  162. _Jv_MutexUnlock (&nt->join_mutex);
  163. if (current->isInterrupted (true))
  164. throw new InterruptedException;
  165. }
  166. void
  167. java::lang::Thread::finish_ ()
  168. {
  169. __sync_synchronize();
  170. natThread *nt = (natThread *) data;
  171. nt->park_helper.deactivate ();
  172. group->removeThread (this);
  173. #ifdef INTERPRETER
  174. if (JVMTI_REQUESTED_EVENT (ThreadEnd))
  175. _Jv_JVMTI_PostEvent (JVMTI_EVENT_THREAD_END, this, nt->jni_env);
  176. #endif
  177. #ifdef ENABLE_JVMPI
  178. if (_Jv_JVMPI_Notify_THREAD_END)
  179. {
  180. JVMPI_Event event;
  181. event.event_type = JVMPI_EVENT_THREAD_END;
  182. event.env_id = _Jv_GetCurrentJNIEnv ();
  183. _Jv_DisableGC ();
  184. (*_Jv_JVMPI_Notify_THREAD_END) (&event);
  185. _Jv_EnableGC ();
  186. }
  187. #endif
  188. // If a method cache was created, free it.
  189. _Jv_FreeMethodCache();
  190. // Clear out thread locals.
  191. locals = NULL;
  192. // Signal any threads that are waiting to join() us.
  193. _Jv_MutexLock (&nt->join_mutex);
  194. {
  195. JvSynchronize sync (this);
  196. nt->alive_flag = THREAD_DEAD;
  197. state = JV_TERMINATED;
  198. }
  199. _Jv_CondNotifyAll (&nt->join_cond, &nt->join_mutex);
  200. _Jv_MutexUnlock (&nt->join_mutex);
  201. }
  202. // Run once at thread startup, either when thread is attached or when
  203. // _Jv_ThreadRun is called.
  204. static void
  205. _Jv_NotifyThreadStart (java::lang::Thread* thread)
  206. {
  207. #ifdef INTERPRETER
  208. if (JVMTI_REQUESTED_EVENT (ThreadStart))
  209. {
  210. natThread *nt = reinterpret_cast<natThread *> (thread->data);
  211. _Jv_JVMTI_PostEvent (JVMTI_EVENT_THREAD_START, thread, nt->jni_env);
  212. }
  213. #endif
  214. #ifdef ENABLE_JVMPI
  215. if (_Jv_JVMPI_Notify_THREAD_START)
  216. {
  217. JVMPI_Event event;
  218. jstring thread_name = thread->getName ();
  219. jstring group_name = NULL, parent_name = NULL;
  220. java::lang::ThreadGroup *group = thread->getThreadGroup ();
  221. if (group)
  222. {
  223. group_name = group->getName ();
  224. group = group->getParent ();
  225. if (group)
  226. parent_name = group->getName ();
  227. }
  228. int thread_len = thread_name ? JvGetStringUTFLength (thread_name) : 0;
  229. int group_len = group_name ? JvGetStringUTFLength (group_name) : 0;
  230. int parent_len = parent_name ? JvGetStringUTFLength (parent_name) : 0;
  231. char thread_chars[thread_len + 1];
  232. char group_chars[group_len + 1];
  233. char parent_chars[parent_len + 1];
  234. if (thread_name)
  235. JvGetStringUTFRegion (thread_name, 0,
  236. thread_name->length(), thread_chars);
  237. if (group_name)
  238. JvGetStringUTFRegion (group_name, 0,
  239. group_name->length(), group_chars);
  240. if (parent_name)
  241. JvGetStringUTFRegion (parent_name, 0,
  242. parent_name->length(), parent_chars);
  243. thread_chars[thread_len] = '\0';
  244. group_chars[group_len] = '\0';
  245. parent_chars[parent_len] = '\0';
  246. event.event_type = JVMPI_EVENT_THREAD_START;
  247. event.env_id = NULL;
  248. event.u.thread_start.thread_name = thread_chars;
  249. event.u.thread_start.group_name = group_chars;
  250. event.u.thread_start.parent_name = parent_chars;
  251. event.u.thread_start.thread_id = (jobjectID) thread;
  252. event.u.thread_start.thread_env_id = _Jv_GetCurrentJNIEnv ();
  253. _Jv_DisableGC ();
  254. (*_Jv_JVMPI_Notify_THREAD_START) (&event);
  255. _Jv_EnableGC ();
  256. }
  257. #endif
  258. }
  259. void
  260. _Jv_ThreadRun (java::lang::Thread* thread)
  261. {
  262. try
  263. {
  264. _Jv_NotifyThreadStart (thread);
  265. thread->run ();
  266. }
  267. catch (java::lang::Throwable *t)
  268. {
  269. // Uncaught exceptions are forwarded to the ThreadGroup. If
  270. // this results in an uncaught exception, that is ignored.
  271. try
  272. {
  273. thread->getUncaughtExceptionHandler()->uncaughtException (thread, t);
  274. }
  275. catch (java::lang::Throwable *f)
  276. {
  277. // Nothing.
  278. }
  279. }
  280. thread->finish_ ();
  281. }
  282. _Jv_Thread_t*
  283. _Jv_ThreadGetData (java::lang::Thread* thread)
  284. {
  285. natThread* nt = (natThread*) thread->data;
  286. return nt->thread;
  287. }
  288. void
  289. java::lang::Thread::start (void)
  290. {
  291. JvSynchronize sync (this);
  292. // Its illegal to re-start() a thread, even if its dead.
  293. if (!startable_flag)
  294. throw new IllegalThreadStateException;
  295. natThread *nt = (natThread *) data;
  296. nt->alive_flag = THREAD_ALIVE;
  297. startable_flag = false;
  298. state = JV_RUNNABLE;
  299. _Jv_ThreadStart (this, nt->thread, (_Jv_ThreadStartFunc *) &_Jv_ThreadRun);
  300. }
  301. void
  302. java::lang::Thread::stop (java::lang::Throwable *)
  303. {
  304. checkAccess ();
  305. // Old applets still call this method. Rather than throwing
  306. // UnsupportedOperationException we simply fail silently.
  307. }
  308. void
  309. java::lang::Thread::suspend (void)
  310. {
  311. checkAccess ();
  312. // Old applets still call this method. Rather than throwing
  313. // UnsupportedOperationException we simply fail silently.
  314. }
  315. static int nextThreadNumber = 0;
  316. jstring
  317. java::lang::Thread::gen_name (void)
  318. {
  319. jint i;
  320. jclass sync = &java::lang::Thread::class$;
  321. {
  322. JvSynchronize dummy(sync);
  323. i = ++nextThreadNumber;
  324. }
  325. // Use an array large enough for "-2147483648"; i.e. 11 chars, + "Thread-".
  326. jchar buffer[7+11];
  327. jchar *bufend = (jchar *) ((char *) buffer + sizeof(buffer));
  328. i = _Jv_FormatInt (bufend, i);
  329. jchar *ptr = bufend - i;
  330. // Prepend "Thread-".
  331. *--ptr = '-';
  332. *--ptr = 'd';
  333. *--ptr = 'a';
  334. *--ptr = 'e';
  335. *--ptr = 'r';
  336. *--ptr = 'h';
  337. *--ptr = 'T';
  338. return JvNewString (ptr, bufend - ptr);
  339. }
  340. void
  341. java::lang::Thread::yield (void)
  342. {
  343. _Jv_ThreadYield ();
  344. }
  345. ::java::lang::Thread$State *
  346. java::lang::Thread::getState()
  347. {
  348. _Jv_InitClass(&::java::lang::Thread$State::class$);
  349. switch (state)
  350. {
  351. case JV_BLOCKED:
  352. return ::java::lang::Thread$State::BLOCKED;
  353. case JV_NEW:
  354. return ::java::lang::Thread$State::NEW;
  355. case JV_RUNNABLE:
  356. return ::java::lang::Thread$State::RUNNABLE;
  357. case JV_TERMINATED:
  358. return ::java::lang::Thread$State::TERMINATED;
  359. case JV_TIMED_WAITING:
  360. return ::java::lang::Thread$State::TIMED_WAITING;
  361. case JV_WAITING:
  362. return ::java::lang::Thread$State::WAITING;
  363. }
  364. // We don't really need a default, but this makes the compiler
  365. // happy.
  366. return ::java::lang::Thread$State::RUNNABLE;
  367. }
  368. JNIEnv *
  369. _Jv_GetCurrentJNIEnv ()
  370. {
  371. java::lang::Thread *t = _Jv_ThreadCurrent ();
  372. if (t == NULL)
  373. return NULL;
  374. return ((natThread *) t->data)->jni_env;
  375. }
  376. void
  377. _Jv_SetCurrentJNIEnv (JNIEnv *env)
  378. {
  379. java::lang::Thread *t = _Jv_ThreadCurrent ();
  380. JvAssert (t != NULL);
  381. ((natThread *) t->data)->jni_env = env;
  382. }
  383. // Attach the current native thread to an existing (but unstarted) Thread
  384. // object. Does not register thread with the garbage collector.
  385. // Returns -1 on failure, 0 upon success.
  386. jint
  387. _Jv_AttachCurrentThread(java::lang::Thread* thread)
  388. {
  389. JvSynchronize sync (thread);
  390. if (thread == NULL || thread->startable_flag == false)
  391. return -1;
  392. thread->startable_flag = false;
  393. natThread *nt = (natThread *) thread->data;
  394. nt->alive_flag = ::java::lang::Thread::THREAD_ALIVE;
  395. thread->state = JV_RUNNABLE;
  396. _Jv_ThreadRegister (nt->thread);
  397. return 0;
  398. }
  399. java::lang::Thread*
  400. _Jv_AttachCurrentThread(jstring name, java::lang::ThreadGroup* group)
  401. {
  402. // Register thread with GC before attempting any allocations.
  403. _Jv_GCAttachThread ();
  404. java::lang::Thread *thread = _Jv_ThreadCurrent ();
  405. if (thread != NULL)
  406. return thread;
  407. if (name == NULL)
  408. name = java::lang::Thread::gen_name ();
  409. thread = new java::lang::Thread (NULL, group, NULL, name, false);
  410. _Jv_AttachCurrentThread (thread);
  411. _Jv_NotifyThreadStart (thread);
  412. return thread;
  413. }
  414. java::lang::Thread*
  415. _Jv_AttachCurrentThreadAsDaemon(jstring name, java::lang::ThreadGroup* group)
  416. {
  417. java::lang::Thread *thread = _Jv_ThreadCurrent ();
  418. if (thread != NULL)
  419. return thread;
  420. if (name == NULL)
  421. name = java::lang::Thread::gen_name ();
  422. thread = new java::lang::Thread (NULL, group, NULL, name, false);
  423. thread->setDaemon (true);
  424. _Jv_AttachCurrentThread (thread);
  425. _Jv_NotifyThreadStart (thread);
  426. return thread;
  427. }
  428. jint
  429. _Jv_DetachCurrentThread (void)
  430. {
  431. java::lang::Thread *t = _Jv_ThreadCurrent ();
  432. if (t == NULL)
  433. return -1;
  434. _Jv_ThreadUnRegister ();
  435. _Jv_GCDetachThread ();
  436. // Release the monitors.
  437. t->finish_ ();
  438. return 0;
  439. }