THREADS 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. This file describes in little detail the modifications to the
  2. Objective-C runtime needed to make it thread safe.
  3. First off, kudos to Galen Hunt who is the author of this great work.
  4. If you have an comments or just want to know where to
  5. send me money to express your undying gratitude for threading the
  6. Objective-C runtime you can reach Galen at:
  7. gchunt@cs.rochester.edu
  8. Any questions, comments, bug reports, etc. should send email either to the
  9. GCC bug account or to:
  10. Scott Christley <scottc@net-community.com>
  11. * Sarray Threading:
  12. The most critical component of the Objective-C runtime is the sparse array
  13. structure (sarray). Sarrays store object selectors and implementations.
  14. Following in the tradition of the Objective-C runtime, my threading
  15. support assumes that fast message dispatching is far more important
  16. than *ANY* and *ALL* other operations. The message dispatching thus
  17. uses *NO* locks on any kind. In fact, if you look in sarray.h, you
  18. will notice that the message dispatching has not been modified.
  19. Instead, I have modified the sarray management functions so that all
  20. updates to the sarray data structure can be made in parallel will
  21. message dispatching.
  22. To support concurrent message dispatching, no dynamically allocated
  23. sarray data structures are freed while more than one thread is
  24. operational. Sarray data structures that are no longer in use are
  25. kept in a linked list of garbage and are released whenever the program
  26. is operating with a single thread. The programmer can also flush the
  27. garbage list by calling sarray_remove_garbage when the programmer can
  28. ensure that no message dispatching is taking place concurrently. The
  29. amount of un-reclaimed sarray garbage should normally be extremely
  30. small in a real program as sarray structures are freed only when using
  31. the "poseAs" functionality and early in program initialization, which
  32. normally occurs while the program is single threaded.
  33. ******************************************************************************
  34. * Static Variables:
  35. The following variables are either statically or globally defined. This list
  36. does not include variables which are internal to implementation dependent
  37. versions of thread-*.c.
  38. The following threading designations are used:
  39. SAFE : Implicitly thread safe.
  40. SINGLE : Must only be used in single thread mode.
  41. MUTEX : Protected by single global mutex objc_runtime_mutex.
  42. UNUSED : Not used in the runtime.
  43. Variable Name: Usage: Defined: Also used in:
  44. =========================== ====== ============ =====================
  45. __objc_class_hash MUTEX class.c
  46. __objc_class_links_resolved UNUSED class.c runtime.h
  47. __objc_class_number MUTEX class.c
  48. __objc_dangling_categories UNUSED init.c
  49. __objc_module_list MUTEX init.c
  50. __objc_selector_array MUTEX selector.c
  51. __objc_selector_hash MUTEX selector.c
  52. __objc_selector_max_index MUTEX selector.c sendmsg.c runtime.h
  53. __objc_selector_names MUTEX selector.c
  54. __objc_thread_exit_status SAFE thread.c
  55. __objc_uninstalled_dtable MUTEX sendmsg.c selector.c
  56. _objc_load_callback SAFE init.c objc-api.h
  57. _objc_lookup_class SAFE class.c objc-api.h
  58. _objc_object_alloc SINGLE objects.c objc-api.h
  59. _objc_object_copy SINGLE objects.c objc-api.h
  60. _objc_object_dispose SINGLE objects.c objc-api.h
  61. frwd_sel SAFE2 sendmsg.c
  62. idxsize MUTEX sarray.c sendmsg.c sarray.h
  63. initialize_sel SAFE2 sendmsg.c
  64. narrays MUTEX sarray.c sendmsg.c sarray.h
  65. nbuckets MUTEX sarray.c sendmsg.c sarray.h
  66. nindices MUTEX sarray.c sarray.h
  67. previous_constructors SAFE1 init.c
  68. proto_class SAFE1 init.c
  69. unclaimed_categories MUTEX init.c
  70. unclaimed_proto_list MUTEX init.c
  71. uninitialized_statics MUTEX init.c
  72. Notes:
  73. 1) Initialized once in unithread mode.
  74. 2) Initialized value will always be same, guaranteed by lock on selector
  75. hash table.
  76. ******************************************************************************
  77. * Frontend/Backend design:
  78. The design of the Objective-C runtime thread and mutex functions utilizes a
  79. frontend/backend implementation.
  80. The frontend, as characterized by the files thr.h and thr.c, is a set
  81. of platform independent structures and functions which represent the
  82. user interface. For example, objc_mutex_lock(). Objective-C programs
  83. should use these structures and functions for their thread and mutex
  84. work if they wish to maintain a high degree of portability across
  85. platforms.
  86. The backend is currently GCC's gthread code (gthr.h and related). For
  87. example, __gthread_objc_mutex_lock(). The thread system is
  88. automatically configured when GCC is configured. On most platforms
  89. this thread backend is able to automatically switch to non-multi-threaded
  90. mode if the threading library is not linked in.
  91. If you want to compile libobjc standalone, then you would need to modify
  92. the configure.ac and makefiles for it and you need to import the
  93. gthread code from GCC.
  94. ******************************************************************************
  95. * Threads:
  96. The thread system attempts to create multiple threads using whatever
  97. operating system or library thread support is available. It does
  98. assume that all system functions are thread safe. Notably this means
  99. that the system implementation of malloc and free must be thread safe.
  100. If a system has multiple processors, the threads are configured for
  101. full parallel processing.
  102. * Backend initialization functions
  103. __objc_init_thread_system(void), int
  104. Initialize the thread subsystem. Called once by __objc_exec_class.
  105. Return -1 if error otherwise return 0.
  106. __objc_close_thread_system(void), int
  107. Closes the thread subsystem, not currently guaranteed to be called.
  108. Return -1 if error otherwise return 0.
  109. *****
  110. * Frontend thread functions
  111. * User programs should use these functions.
  112. objc_thread_detach(SEL selector, id object, id argument), objc_thread_t
  113. Creates and detaches a new thread. The new thread starts by
  114. sending the given selector with a single argument to the
  115. given object.
  116. objc_thread_set_priority(int priority), int
  117. Sets a thread's relative priority within the program. Valid
  118. options are:
  119. OBJC_THREAD_INTERACTIVE_PRIORITY
  120. OBJC_THREAD_BACKGROUND_PRIORITY
  121. OBJC_THREAD_LOW_PRIORITY
  122. objc_thread_get_priority(void), int
  123. Query a thread's priority.
  124. objc_thread_yield(void), void
  125. Yields processor to another thread with equal or higher
  126. priority. It is up to the system scheduler to determine if
  127. the processor is taken or not.
  128. objc_thread_exit(void), int
  129. Terminates a thread. If this is the last thread executing
  130. then the program will terminate.
  131. objc_thread_id(void), int
  132. Returns the current thread's id.
  133. objc_thread_set_data(void *value), int
  134. Set a pointer to the thread's local storage. Local storage is
  135. thread specific.
  136. objc_thread_get_data(void), void *
  137. Returns the pointer to the thread's local storage.
  138. *****
  139. * Backend thread functions
  140. * User programs should *NOT* directly call these functions.
  141. __gthr_objc_thread_detach(void (*func)(void *arg), void *arg), objc_thread_t
  142. Spawns a new thread executing func, called by objc_thread_detach.
  143. Return NULL if error otherwise return thread id.
  144. __gthr_objc_thread_set_priority(int priority), int
  145. Set the thread's priority, called by objc_thread_set_priority.
  146. Return -1 if error otherwise return 0.
  147. __gthr_objc_thread_get_priority(void), int
  148. Query a thread's priority, called by objc_thread_get_priority.
  149. Return -1 if error otherwise return the priority.
  150. __gthr_objc_thread_yield(void), void
  151. Yields the processor, called by objc_thread_yield.
  152. __gthr_objc_thread_exit(void), int
  153. Terminates the thread, called by objc_thread_exit.
  154. Return -1 if error otherwise function does not return.
  155. __gthr_objc_thread_id(void), objc_thread_t
  156. Returns the current thread's id, called by objc_thread_id.
  157. Return -1 if error otherwise return thread id.
  158. __gthr_objc_thread_set_data(void *value), int
  159. Set pointer for thread local storage, called by objc_thread_set_data.
  160. Returns -1 if error otherwise return 0.
  161. __gthr_objc_thread_get_data(void), void *
  162. Returns the pointer to the thread's local storage.
  163. Returns NULL if error, called by objc_thread_get_data.
  164. ******************************************************************************
  165. * Mutexes:
  166. Mutexes can be locked recursively. Each locked mutex remembers
  167. its owner (by thread id) and how many times it has been locked. The
  168. last unlock on a mutex removes the system lock and allows other
  169. threads to access the mutex.
  170. *****
  171. * Frontend mutex functions
  172. * User programs should use these functions.
  173. objc_mutex_allocate(void), objc_mutex_t
  174. Allocates a new mutex. Mutex is initially unlocked.
  175. Return NULL if error otherwise return mutex pointer.
  176. objc_mutex_deallocate(objc_mutex_t mutex), int
  177. Free a mutex. Before freeing the mutex, makes sure that no
  178. one else is using it.
  179. Return -1 if error otherwise return 0.
  180. objc_mutex_lock(objc_mutex_t mutex), int
  181. Locks a mutex. As mentioned earlier, the same thread may call
  182. this routine repeatedly.
  183. Return -1 if error otherwise return 0.
  184. objc_mutex_trylock(objc_mutex_t mutex), int
  185. Attempts to lock a mutex. If lock on mutex can be acquired
  186. then function operates exactly as objc_mutex_lock.
  187. Return -1 if failed to acquire lock otherwise return 0.
  188. objc_mutex_unlock(objc_mutex_t mutex), int
  189. Unlocks the mutex by one level. Other threads may not acquire
  190. the mutex until this thread has released all locks on it.
  191. Return -1 if error otherwise return 0.
  192. *****
  193. * Backend mutex functions
  194. * User programs should *NOT* directly call these functions.
  195. __gthr_objc_mutex_allocate(objc_mutex_t mutex), int
  196. Allocates a new mutex, called by objc_mutex_allocate.
  197. Return -1 if error otherwise return 0.
  198. __gthr_objc_mutex_deallocate(objc_mutex_t mutex), int
  199. Free a mutex, called by objc_mutex_deallocate.
  200. Return -1 if error otherwise return 0.
  201. __gthr_objc_mutex_lock(objc_mutex_t mutex), int
  202. Locks a mutex, called by objc_mutex_lock.
  203. Return -1 if error otherwise return 0.
  204. __gthr_objc_mutex_trylock(objc_mutex_t mutex), int
  205. Attempts to lock a mutex, called by objc_mutex_trylock.
  206. Return -1 if failed to acquire lock or error otherwise return 0.
  207. __gthr_objc_mutex_unlock(objc_mutex_t mutex), int
  208. Unlocks the mutex, called by objc_mutex_unlock.
  209. Return -1 if error otherwise return 0.
  210. ******************************************************************************
  211. * Condition Mutexes:
  212. Mutexes can be locked recursively. Each locked mutex remembers
  213. its owner (by thread id) and how many times it has been locked. The
  214. last unlock on a mutex removes the system lock and allows other
  215. threads to access the mutex.
  216. *
  217. * Frontend condition mutex functions
  218. * User programs should use these functions.
  219. *
  220. objc_condition_allocate(void), objc_condition_t
  221. Allocate a condition mutex.
  222. Return NULL if error otherwise return condition pointer.
  223. objc_condition_deallocate(objc_condition_t condition), int
  224. Deallocate a condition. Note that this includes an implicit
  225. condition_broadcast to insure that waiting threads have the
  226. opportunity to wake. It is legal to dealloc a condition only
  227. if no other thread is/will be using it. Does NOT check for
  228. other threads waiting but just wakes them up.
  229. Return -1 if error otherwise return 0.
  230. objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex), int
  231. Wait on the condition unlocking the mutex until objc_condition_signal()
  232. or objc_condition_broadcast() are called for the same condition. The
  233. given mutex *must* have the depth 1 so that it can be unlocked
  234. here, for someone else can lock it and signal/broadcast the condition.
  235. The mutex is used to lock access to the shared data that make up the
  236. "condition" predicate.
  237. Return -1 if error otherwise return 0.
  238. objc_condition_broadcast(objc_condition_t condition), int
  239. Wake up all threads waiting on this condition. It is recommended that
  240. the called would lock the same mutex as the threads in
  241. objc_condition_wait before changing the "condition predicate"
  242. and make this call and unlock it right away after this call.
  243. Return -1 if error otherwise return 0.
  244. objc_condition_signal(objc_condition_t condition), int
  245. Wake up one thread waiting on this condition.
  246. Return -1 if error otherwise return 0.
  247. *
  248. * Backend condition mutex functions
  249. * User programs should *NOT* directly call these functions.
  250. *
  251. __gthr_objc_condition_allocate(objc_condition_t condition), int
  252. Allocate a condition mutex, called by objc_condition_allocate.
  253. Return -1 if error otherwise return 0.
  254. __gthr_objc_condition_deallocate(objc_condition_t condition), int
  255. Deallocate a condition, called by objc_condition_deallocate.
  256. Return -1 if error otherwise return 0.
  257. __gthr_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex), int
  258. Wait on the condition, called by objc_condition_wait.
  259. Return -1 if error otherwise return 0 when condition is met.
  260. __gthr_objc_condition_broadcast(objc_condition_t condition), int
  261. Wake up all threads waiting on this condition.
  262. Called by objc_condition_broadcast.
  263. Return -1 if error otherwise return 0.
  264. __gthr_objc_condition_signal(objc_condition_t condition), int
  265. Wake up one thread waiting on this condition.
  266. Called by objc_condition_signal.
  267. Return -1 if error otherwise return 0.