MirOS Manual: lock(9), lockinit(9), lockmgr(9), lockmgr_printinfo(9), lockstatus(9), simple_lock(9), simple_lock_init(9), simple_lock_try(9), simple_unlock(9)

LOCK(9)                       BSD Kernel Manual                        LOCK(9)


     lock, simple_lock_init, simple_lock, simple_lock_try, simple_unlock,
     lockinit, lockmgr, lockstatus, lockmgr_printinfo, - kernel lock functions


     #include <sys/lock.h>

     simple_lock_init(struct simplelock *slock);

     simple_lock(struct simplelock *slock);

     simple_lock_try(struct simplelock *slock);

     simple_unlock(struct simplelock *slock);

     lockinit(struct lock *lock, int prio, const char *wmesg, int timo,
             int flags);

     lockmgr(struct lock *lock, u_int flags, struct simplelock *slock,
             struct proc *p);

     lockstatus(struct lock *lock);

     lockmgr_printinfo(struct lock *lock);


     The lock functions provide synchronisation in the kernel by preventing
     multiple processes from simultaneously executing critical sections of
     code accessing shared data. A number of different locks are available:

     struct simplelock
              Provides a simple spinning mutex. A processor will busy-wait
              while trying to acquire a simplelock. The simplelock operations
              are implemented with machine-dependent locking primitives.

              Simplelocks are usually used only by the high-level lock manager
              and to protect short, critical sections of code. Simplelocks are
              the only locks that can be used inside an interrupt handler. For
              a simplelock to be used in an interrupt handler, care must be
              taken to disable the interrupt, acquire the lock, do any pro-
              cessing, release the simplelock and re-enable the interrupt.
              This procedure is necessary to avoid deadlock between the inter-
              rupt handler and other processes executing on the same proces-

     struct lock
              Provides a high-level lock supporting sleeping/spinning until
              the lock can be acquired. The lock manager supplies both
              exclusive-access and shared-access locks, with recursive
              exclusive-access locks within a single process. It also allows
              upgrading a shared-access lock to an exclusive-access lock, as
              well as downgrading an exclusive-access lock to a shared-access

     If the kernel option LOCKDEBUG is enabled, additional facilities are pro-
     vided to record additional lock information. These facilities are provid-
     ed to assist in determining deadlock occurrences.


     The functions which operate on simplelocks are:

              The simplelock slock is initialised to the unlocked state. A
              statically allocated simplelock also can be initialised with the
              macro SIMPLELOCK_INITIALIZER. The effect is the same as the
              dynamic initialisation by a call to simple_lock_init. For exam-

              struct simplelock slock = SIMPLELOCK_INITIALIZER;

              The simplelock slock is locked. If the simplelock is held then
              execution will spin until the simplelock is acquired. Care must
              be taken that the calling process does not already hold the sim-
              plelock. In this case, the simplelock can never be acquired. If
              kernel option LOCKDEBUG is enabled, a "locking against myself"
              panic will occur.

              Try to acquire the simplelock slock without spinning. If the
              simplelock is held by another process then the return value is
              0. If the simplelock was acquired successfully then the return
              value is 1.

              The simplelock slock is unlocked. The simplelock must be locked
              and the calling process must be the one that last acquired the
              simplelock. If the calling process does not hold the simplelock,
              the simplelock will be released but the kernel behaviour is un-

     The functions which operate on locks are:

     lockinit(lock, prio, wmesg, timo, flags)
              The lock lock is initialised according to the parameters provid-
              ed. Arguments are as follows:

              lock     The lock.

              prio     The process priority when it is woken up after sleeping
                       on the lock.

              wmesg    A sleep message used when a process goes to sleep wait-
                       ing for the lock, so that the exact reason it is sleep-
                       ing can easily be identified.

              timo     The maximum sleep time. Used by tsleep(9).

              flags    Flags to specify the lock behaviour permanently over
                       the lifetime of the lock. Valid lock flags are:

                                Processes should not sleep when attempting to
                                acquire the lock.

                                Processes should sleep, then return failure
                                when acquiring the lock.

                                Processes can acquire the lock recursively.

     lockmgr(lock, flags, slock, p)
              Set, change or release a lock according to the parameters pro-
              vided. Arguments are as follows:

              lock     The lock.

              flags    Flags to specify the lock request type. In addition to
                       the flags specified above, the following flags are

                                Get one of many possible shared-access locks.
                                If a process holding an exclusive-access lock
                                requests a shared-access lock, the exclusive-
                                access lock is downgraded to a shared-access

                                Stop further shared-access locks, when they
                                are cleared, grant a pending upgrade if it ex-
                                ists, then grant an exclusive-access lock.
                                Only one exclusive-access lock may exist at a
                                time, except that a process holding an
                                exclusive-access lock may get additional
                                exclusive-access locks if it explicitly sets
                                the LK_CANRECURSE flag in the lock request, or
                                if the LK_CANRECURSE flag was set when the
                                lock was initialised.

                                The process must hold a shared-access lock
                                that it wants to have upgraded to an
                                exclusive-access lock. Other processes may get
                                exclusive access to the protected resource
                                between the time that the upgrade is requested
                                and the time that it is granted.

                                The process must hold a shared-access lock
                                that it wants to have upgraded to an
                                exclusive-access lock. If the request
                                succeeds, no other processes will have ac-
                                quired exclusive access to the protected
                                resource between the time that the upgrade is
                                requested and the time that it is granted.
                                However, if another process has already re-
                                quested an upgrade, the request will fail.

                                The process must hold an exclusive-access lock
                                that it wants to have downgraded to a shared-
                                access lock. If the process holds multiple
                                (recursive) exclusive-access locks, they will
                                all be downgraded to shared-access locks.

                                Release one instance of a lock.

                                Wait for all activity on the lock to end, then
                                mark it decommissioned. This feature is used
                                before freeing a lock that is part of a piece
                                of memory that is about to be freed.

                                Lock is to be re-enabled after drain. The
                                LK_REENABLE flag may be set only at the
                                release of a lock obtained by a drain.

                                Other locks while we have it OK.

                                Attempt at recursive lock fails.

                       LK_SPIN  Lock spins instead of sleeping.

                                Unlock the simplelock slock when the lock is

              slock    Simplelock interlock. If the flag LK_INTERLOCK is set
                       in flags, slock is a simplelock held by the caller.
                       When the lock lock is acquired, the simplelock is
                       released. If the flag LK_INTERLOCK is not set, slock is

              p        Should always point to the current process curproc.

              Determine the status of lock lock. Returns LK_EXCLUSIVE or
              LK_SHARED for exclusive-access and shared-access locks respec-

              Print out information about state of lock lock.


     Successfully acquired locks return 0. A failed lock attempt always re-
     turns a non-zero error value. No lock is held after an error return (in
     particular, a failed LK_UPGRADE or LK_FORCEUPGRADE will have released its
     shared-access lock). Locks will always succeed unless one of the follow-
     ing is true:

     [EBUSY]       LK_FORCEUPGRADE is requested and some other process has al-
                   ready requested a lock upgrade or LK_NOWAIT is set and a
                   sleep would be required.

     [ENOLCK]      LK_SLEEPFAIL is set and a sleep was done.

     [EINTR]       PCATCH is set in lock priority and a signal arrives to in-
                   terrupt a system call.

     [ERESTART]    PCATCH is set in lock priority and a signal arrives so that
                   the system call is restarted.

                   Non-null lock timeout and timeout expires.


     This section describes places within the OpenBSD source tree where actual
     code implementing or utilising the locking framework can be found. All
     pathnames are relative to /usr/src.

     The locking framework itself is implemented within the file
     sys/kern/kern_lock.c. Data structures and function prototypes for the
     framework are located in sys/sys/lock.h.


     pmap(9), spl(9), tsleep(9), uvm(9)


     The kernel locking API first appeared in 4.4BSD-lite2.

MirOS BSD #10-current           June 23, 2000                                3

Generated on 2017-04-03 16:26:17 by $MirOS: src/scripts/roff2htm,v 1.88 2017/01/29 00:51:06 tg Exp $

These manual pages and other documentation are copyrighted by their respective writers; their source is available at our CVSweb, AnonCVS, and other mirrors. The rest is Copyright © 2002–2017 The MirOS Project, Germany.
This product includes material provided by mirabilos.

This manual page’s HTML representation is supposed to be valid XHTML/1.1; if not, please send a bug report — diffs preferred.