---
name: "lock"
section: "9"
platform: "linux"
package: "freebsd-manpages"
version: "6.0-5"
release: "Debian 4.0 (Etch)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/lock/9"
---

# lock(9)

lockmgr family of functions

## NAME

**lockinit**, **lockdestroy**, **lockcount**, **lockmgr**, **lockstatus**, **lockmgr\_printinfo** – lockmgr family of functions

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sys/lockmgr.h\>**

*void*\

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

*void*\

**lockdestroy**(*struct lock \*lkp*);

*int*\

**lockcount**(*struct lock \*lkp*);

*int*\

**lockmgr**(*struct lock \*lkp*, *u\_int flags*, *struct mtx \*interlkp*, *struct thread \*td*);

*int*\

**lockstatus**(*struct lock \*lkp*, *struct thread \*td*);

*void*\

**lockmgr\_printinfo**(*struct lock \*lkp*);

## DESCRIPTION

The **lockinit**() function is used to initialize a lock. It must be called before any operation can be performed on a lock. Its arguments are:

- *lkp*

  A pointer to the lock to initialize.

- *prio*

  The priority passed to [msleep(9)](/msleep/9.md).

- *wmesg*

  The lock message. This is used for both debugging output and [msleep(9)](/msleep/9.md).

- *timo*

  The timeout value passed to [msleep(9)](/msleep/9.md).

- *flags*

  The flags the lock is to be initialized with.

  - LK\_NOWAIT

    Do not sleep while acquiring the lock.

  - LK\_SLEEPFAIL

    Fail after a sleep.

  - LK\_CANRECURSE

    Allow recursive exclusive locks.

  - LK\_REENABLE

    Re-enable the lock after a drain.

  - LK\_NOPAUSE

    Disable the spinlock while acquiring the lock.

  - LK\_TIMELOCK

    Use *timo* during a sleep; otherwise, 0 is used.

The **lockdestroy**() function is used to destroy a lock, and while it is called in a number of places in the kernel, it currently does nothing.

The **lockcount**() function returns a count of the number of exclusive locks and shared locks held against the lock *lkp*.

The **lockmgr**() function handles general locking functionality within the kernel, including support for shared and exclusive locks, and recursion. **lockmgr**() is also able to upgrade and downgrade locks.

Its arguments are:

- *lkp*

  A pointer to the lock to manipulate.

- *flags*

  Flags indicating what action is to be taken.

  - LK\_SHARED

    Acquire a shared lock. If an exclusive lock is currently held, it will be downgraded.

  - LK\_EXCLUSIVE

    Acquire an exclusive lock. If an exclusive lock is already held, and LK\_CANRECURSE is not set, the system will [panic(9)](/panic/9.md).

  - LK\_DOWNGRADE

    Downgrade exclusive lock to a shared lock. Downgrading a shared lock is not permitted. If an exclusive lock has been recursed, all references will be downgraded.

  - LK\_EXCLUPGRADE

    Upgrade a shared lock to an exclusive lock. Fails with EBUSY if there is someone ahead of you in line waiting for an upgrade. If this call fails, the shared lock is lost. Attempts to upgrade an exclusive lock will cause a [panic(9)](/panic/9.md).

  - LK\_UPGRADE

    Upgrade a shared lock to an exclusive lock. If this call fails, the shared lock is lost. Attempts to upgrade an exclusive lock will cause a [panic(9)](/panic/9.md).

  - LK\_RELEASE

    Release the lock. Releasing a lock that is not held can cause a [panic(9)](/panic/9.md).

  - LK\_DRAIN

    Wait for all activity on the lock to end, then mark it decommissioned. This is used before freeing a lock that is part of a piece of memory that is about to be freed. (As documented in ⟨sys/lockmgr.h⟩.)

  - LK\_SLEEPFAIL

    Fail if operation has slept.

  - LK\_NOWAIT

    Do not allow the call to sleep. This can be used to test the lock.

  - LK\_CANRECURSE

    Allow recursion on an exclusive lock. For every lock there must be a release.

  - LK\_INTERLOCK

    Unlock the interlock (which should be locked already).

- *interlkp*

  An interlock mutex for controlling group access to the lock. If LK\_INTERLOCK is specified, **lockmgr**() assumes *interlkp* is currently owned and not recursed, and will return it unlocked. See [mtx\_assert(9)](/mtx_assert/9.md).

- *td*

  A thread responsible for this call. NULL becomes LK\_KERNPROC.

The **lockstatus**() function returns the status of the lock in relation to the *thread* passed to it. Note that if *td* is NULL and an exclusive lock is held, LK\_EXCLUSIVE will be returned.

The **lockmgr\_printinfo**() function prints debugging information about the lock. It is used primarily by [VOP\_PRINT(9)](/VOP_PRINT/9.md) functions.

## RETURN VALUES

The **lockcount**() function returns an integer greater than or equal to zero.

The **lockmgr**() function returns 0 on success and non-zero on failure.

The **lockstatus**() function returns:

- LK\_EXCLUSIVE

  An exclusive lock is held by the thread *td*.

- LK\_EXCLOTHER

  An exclusive lock is held by someone other than the thread *td*.

- LK\_SHARED

  A shared lock is held.

- 0

  The lock is not held by anyone.

## ERRORS

**lockmgr**() fails if:

- \[EBUSY\]

  LK\_FORCEUPGRADE was requested and another thread had already requested a lock upgrade.

- \[EBUSY\]

  LK\_NOWAIT was set, and a sleep would have been required.

- \[ENOLCK\]

  LK\_SLEEPFAIL was set and **lockmgr**() did sleep.

- \[EINTR\]

  PCATCH was set in the lock priority, and a signal was delivered during a sleep. Note the ERESTART error below.

- \[ERESTART\]

  PCATCH was set in the lock priority, a signal was delivered during a sleep, and the system call is to be restarted.

- \[EWOULDBLOCK\]

  a non-zero timeout was given, and the timeout expired.

## LOCKS

If LK\_INTERLOCK is passed in the *flags* argument to **lockmgr**(), the *interlkp* must be held prior to calling **lockmgr**(), and will be returned unlocked.

Upgrade attempts that fail result in the loss of the lock that is currently held. Also, it is invalid to upgrade an exclusive lock, and a [panic(9)](/panic/9.md) will be the result of trying.

## SEE ALSO

[msleep(9)](/msleep/9.md), [mtx\_assert(9)](/mtx_assert/9.md), [panic(9)](/panic/9.md), [VOP\_PRINT(9)](/VOP_PRINT/9.md)

## AUTHORS

This manual page was written by Chad David ⟨davidc@acns.ab.ca⟩.
