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> int canlock(Lock *l) void unlock(Lock *l) int canqlock(QLock *l) void qunlock(QLock *l) int canrlock(RWLock *l) void runlock(RWLock *l) int canwlock(RWLock *l) void wunlock(RWLock *l)
int rwakeup(Rendez *r) int rwakeupall(Rendez *r)
long decref(Ref*) |
DESCRIPTION
|
These routines are used to synchronize processes sharing memory.
|
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) |
BUGS
|
Locks are not strictly spin locks. After each unsuccessful attempt,
lock calls sleep(0) to yield the CPU; this handles the common
case where some other process holds the lock. After a thousand
unsuccessful attempts, lock sleeps for 100ms between attempts.
After another thousand unsuccessful attempts, lock
sleeps for a full second between attempts. Locks are not intended
to be held for long periods of time. The 100ms and full second
sleeps are only heuristics to avoid tying up the CPU when a process
deadlocks. 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.
|