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

# mtx\_trylock\_flags(9)

kernel synchronization primitives

## NAME

**mutex**, **mtx\_init**, **mtx\_lock**, **mtx\_lock\_spin**, **mtx\_lock\_flags**, **mtx\_lock\_spin\_flags**, **mtx\_trylock**, **mtx\_trylock\_flags**, **mtx\_unlock**, **mtx\_unlock\_spin**, **mtx\_unlock\_flags**, **mtx\_unlock\_spin\_flags**, **mtx\_destroy**, **mtx\_initialized**, **mtx\_owned**, **mtx\_recursed**, **mtx\_assert**, **MTX\_SYSINIT** – kernel synchronization primitives

## SYNOPSIS

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

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

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

*void*\

**mtx\_init**(*struct mtx \*mutex*, *const char \*name*, *const char \*type*, *int opts*);

*void*\

**mtx\_lock**(*struct mtx \*mutex*);

*void*\

**mtx\_lock\_spin**(*struct mtx \*mutex*);

*void*\

**mtx\_lock\_flags**(*struct mtx \*mutex*, *int flags*);

*void*\

**mtx\_lock\_spin\_flags**(*struct mtx \*mutex*, *int flags*);

*int*\

**mtx\_trylock**(*struct mtx \*mutex*);

*int*\

**mtx\_trylock\_flags**(*struct mtx \*mutex*, *int flags*);

*void*\

**mtx\_unlock**(*struct mtx \*mutex*);

*void*\

**mtx\_unlock\_spin**(*struct mtx \*mutex*);

*void*\

**mtx\_unlock\_flags**(*struct mtx \*mutex*, *int flags*);

*void*\

**mtx\_unlock\_spin\_flags**(*struct mtx \*mutex*, *int flags*);

*void*\

**mtx\_destroy**(*struct mtx \*mutex*);

*int*\

**mtx\_initialized**(*struct mtx \*mutex*);

*int*\

**mtx\_owned**(*struct mtx \*mutex*);

*int*\

**mtx\_recursed**(*struct mtx \*mutex*);

**options INVARIANTS** **options INVARIANT\_SUPPORT**

*void*\

**mtx\_assert**(*struct mtx \*mutex*, *int what*);

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

**MTX\_SYSINIT**(*name*, *struct mutex \*mtx*, *const char \*description*, *int opts*);

## DESCRIPTION

Mutexes are the most basic and primary method of thread synchronization. The major design considerations for mutexes are:

1. Acquiring and releasing uncontested mutexes should be as cheap as possible.

2. They must have the information and storage space to support priority propagation.

3. A thread must be able to recursively acquire a mutex, provided that the mutex is initialized to support recursion.

There are currently two flavors of mutexes, those that context switch when they block and those that do not.

By default, MTX\_DEF mutexes will context switch when they are already held. As an optimization, they may spin for some amount of time before context switching. It is important to remember that since a thread may be preempted at any time, the possible context switch introduced by acquiring a mutex is guaranteed to not break anything that is not already broken.

Mutexes which do not context switch are MTX\_SPIN mutexes. These should only be used to protect data shared with primary interrupt code. This includes INTR\_FAST interrupt handlers and low level scheduling code. In all architectures both acquiring and releasing of a uncontested spin mutex is more expensive than the same operation on a non-spin mutex. In order to protect an interrupt service routine from blocking against itself all interrupts are either blocked or deferred on a processor while holding a spin lock. It is permissible to hold multiple spin mutexes.

Once a spin mutex has been acquired it is not permissible to acquire a blocking mutex.

The storage needed to implement a mutex is provided by a *struct* *mtx*. In general this should be treated as an opaque object and referenced only with the mutex primitives.

The **mtx\_init**() function must be used to initialize a mutex before it can be passed to any of the other mutex functions. The *name* option is used to identify the lock in debugging output etc. The *type* option is used by the witness code to classify a mutex when doing checks of lock ordering. If *type* is NULL, *name* is used in its place. The pointer passed in as *name* and *type* is saved rather than the data it points to. The data pointed to must remain stable until the mutex is destroyed. The *opts* argument is used to set the type of mutex. It may contain either MTX\_DEF or MTX\_SPIN but not both. See below for additional initialization options. It is not permissible to pass the same *mutex* to **mtx\_init**() multiple times without intervening calls to **mtx\_destroy**().

