Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ (2) — Plan9 1st Edition

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

fork(2)

LOCK(2)

NAME

lockinit, lock, canlock, unlock − shared memory spin lock

SYNOPSIS

­#include <lock.h>

voidlockinit(void);

voidlock(Lock ∗lk);

intcanlock(Lock ∗lk);

voidunlock(Lock ∗lk);

/∗ Alef only ∗/

adt Lock
{
voidlock(∗Lock);
voidunlock(∗Lock);
intcanlock(∗Lock);
};

adt QLock
{
voidlock(∗Lock);
voidunlock(∗Lock);
intcanlock(∗Lock);
};

adt RWlock
{
voidRlock(∗RWlock);
voidRunlock(∗RWlock);
voidWlock(∗RWlock);
voidWunlock(∗RWlock);
};

adt Ref
{
intinc(∗Ref);
intdec(∗Ref);
intref(∗Ref);
};

DESCRIPTION

These routines are used by processes sharing memory to synchronize using spin locks.  ­Lockinit must be called before the first use of the other routines.  ­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. 

Alef

Alef locks have similar functionality, but no special initialization is required.  The ADT ­Lock has functions lock, unlock, and canlock, just like locks in C. ­QLocks have the same interface but are not spin locks; instead if the lock is taken ­QLock.lock will suspend execution of the calling task until it is released. 

Although ­Locks are the more primitive lock, their use is discouraged and even erroneous for most purposes.  For example, ­Locks cannot synchronize between tasks in the same proc.  Use ­QLocks instead. 

­RWlocks manage access to a data structure that has distinct readers and writers.  ­RWlock.Rlock grants read access; ­RWlock.Runlock releases it.  ­RWlock.Wlock grants write access; ­RWlock.Wunlock releases it.  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. 

­Refs manage reference counters.  ­Ref.inc increments the counter and returns the old value; ­Ref.dec decrements the counter and returns the new value.  ­Ref.ref returns the current value. 

SOURCE

­/sys/src/liblock

SEE ALSO

­rfork in fork(2)

Plan 9  —  April 04, 1995

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026