5-51
Implementing Classes in SOM
5.11 Customizing Mutual Exclusion Services (Thread Safety)
The SOM kernel and the other SOMobjects frameworks (DSOM, Persistence, Replication, and
so on), have been made thread safe with respect to multi-threaded processes. As used here,
“thread safe” means that the SOMobjects run time has been implemented using critical sec-
tions to protect sections of the code which must only be executed by a single thread in a
multi-threaded application process at one time. Critical sections in SOM are implemented using
mutual exclusion semaphores, or “mutex semaphores,” designed to block individual threads
(as opposed to an entire process).
Some operating systems provide native multi-threading (for example, OS/2 and AIX 4.1). On
other operating systems that do not support native multi-threading (such as, AIX 3.2), thread
support may be provided as part of particular programming environments (like DCE) or li-
braries.
It is vital that SOM critical sections employ the mutex services that are provided by the thread
package used by the application. Consequently, SOM provides a mechanism for defining and
customizing mutex services.
Five mutex service functions are used to implement critical sections in SOM. These functions
are called indirectly via the global pointer variables defined below. A somToken parameter
(called “sem” below) is used as a generic “handle” to refer to a mutex semaphore — usually it is a
pointer to a mutex semaphore variable or data structure. The actual representation of the mutex
semaphore is hidden by the functions.
unsigned long (*SOMCreateMutexSem)(somToken *sem);
The referenced function creates a mutex semaphore, whose handle is returned as an
output parameter in the somToken variable, “sem”.
If the call succeeds, a 0 is returned. Otherwise, a non-zero error code is returned.
unsigned long (*SOMRequestMutexSem)(somToken sem);
The referenced function requests ownership of the mutex semaphore identified by the
parameter, sem. If the semaphore is not currently owned by another thread, ownership is
assigned to the calling thread. Otherwise, the calling thread is blocked until the sema-
phore is released by the current owner.
Important: If the same thread calls SOMRequestMutexSem multiple times, a reference
count must be kept, so that the semaphore is released only after the same number of
calls to SOMReleaseMutexSem. Some, but not all, thread packages provide refer-
ence counting automatically, via “counting semaphores.”
If the call succeeds, a 0 is returned. Otherwise, a non-zero error code is returned.
unsigned long (*SOMReleaseMutexSem)(somToken sem);
The referenced function releases ownership of the mutex semaphore identified by the
parameter, sem.
Important: If the same thread calls SOMRequestMutexSem multiple times, a reference
count must be kept, so that the semaphore is released only after the same number of
calls to SOMReleaseMutexSem. Some, but not all, thread packages provide refer-
ence counting automatically, via “counting semaphores.”
If the call succeeds, a 0 is returned. Otherwise, a non-zero error code is returned.
unsigned long (*SOMDestroyMutexSem)(somToken sem);
The referenced function destroys the a mutex semaphore identified by the parameter,
sem.
If the call succeeds, a 0 is returned. Otherwise, a non-zero error code is returned.
unsigned long (*SOMGetThreadId)();
Comentarios a estos manuales