membarrier(2)

membarrier2

Memory barrier

Usage

#include <sys/membarrier.h>

int

membarrier(int cmd, unsigned flags, int cpu_id);

Library

Standard C Library (libc, -lc)

Overview

The membarrier system call provides a memory barrier, ensuring ordering between memory accesses performed by different threads or processes within the same address space.

The following values for cmd may be specified:

MEMBARRIER_CMD_QUERY

Query supported commands. A bitmask of commands supported by the kernel is returned.

MEMBARRIER_CMD_GLOBAL

Issue a memory barrier for all threads from all processes.

MEMBARRIER_CMD_SHARED

This is an alias for MEMBARRIER_CMD_GLOBAL.

MEMBARRIER_CMD_GLOBAL_EXPEDITED

Execute a memory barrier on all running threads of all processes registered with MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED.

MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED

Register the process to receive MEMBARRIER_CMD_GLOBAL_EXPEDITED memory barriers.

MEMBARRIER_CMD_PRIVATE_EXPEDITED

Execute a memory barrier on each running thread belonging to the same process as the thread calling membarrier.

MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED

Register the process to receive MEMBARRIER_CMD_PRIVATE_EXPEDITED memory barriers.

MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE

In addition to the guarantees provided by MEMBARRIER_CMD_PRIVATE_EXPEDITED it also executes machine-specific serialization instructions, which ensures that all possible speculative and out-of-order activities on the target CPU are fenced.

MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE

Register the process to receive MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE memory barriers.

MEMBARRIER_CMD_GET_REGISTRATIONS

Return the bitmask of the currently registered receive barriers.

The following cmd values are defined for source compatibility but are not yet supported:

MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ

The flags argument must be 0. The cpu_id argument is ignored.

Return values

If the cmd is MEMBARRIER_CMD_QUERY a bitmask of supported commands is returned. For the MEMBARRIER_CMD_GET_REGISTRATIONS command, a bitmask of the registrations for the current process is returned. Otherwise, on success, membarrier returns 0. On error, -1 is returned and errno is set to indicate the error.

Errors

membarrier may fail with the following errors:

[EINVAL]

cmd does not specify a valid command.

[EINVAL]

flags is not 0.

[EPERM]

An attempt was made to use MEMBARRIER_CMD_GLOBAL_EXPEDITED, MEMBARRIER_CMD_PRIVATE_EXPEDITED, or MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE by a process that did not previously register use with the corresponding MEMBARRIER_CMD_REGISTER_* cmd.

Releases

7 releases since FreeBSD 14.2-RELEASE; the text changed 1 time.

  1. 14.2–15.0
  2. 15.1–16.0

Standards

The membarrier system call originated in Linux. This implementation aims to be source-compatible with the Linux implementation. Certain cmd and flags values are currently not supported by FreeBSD.

History

The membarrier function was introduced in FreeBSD 14.1.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.