NAME
lock, canlock, unlock, qlock, canqlock, qunlock, rlock, canrlock,
runlock, wlock, canwlock, wunlock, rsleep, rwakeup, rwakeupall,
incref, decref – spin locks, queueing rendezvous locks, reader–writer
locks, rendezvous points, and reference counts |
SYNOPSIS
#include <u.h> #include <libc.h>
void lock(Lock *l)
void qlock(QLock *l)
void rlock(RWLock *l)
void wlock(RWLock *l)
typedef struct Rendez {
void rsleep(Rendez *r)
#include <thread.h>
typedef struct Ref {
void incref(Ref*) |
DESCRIPTION
These routines are used to synchronize processes sharing memory.
Locks are spin locks, QLocks and RWLocks are different types of queueing rendezvous locks, and Rendezes are rendezvous points. Locks and rendezvous points work in regular programs as well as programs that use the thread library (see thread(2)). The thread library replaces the rendezvous(2) system call with its own implementation, threadrendezvous, so that threads as well as processes may be synchronized by locking calls in threaded programs. Used carelessly, spin locks can be expensive and can easily generate deadlocks. Their use is discouraged, especially in programs that use the thread library because they prevent context switches between threads. Lock blocks until the lock has been obtained. Canlock is non–blocking. It tries to obtain a lock and returns a non–zero value if it was successful, 0 otherwise. Unlock releases a lock. QLocks have the same interface but are not spin locks; instead if the lock is taken qlock will suspend execution of the calling task until it is released. Although Locks are the more primitive lock, they have limitations; for example, they cannot synchronize between tasks in the same proc. Use QLocks instead. RWLocks manage access to a data structure that has distinct readers and writers. Rlock grants read access; runlock releases it. Wlock grants write access; wunlock releases it. Canrlock and canwlock are the non–blocking versions. There may be any number of simultaneous readers, but only one writer. Moreover, if write access is granted no one may have read access until write access is released. All types of lock should be initialized to all zeros before use; this puts them in the unlocked state. Rendezes are rendezvous points. Each Rendez r is protected by a QLock r–>l, which must be held by the callers of rsleep, rwakeup, and rwakeupall. Rsleep atomically releases r–>l and suspends execution of the calling task. After resuming execution, rsleep will reacquire r–>l before returning. If any processes are sleeping on r, rwakeup wakes one of them. it returns 1 if a process was awakened, 0 if not. Rwakeupall wakes all processes sleeping on r, returning the number of processes awakened. Rwakeup and rwakeupall do not release r–>l and do not suspend execution of the current task. Before use, Rendezes should be initialized to all zeros except for r–>l pointer, which should point at the QLock that will guard r. It is important that this QLock is the same one that protects the rendezvous condition; see the example.
A Ref contains a long that can be incremented and decremented
atomically: Incref increments the Ref in one atomic operation.
Decref atomically decrements the Ref and returns zero if the resulting
value is zero, non–zero otherwise. |
EXAMPLE
Implement a buffered single–element channel using rsleep and rwakeup:
|
SOURCE
/sys/src/libc/port/lock.c /sys/src/libc/9sys/qlock.c /sys/src/libthread/ref.c |
SEE ALSO
rfork in fork(2), semacquire(2) |
BUGS
Locks are not actually spin locks. After one unsuccessful attempt,
lock calls semacquire repeatedly (thus yielding the CPU) until
it succeeds in acquiring a semaphore internal to the Lock. Locks
are not intended to be held for long periods of time. As discussed
above, if a lock is to be held for much more than a few
instructions, the queueing lock types should be almost always
be used.
It is an error for a program to fork when it holds a lock in shared
memory, since this will result in two processes holding the same
lock at the same time, which should not happen. |