thr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* GNU Objective C Runtime Thread Interface
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 3, or (at your option) any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "objc-private/common.h"
  20. #include "objc-private/error.h"
  21. #define _LIBOBJC
  22. #include "config.h"
  23. #include "tconfig.h"
  24. #include "coretypes.h"
  25. #include "tm.h"
  26. #include "defaults.h"
  27. #include "objc/thr.h"
  28. #include "objc/message.h" /* For objc_msg_lookup(). */
  29. #include "objc/runtime.h"
  30. #include "objc-private/module-abi-8.h"
  31. #include "objc-private/runtime.h"
  32. #include <gthr.h>
  33. #include <stdlib.h>
  34. /* Global exit status. */
  35. int __objc_thread_exit_status = 0;
  36. /* Flag which lets us know if we ever became multi threaded. */
  37. int __objc_is_multi_threaded = 0;
  38. /* The hook function called when the runtime becomes multi
  39. threaded. */
  40. objc_thread_callback _objc_became_multi_threaded = NULL;
  41. /* Use this to set the hook function that will be called when the
  42. runtime initially becomes multi threaded. The hook function is
  43. only called once, meaning only when the 2nd thread is spawned, not
  44. for each and every thread.
  45. It returns the previous hook function or NULL if there is none.
  46. A program outside of the runtime could set this to some function so
  47. it can be informed; for example, the GNUstep Base Library sets it
  48. so it can implement the NSBecomingMultiThreaded notification. */
  49. objc_thread_callback objc_set_thread_callback (objc_thread_callback func)
  50. {
  51. objc_thread_callback temp = _objc_became_multi_threaded;
  52. _objc_became_multi_threaded = func;
  53. return temp;
  54. }
  55. /* Private functions.
  56. These functions are utilized by the runtime, but they are not
  57. considered part of the public interface. */
  58. /* Initialize the threads subsystem. */
  59. int
  60. __objc_init_thread_system(void)
  61. {
  62. return __gthread_objc_init_thread_system ();
  63. }
  64. /* First function called in a thread, starts everything else.
  65. This function is passed to the backend by objc_thread_detach as the
  66. starting function for a new thread. */
  67. struct __objc_thread_start_state
  68. {
  69. SEL selector;
  70. id object;
  71. id argument;
  72. };
  73. static void __attribute__((noreturn))
  74. __objc_thread_detach_function (struct __objc_thread_start_state *istate)
  75. {
  76. /* Valid state? */
  77. if (istate)
  78. {
  79. id (*imp) (id, SEL, id);
  80. SEL selector = istate->selector;
  81. id object = istate->object;
  82. id argument = istate->argument;
  83. /* Don't need anymore so free it. */
  84. objc_free (istate);
  85. /* Clear out the thread local storage. */
  86. objc_thread_set_data (NULL);
  87. /* Check to see if we just became multi threaded. */
  88. if (! __objc_is_multi_threaded)
  89. {
  90. __objc_is_multi_threaded = 1;
  91. /* Call the hook function. */
  92. if (_objc_became_multi_threaded != NULL)
  93. (*_objc_became_multi_threaded) ();
  94. }
  95. /* Call the method. */
  96. if ((imp = (id (*) (id, SEL, id))objc_msg_lookup (object, selector)))
  97. (*imp) (object, selector, argument);
  98. else
  99. {
  100. /* FIXME: Should we abort here ? */
  101. _objc_abort ("objc_thread_detach called with bad selector.\n");
  102. }
  103. }
  104. else
  105. {
  106. /* FIXME: Should we abort here ? */
  107. _objc_abort ("objc_thread_detach called with NULL state.\n");
  108. }
  109. /* Exit the thread. */
  110. objc_thread_exit ();
  111. /* Make sure compiler detects no return. */
  112. __builtin_trap ();
  113. }
  114. /* Public functions.
  115. These functions constitute the public interface to the Objective-C
  116. thread and mutex functionality. */
  117. /* Detach a new thread of execution and return its id. Returns NULL
  118. if fails. Thread is started by sending message with selector to
  119. object. Message takes a single argument. */
  120. objc_thread_t
  121. objc_thread_detach (SEL selector, id object, id argument)
  122. {
  123. struct __objc_thread_start_state *istate;
  124. objc_thread_t thread_id = NULL;
  125. /* Allocate the state structure. */
  126. if (!(istate = (struct __objc_thread_start_state *)objc_malloc
  127. (sizeof (*istate))))
  128. return NULL;
  129. /* Initialize the state structure. */
  130. istate->selector = selector;
  131. istate->object = object;
  132. istate->argument = argument;
  133. /* Lock access. */
  134. objc_mutex_lock (__objc_runtime_mutex);
  135. /* Call the backend to spawn the thread. */
  136. if ((thread_id = __gthread_objc_thread_detach ((void *)__objc_thread_detach_function,
  137. istate)) == NULL)
  138. {
  139. /* Failed! */
  140. objc_mutex_unlock (__objc_runtime_mutex);
  141. objc_free (istate);
  142. return NULL;
  143. }
  144. /* Increment our thread counter. */
  145. __objc_runtime_threads_alive++;
  146. objc_mutex_unlock (__objc_runtime_mutex);
  147. return thread_id;
  148. }
  149. /* Set the current thread's priority. */
  150. int
  151. objc_thread_set_priority (int priority)
  152. {
  153. return __gthread_objc_thread_set_priority (priority);
  154. }
  155. /* Return the current thread's priority. */
  156. int
  157. objc_thread_get_priority (void)
  158. {
  159. return __gthread_objc_thread_get_priority ();
  160. }
  161. /* Yield our process time to another thread. Any BUSY waiting that is
  162. done by a thread should use this function to make sure that other
  163. threads can make progress even on a lazy uniprocessor system. */
  164. void
  165. objc_thread_yield (void)
  166. {
  167. __gthread_objc_thread_yield ();
  168. }
  169. /* Terminate the current tread. Doesn't return. Actually, if it
  170. failed returns -1. */
  171. int
  172. objc_thread_exit (void)
  173. {
  174. /* Decrement our counter of the number of threads alive. */
  175. objc_mutex_lock (__objc_runtime_mutex);
  176. __objc_runtime_threads_alive--;
  177. objc_mutex_unlock (__objc_runtime_mutex);
  178. /* Call the backend to terminate the thread. */
  179. return __gthread_objc_thread_exit ();
  180. }
  181. /* Returns an integer value which uniquely describes a thread. Must
  182. not be NULL which is reserved as a marker for "no thread". */
  183. objc_thread_t
  184. objc_thread_id (void)
  185. {
  186. return __gthread_objc_thread_id ();
  187. }
  188. /* Sets the thread's local storage pointer. Returns 0 if successful
  189. or -1 if failed. */
  190. int
  191. objc_thread_set_data (void *value)
  192. {
  193. return __gthread_objc_thread_set_data (value);
  194. }
  195. /* Returns the thread's local storage pointer. Returns NULL on
  196. failure. */
  197. void *
  198. objc_thread_get_data (void)
  199. {
  200. return __gthread_objc_thread_get_data ();
  201. }
  202. /* Public mutex functions */
  203. /* Allocate a mutex. Return the mutex pointer if successful or NULL
  204. if the allocation failed for any reason. */
  205. objc_mutex_t
  206. objc_mutex_allocate (void)
  207. {
  208. objc_mutex_t mutex;
  209. /* Allocate the mutex structure. */
  210. if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex))))
  211. return NULL;
  212. /* Call backend to create the mutex. */
  213. if (__gthread_objc_mutex_allocate (mutex))
  214. {
  215. /* Failed! */
  216. objc_free (mutex);
  217. return NULL;
  218. }
  219. /* Initialize mutex. */
  220. mutex->owner = NULL;
  221. mutex->depth = 0;
  222. return mutex;
  223. }
  224. /* Deallocate a mutex. Note that this includes an implicit mutex_lock
  225. to insure that no one else is using the lock. It is legal to
  226. deallocate a lock if we have a lock on it, but illegal to
  227. deallocate a lock held by anyone else. Returns the number of locks
  228. on the thread. (1 for deallocate). */
  229. int
  230. objc_mutex_deallocate (objc_mutex_t mutex)
  231. {
  232. int depth;
  233. /* Valid mutex? */
  234. if (! mutex)
  235. return -1;
  236. /* Acquire lock on mutex. */
  237. depth = objc_mutex_lock (mutex);
  238. /* Call backend to destroy mutex. */
  239. if (__gthread_objc_mutex_deallocate (mutex))
  240. return -1;
  241. /* Free the mutex structure. */
  242. objc_free (mutex);
  243. /* Return last depth. */
  244. return depth;
  245. }
  246. /* Grab a lock on a mutex. If this thread already has a lock on this
  247. mutex then we increment the lock count. If another thread has a
  248. lock on the mutex we block and wait for the thread to release the
  249. lock. Returns the lock count on the mutex held by this thread. */
  250. int
  251. objc_mutex_lock (objc_mutex_t mutex)
  252. {
  253. objc_thread_t thread_id;
  254. int status;
  255. /* Valid mutex? */
  256. if (! mutex)
  257. return -1;
  258. /* If we already own the lock then increment depth. */
  259. thread_id = __gthread_objc_thread_id ();
  260. if (mutex->owner == thread_id)
  261. return ++mutex->depth;
  262. /* Call the backend to lock the mutex. */
  263. status = __gthread_objc_mutex_lock (mutex);
  264. /* Failed? */
  265. if (status)
  266. return status;
  267. /* Successfully locked the thread. */
  268. mutex->owner = thread_id;
  269. return mutex->depth = 1;
  270. }
  271. /* Try to grab a lock on a mutex. If this thread already has a lock
  272. on this mutex then we increment the lock count and return it. If
  273. another thread has a lock on the mutex returns -1. */
  274. int
  275. objc_mutex_trylock (objc_mutex_t mutex)
  276. {
  277. objc_thread_t thread_id;
  278. int status;
  279. /* Valid mutex? */
  280. if (! mutex)
  281. return -1;
  282. /* If we already own the lock then increment depth. */
  283. thread_id = __gthread_objc_thread_id ();
  284. if (mutex->owner == thread_id)
  285. return ++mutex->depth;
  286. /* Call the backend to try to lock the mutex. */
  287. status = __gthread_objc_mutex_trylock (mutex);
  288. /* Failed? */
  289. if (status)
  290. return status;
  291. /* Successfully locked the thread. */
  292. mutex->owner = thread_id;
  293. return mutex->depth = 1;
  294. }
  295. /* Unlocks the mutex by one level. Decrements the lock count on this
  296. mutex by one. If the lock count reaches zero, release the lock on
  297. the mutex. Returns the lock count on the mutex. It is an error to
  298. attempt to unlock a mutex which this thread doesn't hold in which
  299. case return -1 and the mutex is unaffected. */
  300. int
  301. objc_mutex_unlock (objc_mutex_t mutex)
  302. {
  303. objc_thread_t thread_id;
  304. int status;
  305. /* Valid mutex? */
  306. if (! mutex)
  307. return -1;
  308. /* If another thread owns the lock then abort. */
  309. thread_id = __gthread_objc_thread_id ();
  310. if (mutex->owner != thread_id)
  311. return -1;
  312. /* Decrement depth and return. */
  313. if (mutex->depth > 1)
  314. return --mutex->depth;
  315. /* Depth down to zero so we are no longer the owner. */
  316. mutex->depth = 0;
  317. mutex->owner = NULL;
  318. /* Have the backend unlock the mutex. */
  319. status = __gthread_objc_mutex_unlock (mutex);
  320. /* Failed? */
  321. if (status)
  322. return status;
  323. return 0;
  324. }
  325. /* Public condition mutex functions */
  326. /* Allocate a condition. Return the condition pointer if successful
  327. or NULL if the allocation failed for any reason. */
  328. objc_condition_t
  329. objc_condition_allocate (void)
  330. {
  331. objc_condition_t condition;
  332. /* Allocate the condition mutex structure. */
  333. if (! (condition =
  334. (objc_condition_t) objc_malloc (sizeof (struct objc_condition))))
  335. return NULL;
  336. /* Call the backend to create the condition mutex. */
  337. if (__gthread_objc_condition_allocate (condition))
  338. {
  339. /* Failed! */
  340. objc_free (condition);
  341. return NULL;
  342. }
  343. /* Success! */
  344. return condition;
  345. }
  346. /* Deallocate a condition. Note that this includes an implicit
  347. condition_broadcast to insure that waiting threads have the
  348. opportunity to wake. It is legal to dealloc a condition only if no
  349. other thread is/will be using it. Here we do NOT check for other
  350. threads waiting but just wake them up. */
  351. int
  352. objc_condition_deallocate (objc_condition_t condition)
  353. {
  354. /* Broadcast the condition. */
  355. if (objc_condition_broadcast (condition))
  356. return -1;
  357. /* Call the backend to destroy. */
  358. if (__gthread_objc_condition_deallocate (condition))
  359. return -1;
  360. /* Free the condition mutex structure. */
  361. objc_free (condition);
  362. return 0;
  363. }
  364. /* Wait on the condition unlocking the mutex until
  365. objc_condition_signal () or objc_condition_broadcast () are called
  366. for the same condition. The given mutex *must* have the depth set
  367. to 1 so that it can be unlocked here, so that someone else can lock
  368. it and signal/broadcast the condition. The mutex is used to lock
  369. access to the shared data that make up the "condition"
  370. predicate. */
  371. int
  372. objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
  373. {
  374. objc_thread_t thread_id;
  375. /* Valid arguments? */
  376. if (! mutex || ! condition)
  377. return -1;
  378. /* Make sure we are owner of mutex. */
  379. thread_id = __gthread_objc_thread_id ();
  380. if (mutex->owner != thread_id)
  381. return -1;
  382. /* Cannot be locked more than once. */
  383. if (mutex->depth > 1)
  384. return -1;
  385. /* Virtually unlock the mutex. */
  386. mutex->depth = 0;
  387. mutex->owner = (objc_thread_t)NULL;
  388. /* Call the backend to wait. */
  389. __gthread_objc_condition_wait (condition, mutex);
  390. /* Make ourselves owner of the mutex. */
  391. mutex->owner = thread_id;
  392. mutex->depth = 1;
  393. return 0;
  394. }
  395. /* Wake up all threads waiting on this condition. It is recommended
  396. that the called would lock the same mutex as the threads in
  397. objc_condition_wait before changing the "condition predicate" and
  398. make this call and unlock it right away after this call. */
  399. int
  400. objc_condition_broadcast (objc_condition_t condition)
  401. {
  402. /* Valid condition mutex? */
  403. if (! condition)
  404. return -1;
  405. return __gthread_objc_condition_broadcast (condition);
  406. }
  407. /* Wake up one thread waiting on this condition. It is recommended
  408. that the called would lock the same mutex as the threads in
  409. objc_condition_wait before changing the "condition predicate" and
  410. make this call and unlock it right away after this call. */
  411. int
  412. objc_condition_signal (objc_condition_t condition)
  413. {
  414. /* Valid condition mutex? */
  415. if (! condition)
  416. return -1;
  417. return __gthread_objc_condition_signal (condition);
  418. }
  419. /* Make the objc thread system aware that a thread which is managed
  420. (started, stopped) by external code could access objc facilities
  421. from now on. This is used when you are interfacing with some
  422. external non-objc-based environment/system - you must call
  423. objc_thread_add () before an alien thread makes any calls to
  424. Objective-C. Do not cause the _objc_became_multi_threaded hook to
  425. be executed. */
  426. void
  427. objc_thread_add (void)
  428. {
  429. objc_mutex_lock (__objc_runtime_mutex);
  430. __objc_is_multi_threaded = 1;
  431. __objc_runtime_threads_alive++;
  432. objc_mutex_unlock (__objc_runtime_mutex);
  433. }
  434. /* Make the objc thread system aware that a thread managed (started,
  435. stopped) by some external code will no longer access objc and thus
  436. can be forgotten by the objc thread system. Call
  437. objc_thread_remove () when your alien thread is done with making
  438. calls to Objective-C. */
  439. void
  440. objc_thread_remove (void)
  441. {
  442. objc_mutex_lock (__objc_runtime_mutex);
  443. __objc_runtime_threads_alive--;
  444. objc_mutex_unlock (__objc_runtime_mutex);
  445. }