The **mtx\_lock**() function acquires a MTX\_DEF mutual exclusion lock on behalf of the currently running kernel thread. If another kernel thread is holding the mutex, the caller will be disconnected from the CPU until the mutex is available (i.e., it will block).

The **mtx\_lock\_spin**() function acquires a MTX\_SPIN mutual exclusion lock on behalf of the currently running kernel thread. If another kernel thread is holding the mutex, the caller will spin until the mutex becomes available. Interrupts are disabled during the spin and remain disabled following the acquiring of the lock.

It is possible for the same thread to recursively acquire a mutex with no ill effects, provided that the MTX\_RECURSE bit was passed to **mtx\_init**() during the initialization of the mutex.

The **mtx\_lock\_flags**() and **mtx\_lock\_spin\_flags**() functions acquire a MTX\_DEF or MTX\_SPIN lock, respectively, and also accept a *flags* argument. In both cases, the only flag presently available for lock acquires is MTX\_QUIET. If the MTX\_QUIET bit is turned on in the *flags* argument, then if KTR\_LOCK tracing is being done, it will be silenced during the lock acquire.

The **mtx\_trylock**() attempts to acquire the MTX\_DEF mutex pointed to by *mutex*. If the mutex cannot be immediately acquired **mtx\_trylock**() will return 0, otherwise the mutex will be acquired and a non-zero value will be returned.

The **mtx\_trylock\_flags**() function has the same behavior as **mtx\_trylock**() but should be used when the caller desires to pass in a *flags* value. Presently, the only valid value in the **mtx\_trylock**() case is MTX\_QUIET, and its effects are identical to those described for **mtx\_lock**() above.

The **mtx\_unlock**() function releases a MTX\_DEF mutual exclusion lock. The current thread may be preempted if a higher priority thread is waiting for the mutex.

The **mtx\_unlock\_spin**() function releases a MTX\_SPIN mutual exclusion lock.

The **mtx\_unlock\_flags**() and **mtx\_unlock\_spin\_flags**() functions behave in exactly the same way as do the standard mutex unlock routines above, while also allowing a *flags* argument which may specify MTX\_QUIET. The behavior of MTX\_QUIET is identical to its behavior in the mutex lock routines.

The **mtx\_destroy**() function is used to destroy *mutex* so the data associated with it may be freed or otherwise overwritten. Any mutex which is destroyed must previously have been initialized with **mtx\_init**(). It is permissible to have a single hold count on a mutex when it is destroyed. It is not permissible to hold the mutex recursively, or have another thread blocked on the mutex when it is destroyed.

The **mtx\_initialized**() function returns non-zero if *mutex* has been initialized and zero otherwise.

The **mtx\_owned**() function returns non-zero if the current thread holds *mutex*. If the current thread does not hold *mutex* zero is returned.

The **mtx\_recursed**() function returns non-zero if the *mutex* is recursed. This check should only be made if the running thread already owns *mutex*.

The **mtx\_assert**() function allows assertions specified in *what* to be made about *mutex*. If the assertions are not true and the kernel is compiled with **options INVARIANTS** and **options INVARIANT\_SUPPORT**, the kernel will panic. Currently the following assertions are supported:

- MA\_OWNED

  Assert that the current thread holds the mutex pointed to by the first argument.

- MA\_NOTOWNED

  Assert that the current thread does not hold the mutex pointed to by the first argument.

- MA\_RECURSED

  Assert that the current thread has recursed on the mutex pointed to by the first argument. This assertion is only valid in conjunction with MA\_OWNED.

- MA\_NOTRECURSED

  Assert that the current thread has not recursed on the mutex pointed to by the first argument. This assertion is only valid in conjunction with MA\_OWNED.

