OSF DCE Application Development Guide--Core Components

Programming with Threads
without interference. However, this is difficult to do successfully because the function
may be called by many libraries. A global lock solves this problem by providing a
universal lock. Any code that calls any nonreentrant function uses the same lock.
To lock a global lock, call the pthread_lock_global_np( ) routine. To unlock a global
lock, call the pthread_unlock_global_np( ) routine.
Note: Many COBOL and FORTRAN compilers generate inherently nonreentrant
code. Many C, Ada, Pascal, and BLISS compilers generate reentrant code
by default. It is possible to write nonreentrant code in the reentrant
languages by not following a locking protocol.
8.4.2 Thread-Specific Storage
To avoid nonreentrancy when writing new software, avoid using global variables to
store data that is thread-specific data.
Alternatively, allocate thread-specific data on the stack or heap and explicitly pass its
address to called routines.
8.5 Avoiding Priority Inversion
Priority inversion occurs when interaction among three or more threads blocks the
highest-priority thread from executing. For example, a high-priority thread waits for a
resource locked by a low-priority thread, and the low-priority thread waits while a
middle-priority thread executes. The high-priority thread is made to wait while a thread
of lower priority (the middle-priority thread) executes.
To avoid priority inversion, associate a priority with each resource and force any thread
using that object to first raise its priority to that associated with the object. This method
of avoiding priority inversion is not a complete solution because all threads will then
block at the same ceiling priority and be unblocked in FIFO order rather than by their
actual priority.
The SCHED_OTHER (default) scheduling policy prevents priority inversion from
causing a complete blockage of the high-priority thread because the low-priority thread
is permitted to execute and release the resource. The SCHED_FIFO and SCHED_RR
policies, however, do not force resumption of the low-priority thread if the middle-
priority thread executes indefinitely.
8.6 Using Synchronization Objects
124245 Tandem Computers Incorporated 8 11