Initialization of synchronization mechanisms
Some general characteristics of the initialization functions are:
-
The first argument is a pointer to the locking structure to
be initialized.
-
The type argument can take on the values of either:
USYNC_THREAD-
thread-to-thread synchronization
USYNC_PROCESS-
interprocess synchronization.
For such use, the synchronization data structures must reside
in memory that is shared between the processes,
using either IPC shared memory or the mapped file feature.
The type argument is not available for the two spinning type locks.
-
Two mechanism types (barriers and semaphore) require an initial value (count).
-
The last argument is of type (void *),
is reserved for future use, and
should be set to NULL for future compatibility.
The syntax of these functions is given below.
int sema_init( sema_t *sema, int count, int type, void *arg);
int barrier_init( barrier_t *barrier, int count, int type, void *arg);
int _barrier_spin_init( barrier_spin_t *barrier, int count, void *arg);
int _spin_init( spin_t *lock, void *arg);
int cond_init( cond_t *cond, int type, void *arg);
int mutex_init( mutex_t *mutex, int type, void *arg);
int rmutex_init( rmutex_t *rmutex, int type, void *arg);
int rwlock_init( rwlock_t *rwlock, int type, void *arg);
Alternative initialization
In this implementation,
it is valid to use statically initialized (zero-filled) data structures
for the synchronization mechanisms.
For most of the mechanisms, a zero-filled data structure
is taken to be unlocked and of type USYNC_THREAD.
The mechanisms that take a count argument have the following
additional interpretations:
-
A zero-filled sema_t structure represents zero available resources.
A
sema_wait(3synch)
on that structure will block.
-
A zero-filled barrier_t structure is valid but meaningless.
Filling the data structure with zeroes is not
recommended for re-initialization of synchronization structures.
In general,
it is incorrect to re-initialize a synchronization structure while in use.
Some of the initialization functions (shown above) return EBUSY
if called for an active data structure
(one on which threads are blocked).
Zero-filling the data structure bypasses that check.
Next topic:
Invalidation of synchronization mechanisms
Previous topic:
Further considerations for synchronization mechanisms
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004