| [2] | 1 | This file describes in little detail the modifications to the
|
|---|
| 2 | Objective-C runtime needed to make it thread safe.
|
|---|
| 3 |
|
|---|
| 4 | First off, kudos to Galen Hunt who is the author of this great work.
|
|---|
| 5 |
|
|---|
| 6 | If you have an comments or just want to know where to
|
|---|
| 7 | send me money to express your undying gratitude for threading the
|
|---|
| 8 | Objective-C runtime you can reach Galen at:
|
|---|
| 9 |
|
|---|
| 10 | [email protected]
|
|---|
| 11 |
|
|---|
| 12 | Any questions, comments, bug reports, etc. should send email either to the
|
|---|
| 13 | GCC bug account or to:
|
|---|
| 14 |
|
|---|
| 15 | Scott Christley <[email protected]>
|
|---|
| 16 |
|
|---|
| 17 | * Sarray Threading:
|
|---|
| 18 |
|
|---|
| 19 | The most critical component of the Objective-C runtime is the sparse array
|
|---|
| 20 | structure (sarray). Sarrays store object selectors and implementations.
|
|---|
| 21 | Following in the tradition of the Objective-C runtime, my threading
|
|---|
| 22 | support assumes that fast message dispatching is far more important
|
|---|
| 23 | than *ANY* and *ALL* other operations. The message dispatching thus
|
|---|
| 24 | uses *NO* locks on any kind. In fact, if you look in sarray.h, you
|
|---|
| 25 | will notice that the message dispatching has not been modified.
|
|---|
| 26 | Instead, I have modified the sarray management functions so that all
|
|---|
| 27 | updates to the sarray data structure can be made in parallel will
|
|---|
| 28 | message dispatching.
|
|---|
| 29 |
|
|---|
| 30 | To support concurrent message dispatching, no dynamically allocated
|
|---|
| 31 | sarray data structures are freed while more than one thread is
|
|---|
| 32 | operational. Sarray data structures that are no longer in use are
|
|---|
| 33 | kept in a linked list of garbage and are released whenever the program
|
|---|
| 34 | is operating with a single thread. The programmer can also flush the
|
|---|
| 35 | garbage list by calling sarray_remove_garbage when the programmer can
|
|---|
| 36 | ensure that no message dispatching is taking place concurrently. The
|
|---|
| 37 | amount of un-reclaimed sarray garbage should normally be extremely
|
|---|
| 38 | small in a real program as sarray structures are freed only when using
|
|---|
| 39 | the "poseAs" functionality and early in program initialization, which
|
|---|
| 40 | normally occurs while the program is single threaded.
|
|---|
| 41 |
|
|---|
| 42 | ******************************************************************************
|
|---|
| 43 | * Static Variables:
|
|---|
| 44 |
|
|---|
| 45 | The following variables are either statically or globally defined. This list
|
|---|
| 46 | does not include variables which are internal to implementation dependent
|
|---|
| 47 | versions of thread-*.c.
|
|---|
| 48 |
|
|---|
| 49 | The following threading designations are used:
|
|---|
| 50 | SAFE : Implicitly thread safe.
|
|---|
| 51 | SINGLE : Must only be used in single thread mode.
|
|---|
| 52 | MUTEX : Protected by single global mutex objc_runtime_mutex.
|
|---|
| 53 | UNUSED : Not used in the runtime.
|
|---|
| 54 |
|
|---|
| 55 | Variable Name: Usage: Defined: Also used in:
|
|---|
| 56 | =========================== ====== ============ =====================
|
|---|
| 57 | __objc_class_hash MUTEX class.c
|
|---|
| 58 | __objc_class_links_resolved UNUSED class.c runtime.h
|
|---|
| 59 | __objc_class_number MUTEX class.c
|
|---|
| 60 | __objc_dangling_categories UNUSED init.c
|
|---|
| 61 | __objc_module_list MUTEX init.c
|
|---|
| 62 | __objc_selector_array MUTEX selector.c
|
|---|
| 63 | __objc_selector_hash MUTEX selector.c
|
|---|
| 64 | __objc_selector_max_index MUTEX selector.c sendmsg.c runtime.h
|
|---|
| 65 | __objc_selector_names MUTEX selector.c
|
|---|
| 66 | __objc_thread_exit_status SAFE thread.c
|
|---|
| 67 | __objc_uninstalled_dtable MUTEX sendmsg.c selector.c
|
|---|
| 68 | _objc_load_callback SAFE init.c objc-api.h
|
|---|
| 69 | _objc_lookup_class SAFE class.c objc-api.h
|
|---|
| 70 | _objc_object_alloc SINGLE objects.c objc-api.h
|
|---|
| 71 | _objc_object_copy SINGLE objects.c objc-api.h
|
|---|
| 72 | _objc_object_dispose SINGLE objects.c objc-api.h
|
|---|
| 73 | frwd_sel SAFE2 sendmsg.c
|
|---|
| 74 | idxsize MUTEX sarray.c sendmsg.c sarray.h
|
|---|
| 75 | initialize_sel SAFE2 sendmsg.c
|
|---|
| 76 | narrays MUTEX sarray.c sendmsg.c sarray.h
|
|---|
| 77 | nbuckets MUTEX sarray.c sendmsg.c sarray.h
|
|---|
| 78 | nindices MUTEX sarray.c sarray.h
|
|---|
| 79 | previous_constructors SAFE1 init.c
|
|---|
| 80 | proto_class SAFE1 init.c
|
|---|
| 81 | unclaimed_categories MUTEX init.c
|
|---|
| 82 | unclaimed_proto_list MUTEX init.c
|
|---|
| 83 | uninitialized_statics MUTEX init.c
|
|---|
| 84 |
|
|---|
| 85 | Notes:
|
|---|
| 86 | 1) Initialized once in unithread mode.
|
|---|
| 87 | 2) Initialized value will always be same, guaranteed by lock on selector
|
|---|
| 88 | hash table.
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | ******************************************************************************
|
|---|
| 92 | * Frontend/Backend design:
|
|---|
| 93 |
|
|---|
| 94 | The design of the Objective-C runtime thread and mutex functions utilizes a
|
|---|
| 95 | frontend/backend implementation.
|
|---|
| 96 |
|
|---|
| 97 | The frontend, as characterized by the files thr.h and thr.c, is a set
|
|---|
| 98 | of platform independent structures and functions which represent the
|
|---|
| 99 | user interface. Objective-C programs should use these structures and
|
|---|
| 100 | functions for their thread and mutex work if they wish to maintain a
|
|---|
| 101 | high degree of portability across platforms.
|
|---|
| 102 |
|
|---|
| 103 | The backend is composed of a file with the necessary code to map the ObjC
|
|---|
| 104 | thread and mutex to a platform specific implementation. For example, the
|
|---|
| 105 | file thr-solaris.c contains the implementation for Solaris.
|
|---|
| 106 |
|
|---|
| 107 | If you are compiling libobjc as part of GCC, the thr-objc.c backend is
|
|---|
| 108 | always used; this backend uses GCC's gthread code. The thread system
|
|---|
| 109 | is automatically configured when GCC is configured. Important: make
|
|---|
| 110 | sure you configure GCC using `--enable-threads' if you want threads !
|
|---|
| 111 |
|
|---|
| 112 | If you want to compile libobjc standalone, then you would need to
|
|---|
| 113 | modify the configure.in and makefiles for it; and you need to pick an
|
|---|
| 114 | appropriate backend file for the target platform; you make this choice
|
|---|
| 115 | by assigning the OBJC_THREAD_FILE make variable to the basename of the
|
|---|
| 116 | backend file. For example, OBJC_THREAD_FILE=thr-posix would indicate
|
|---|
| 117 | that the generic posix backend file, thr-posix.c, should be compiled
|
|---|
| |
|---|