URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libobjc/] [THREADS] - Rev 739
Compare with Previous | Blame | View Log
This file describes in little detail the modifications to theObjective-C runtime needed to make it thread safe.First off, kudos to Galen Hunt who is the author of this great work.If you have an comments or just want to know where tosend me money to express your undying gratitude for threading theObjective-C runtime you can reach Galen at:gchunt@cs.rochester.eduAny questions, comments, bug reports, etc. should send email either to theGCC bug account or to:Scott Christley <scottc@net-community.com>* Sarray Threading:The most critical component of the Objective-C runtime is the sparse arraystructure (sarray). Sarrays store object selectors and implementations.Following in the tradition of the Objective-C runtime, my threadingsupport assumes that fast message dispatching is far more importantthan *ANY* and *ALL* other operations. The message dispatching thususes *NO* locks on any kind. In fact, if you look in sarray.h, youwill notice that the message dispatching has not been modified.Instead, I have modified the sarray management functions so that allupdates to the sarray data structure can be made in parallel willmessage dispatching.To support concurrent message dispatching, no dynamically allocatedsarray data structures are freed while more than one thread isoperational. Sarray data structures that are no longer in use arekept in a linked list of garbage and are released whenever the programis operating with a single thread. The programmer can also flush thegarbage list by calling sarray_remove_garbage when the programmer canensure that no message dispatching is taking place concurrently. Theamount of un-reclaimed sarray garbage should normally be extremelysmall in a real program as sarray structures are freed only when usingthe "poseAs" functionality and early in program initialization, whichnormally occurs while the program is single threaded.******************************************************************************* Static Variables:The following variables are either statically or globally defined. This listdoes not include variables which are internal to implementation dependentversions of thread-*.c.The following threading designations are used:SAFE : Implicitly thread safe.SINGLE : Must only be used in single thread mode.MUTEX : Protected by single global mutex objc_runtime_mutex.UNUSED : Not used in the runtime.Variable Name: Usage: Defined: Also used in:=========================== ====== ============ =====================__objc_class_hash MUTEX class.c__objc_class_links_resolved UNUSED class.c runtime.h__objc_class_number MUTEX class.c__objc_dangling_categories UNUSED init.c__objc_module_list MUTEX init.c__objc_selector_array MUTEX selector.c__objc_selector_hash MUTEX selector.c__objc_selector_max_index MUTEX selector.c sendmsg.c runtime.h__objc_selector_names MUTEX selector.c__objc_thread_exit_status SAFE thread.c__objc_uninstalled_dtable MUTEX sendmsg.c selector.c_objc_load_callback SAFE init.c objc-api.h_objc_lookup_class SAFE class.c objc-api.h_objc_object_alloc SINGLE objects.c objc-api.h_objc_object_copy SINGLE objects.c objc-api.h_objc_object_dispose SINGLE objects.c objc-api.hfrwd_sel SAFE2 sendmsg.cidxsize MUTEX sarray.c sendmsg.c sarray.hinitialize_sel SAFE2 sendmsg.cnarrays MUTEX sarray.c sendmsg.c sarray.hnbuckets MUTEX sarray.c sendmsg.c sarray.hnindices MUTEX sarray.c sarray.hprevious_constructors SAFE1 init.cproto_class SAFE1 init.cunclaimed_categories MUTEX init.cunclaimed_proto_list MUTEX init.cuninitialized_statics MUTEX init.cNotes:1) Initialized once in unithread mode.2) Initialized value will always be same, guaranteed by lock on selectorhash table.******************************************************************************* Frontend/Backend design:The design of the Objective-C runtime thread and mutex functions utilizes afrontend/backend implementation.The frontend, as characterized by the files thr.h and thr.c, is a setof platform independent structures and functions which represent theuser interface. For example, objc_mutex_lock(). Objective-C programsshould use these structures and functions for their thread and mutexwork if they wish to maintain a high degree of portability acrossplatforms.The backend is currently GCC's gthread code (gthr.h and related). Forexample, __gthread_objc_mutex_lock(). The thread system isautomatically configured when GCC is configured. On most platformsthis thread backend is able to automatically switch to non-multi-threadedmode if the threading library is not linked in.If you want to compile libobjc standalone, then you would need to modifythe configure.in and makefiles for it and you need to import thegthread code from GCC.******************************************************************************* Threads:The thread system attempts to create multiple threads using whateveroperating system or library thread support is available. It doesassume that all system functions are thread safe. Notably this meansthat the system implementation of malloc and free must be thread safe.If a system has multiple processors, the threads are configured forfull parallel processing.* Backend initialization functions__objc_init_thread_system(void), intInitialize the thread subsystem. Called once by __objc_exec_class.Return -1 if error otherwise return 0.__objc_close_thread_system(void), intCloses the thread subsystem, not currently guaranteed to be called.Return -1 if error otherwise return 0.****** Frontend thread functions* User programs should use these functions.objc_thread_detach(SEL selector, id object, id argument), objc_thread_tCreates and detaches a new thread. The new thread starts bysending the given selector with a single argument to thegiven object.objc_thread_set_priority(int priority), intSets a thread's relative priority within the program. Validoptions are:OBJC_THREAD_INTERACTIVE_PRIORITYOBJC_THREAD_BACKGROUND_PRIORITYOBJC_THREAD_LOW_PRIORITYobjc_thread_get_priority(void), intQuery a thread's priority.objc_thread_yield(void), voidYields processor to another thread with equal or higherpriority. It is up to the system scheduler to determine ifthe processor is taken or not.objc_thread_exit(void), intTerminates a thread. If this is the last thread executingthen the program will terminate.objc_thread_id(void), intReturns the current thread's id.objc_thread_set_data(void *value), intSet a pointer to the thread's local storage. Local storage isthread specific.objc_thread_get_data(void), void *Returns the pointer to the thread's local storage.****** Backend thread functions* User programs should *NOT* directly call these functions.__gthr_objc_thread_detach(void (*func)(void *arg), void *arg), objc_thread_tSpawns a new thread executing func, called by objc_thread_detach.Return NULL if error otherwise return thread id.__gthr_objc_thread_set_priority(int priority), intSet the thread's priority, called by objc_thread_set_priority.Return -1 if error otherwise return 0.__gthr_objc_thread_get_priority(void), intQuery a thread's priority, called by objc_thread_get_priority.Return -1 if error otherwise return the priority.__gthr_objc_thread_yield(void), voidYields the processor, called by objc_thread_yield.__gthr_objc_thread_exit(void), intTerminates the thread, called by objc_thread_exit.Return -1 if error otherwise function does not return.__gthr_objc_thread_id(void), objc_thread_tReturns the current thread's id, called by objc_thread_id.Return -1 if error otherwise return thread id.__gthr_objc_thread_set_data(void *value), intSet pointer for thread local storage, called by objc_thread_set_data.Returns -1 if error otherwise return 0.__gthr_objc_thread_get_data(void), void *Returns the pointer to the thread's local storage.Returns NULL if error, called by objc_thread_get_data.******************************************************************************* Mutexes:Mutexes can be locked recursively. Each locked mutex remembersits owner (by thread id) and how many times it has been locked. Thelast unlock on a mutex removes the system lock and allows otherthreads to access the mutex.****** Frontend mutex functions* User programs should use these functions.objc_mutex_allocate(void), objc_mutex_tAllocates a new mutex. Mutex is initially unlocked.Return NULL if error otherwise return mutex pointer.objc_mutex_deallocate(objc_mutex_t mutex), intFree a mutex. Before freeing the mutex, makes sure that noone else is using it.Return -1 if error otherwise return 0.objc_mutex_lock(objc_mutex_t mutex), intLocks a mutex. As mentioned earlier, the same thread may callthis routine repeatedly.Return -1 if error otherwise return 0.objc_mutex_trylock(objc_mutex_t mutex), intAttempts to lock a mutex. If lock on mutex can be acquiredthen function operates exactly as objc_mutex_lock.Return -1 if failed to acquire lock otherwise return 0.objc_mutex_unlock(objc_mutex_t mutex), intUnlocks the mutex by one level. Other threads may not acquirethe mutex until this thread has released all locks on it.Return -1 if error otherwise return 0.****** Backend mutex functions* User programs should *NOT* directly call these functions.__gthr_objc_mutex_allocate(objc_mutex_t mutex), intAllocates a new mutex, called by objc_mutex_allocate.Return -1 if error otherwise return 0.__gthr_objc_mutex_deallocate(objc_mutex_t mutex), intFree a mutex, called by objc_mutex_deallocate.Return -1 if error otherwise return 0.__gthr_objc_mutex_lock(objc_mutex_t mutex), intLocks a mutex, called by objc_mutex_lock.Return -1 if error otherwise return 0.__gthr_objc_mutex_trylock(objc_mutex_t mutex), intAttempts to lock a mutex, called by objc_mutex_trylock.Return -1 if failed to acquire lock or error otherwise return 0.__gthr_objc_mutex_unlock(objc_mutex_t mutex), intUnlocks the mutex, called by objc_mutex_unlock.Return -1 if error otherwise return 0.******************************************************************************* Condition Mutexes:Mutexes can be locked recursively. Each locked mutex remembersits owner (by thread id) and how many times it has been locked. Thelast unlock on a mutex removes the system lock and allows otherthreads to access the mutex.** Frontend condition mutex functions* User programs should use these functions.*objc_condition_allocate(void), objc_condition_tAllocate a condition mutex.Return NULL if error otherwise return condition pointer.objc_condition_deallocate(objc_condition_t condition), intDeallocate a condition. Note that this includes an implicitcondition_broadcast to insure that waiting threads have theopportunity to wake. It is legal to dealloc a condition onlyif no other thread is/will be using it. Does NOT check forother threads waiting but just wakes them up.Return -1 if error otherwise return 0.objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex), intWait on the condition unlocking the mutex until objc_condition_signal()or objc_condition_broadcast() are called for the same condition. Thegiven mutex *must* have the depth 1 so that it can be unlockedhere, for someone else can lock it and signal/broadcast the condition.The mutex is used to lock access to the shared data that make up the"condition" predicate.Return -1 if error otherwise return 0.objc_condition_broadcast(objc_condition_t condition), intWake up all threads waiting on this condition. It is recommended thatthe called would lock the same mutex as the threads inobjc_condition_wait before changing the "condition predicate"and make this call and unlock it right away after this call.Return -1 if error otherwise return 0.objc_condition_signal(objc_condition_t condition), intWake up one thread waiting on this condition.Return -1 if error otherwise return 0.** Backend condition mutex functions* User programs should *NOT* directly call these functions.*__gthr_objc_condition_allocate(objc_condition_t condition), intAllocate a condition mutex, called by objc_condition_allocate.Return -1 if error otherwise return 0.__gthr_objc_condition_deallocate(objc_condition_t condition), intDeallocate a condition, called by objc_condition_deallocate.Return -1 if error otherwise return 0.__gthr_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex), intWait on the condition, called by objc_condition_wait.Return -1 if error otherwise return 0 when condition is met.__gthr_objc_condition_broadcast(objc_condition_t condition), intWake up all threads waiting on this condition.Called by objc_condition_broadcast.Return -1 if error otherwise return 0.__gthr_objc_condition_signal(objc_condition_t condition), intWake up one thread waiting on this condition.Called by objc_condition_signal.Return -1 if error otherwise return 0.
