TESTANDSET(3C-SVR3)RISC/os Reference ManualESTANDSET(3C-SVR3)
NAME
test_and_set, test_then_add, add_then_test - atomic opera-
tions on integer variables
SYNOPSIS
#include <mutex.h>
int testandset (addr,v)
int *addr, v;
int testthenadd (addr,v)
int *addr, v;
int addthentest (addr,v)
int *addr, v;
DESCRIPTION
test_and_set, test_then_add and add_then_test perform atomic
operations on integer variables. These functions are
included in a library of mutual exclusion primitives
/usr/lib/libc/mutex/libmutex.a .
test_and_set atomically sets the value of the integer vari-
able pointed to by addr to v and returns its previous value.
I. test_and_set can be used to implement binary semaphores.
test_then_add atomically adds v to the values of the integer
variable pointed to by addr and returns its value prior to
the operation. add_then_test atomically adds v to the value
of the integer pointed to by addr and returns its value
after performing the operation. These functions are useful
for building counting semaphores.
On systems using the mips2 instruction set these functions
take advantage of the ll/sc instructions. On systems using
the mips1 instruction set the mutex library uses monitors
based on Lamport's fast mutual exclusion algorithm. The data
structures used by the monitors are allocated in a shared
memory region transparently to the application. Mutex moni-
tor regions are identified by unique id's derived from the
userid or from the environment variable MUTEXID if present.
Unrelated applications therefore do not compete for moni-
tors. The mutex library is configured by default to use 31
monitors. This may be overridden by setting the environment
variable MUTEXMONITORS to any value in the range 7 to 257.
Use of larger values may improve performance by reducing
monitor contention at the expense of increased memory usage.
It is recommended that prime numbers be used as values for
MUTEXMONITORS .
All known software mutual exclusion algorithms require the
use of internal process id's and Lamport's is no exception.
Printed 11/19/92 Page 1
TESTANDSET(3C-SVR3)RISC/os Reference ManualESTANDSET(3C-SVR3)
The mutex library default configuration allows up to 64
processes to share a mutex region. The library may be recon-
figured if needed by setting the environment variable
MUTEXPROCESSES to an appropriate value not exceeding 1024.
CAVEAT
Since mutex regions are created by the first process calling
mutex library functions in a group of related applications,
only its environment may be used to configure the number of
processes or mutex monitors.
Page 2 Printed 11/19/92