The **MTX\_SYSINIT**() macro is used to generate a call to the **mtx\_sysinit**() routine at system startup in order to initialize a given mutex lock. The parameters are the same as **mtx\_init**() but with an additional argument, *name*, that is used in generating unique variable names for the related structures associated with the lock and the sysinit routine.

### The Default Mutex Type

Most kernel code should use the default lock type, MTX\_DEF. The default lock type will allow the thread to be disconnected from the CPU if the lock is already held by another thread. The implementation may treat the lock as a short term spin lock under some circumstances. However, it is always safe to use these forms of locks in an interrupt thread without fear of deadlock against an interrupted thread on the same CPU.

### The Spin Mutex Type

A MTX\_SPIN mutex will not relinquish the CPU when it cannot immediately get the requested lock, but will loop, waiting for the mutex to be released by another CPU. This could result in deadlock if another thread interrupted the thread which held a mutex and then tried to acquire the mutex. For this reason spin locks disable all interrupts on the local CPU.

Spin locks are fairly specialized locks that are intended to be held for very short periods of time. Their primary purpose is to protect portions of the code that implement other synchronization primitives such as default mutexes, thread scheduling, and interrupt threads.

### Initialization Options

The options passed in the *opts* argument of **mtx\_init**() specify the mutex type. One of the MTX\_DEF or MTX\_SPIN options is required and only one of those two options may be specified. The possibilities are:

- MTX\_DEF

  Default mutexes will always allow the current thread to be suspended to avoid deadlock conditions against interrupt threads. The implementation of this lock type may spin for a while before suspending the current thread.

- MTX\_SPIN

  Spin mutexes will never relinquish the CPU. All interrupts are disabled on the local CPU while any spin lock is held.

- MTX\_RECURSE

  Specifies that the initialized mutex is allowed to recurse. This bit must be present if the mutex is permitted to recurse.

- MTX\_QUIET

  Do not log any mutex operations for this lock.

- MTX\_NOWITNESS

  Instruct [witness(4)](/witness/4.md) to ignore this lock.

- MTX\_DUPOK

  Witness should not log messages about duplicate locks being acquired.

### Lock and Unlock Flags

The flags passed to the **mtx\_lock\_flags**(), **mtx\_lock\_spin\_flags**(), **mtx\_unlock\_flags**(), and **mtx\_unlock\_spin\_flags**() functions provide some basic options to the caller, and are often used only under special circumstances to modify lock or unlock behavior. Standard locking and unlocking should be performed with the **mtx\_lock**(), **mtx\_lock\_spin**(), **mtx\_unlock**(), and **mtx\_unlock\_spin**() functions. Only if a flag is required should the corresponding flags-accepting routines be used.

Options that modify mutex behavior:

- MTX\_QUIET

  This option is used to quiet logging messages during individual mutex operations. This can be used to trim superfluous logging messages for debugging purposes.

### Giant

If *Giant* must be acquired, it must be acquired prior to acquiring other mutexes. Put another way: it is impossible to acquire *Giant* non-recursively while holding another mutex. It is possible to acquire other mutexes while holding *Giant*, and it is possible to acquire *Giant* recursively while holding other mutexes.

### Sleeping

Sleeping while holding a mutex (except for *Giant*) is never safe and should be avoided. There are numerous assertions which will fail if this is attempted.

### Functions Which Access Memory in Userspace

No mutexes should be held (except for *Giant*) across functions which access memory in userspace, such as [copyin(9)](/copyin/9.md), [copyout(9)](/copyout/9.md), [uiomove(9)](/uiomove/9.md), [fuword(9)](/fuword/9.md), etc. No locks are needed when calling these functions.

## SEE ALSO

[condvar(9)](/condvar/9.md), [msleep(9)](/msleep/9.md), [mtx\_pool(9)](/mtx_pool/9.md), [MUTEX\_PROFILING(9)](/MUTEX_PROFILING/9.md), [panic(9)](/panic/9.md), [sema(9)](/sema/9.md), [sx(9)](/sx/9.md)

## HISTORY

These functions appeared in BSD/OS 4.1 and FreeBSD 5.0.
