annotate libobjc/THREADS @ 158:494b0b89df80 default tip

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