|
Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
|
Macros | |
| #define | SCOPE_VAR_DEFINE(_name, _type, _exit_fn, _init_fn, ...) |
| Define a scoped variable type. | |
| #define | SCOPE_GUARD_DEFINE(_name, _type, _lock, _unlock) |
| Define a scoped guard type. | |
| #define | SCOPE_COND_GUARD_DEFINE(_name, _type, _try_lock, _unlock) |
| Define a conditional (failable) scoped guard type. | |
| #define | SCOPE_DEFER_DEFINE(_func, ...) |
| Define a scoped defer type. | |
| #define | scope_var(_name, _var) |
| Declare a variable with automatic cleanup. | |
| #define | scope_var_init(_name, _var, _init_expr) |
| Declare a variable with automatic cleanup using direct initialization. | |
| #define | scope_guard(_name) |
| Acquire a scoped guard lock. | |
| #define | scoped_guard(_name, ...) |
| Acquire a guard for the lifetime of the following block. | |
| #define | scoped_cond_guard(_name, _fail, ...) |
| Conditionally acquire a guard for the lifetime of the following block. | |
| #define | scope_defer(_name) |
| Register a scoped deferred call. | |
| #define SCOPE_COND_GUARD_DEFINE | ( | _name, | |
| _type, | |||
| _try_lock, | |||
| _unlock ) |
#include <zephyr/cleanup.h>
Define a conditional (failable) scoped guard type.
This macro defines a guard whose lock acquisition may fail, for use with scoped_cond_guard. Unlike SCOPE_GUARD_DEFINE, the acquire expression is evaluated for success: when it evaluates truthy the guard stores _type _T and the release expression runs on scope exit; when it evaluates falsy the guard stores NULL and nothing is released.
| _name | Name of the guard (will be prefixed with guard_) |
| _type | Type of the lock object (must be a pointer type) |
| _try_lock | Expression evaluating truthy when the lock is acquired (can reference _T) |
| _unlock | Expression to release the lock (can reference _T) |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define scope_defer | ( | _name | ) |
#include <zephyr/cleanup.h>
Register a scoped deferred call.
This macro creates a defer variable with an automatically generated unique name. The defer will execute its cleanup action when going out of scope.
| _name | Name of the defer type (defined by SCOPE_DEFER_DEFINE or SCOPE_VAR_DEFINE with defer_ prefix) |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define SCOPE_DEFER_DEFINE | ( | _func, | |
| ... ) |
#include <zephyr/cleanup.h>
Define a scoped defer type.
This macro defines a defer that executes a cleanup function when the variable goes out of scope. Unlike guards, defers don't acquire a resource on initialization.
| _func | The function to call at cleanup |
| ... | The argument types to pass to the cleanup function |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define scope_guard | ( | _name | ) |
#include <zephyr/cleanup.h>
Acquire a scoped guard lock.
This macro creates a guard variable with an automatically generated unique name. The guard will acquire the lock on initialization and release it when going out of scope.
| _name | Name of the guard type (defined by SCOPE_GUARD_DEFINE or SCOPE_VAR_DEFINE with guard_ prefix) |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define SCOPE_GUARD_DEFINE | ( | _name, | |
| _type, | |||
| _lock, | |||
| _unlock ) |
#include <zephyr/cleanup.h>
Define a scoped guard type.
This macro defines a guard that automatically acquires a lock on initialization and releases it when going out of scope.
| _name | Name of the guard (will be prefixed with guard_) |
| _type | Type of the lock object (typically a pointer) |
| _lock | Expression to acquire the lock (can reference _T) |
| _unlock | Expression to release the lock (can reference _T) |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define scope_var | ( | _name, | |
| _var ) |
#include <zephyr/cleanup.h>
Declare a variable with automatic cleanup.
This macro declares a variable with automatic cleanup using a previously defined cleanup helper. The variable will be automatically cleaned up when it goes out of scope.
The variable is initialized by calling the init function defined in SCOPE_VAR_DEFINE. Use scope_var_init if you want to initialize the variable with a direct expression instead of calling the init function.
| _name | Name of the cleanup helper (defined by SCOPE_VAR_DEFINE) |
| _var | Name of the variable to declare |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define SCOPE_VAR_DEFINE | ( | _name, | |
| _type, | |||
| _exit_fn, | |||
| _init_fn, | |||
| ... ) |
#include <zephyr/cleanup.h>
Define a scoped variable type.
This macro defines a new cleanup helper that can be used with the __cleanup attribute to automatically execute cleanup code when a variable goes out of scope.
| _name | Name of the cleanup helper |
| _type | Type of the variable to be cleaned up |
| _exit_fn | Cleanup code to execute when variable goes out of scope (can reference _T) |
| _init_fn | Initialization expression for the variable |
| ... | Initialization arguments (variadic) |
The macro creates:
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define scope_var_init | ( | _name, | |
| _var, | |||
| _init_expr ) |
#include <zephyr/cleanup.h>
Declare a variable with automatic cleanup using direct initialization.
This macro declares a variable with automatic cleanup using a previously defined cleanup helper. The variable will be automatically cleaned up when it goes out of scope.
Unlike scope_var, this macro accepts a direct initialization expression instead of calling the init function defined in SCOPE_VAR_DEFINE. Use this when you need to bypass the init function and initialize the variable directly (e.g., with a struct initializer, a different function, or a literal value).
| _name | Name of the cleanup helper (defined by SCOPE_VAR_DEFINE) |
| _var | Name of the variable to declare |
| _init_expr | Direct initialization expression for the variable (e.g., {0}, NULL, or a function call) |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define scoped_cond_guard | ( | _name, | |
| _fail, | |||
| ... ) |
#include <zephyr/cleanup.h>
Conditionally acquire a guard for the lifetime of the following block.
This macro attempts to acquire a conditional (failable) scoped guard and, on success, holds it for the duration of the brace block that immediately follows, releasing it as soon as the block is exited. If the lock cannot be acquired, _fail is executed and the block is skipped.
On success the block runs exactly once and the lock is released on any exit from the block, including break, return and goto (continue behaves like break).
| _name | Name of the guard type (defined by SCOPE_COND_GUARD_DEFINE) |
| _fail | Statement executed when the lock cannot be acquired (e.g. break, return -EBUSY, or {} to silently skip the block) |
| ... | Arguments to acquire the guard (e.g. the lock object) |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage:
| #define scoped_guard | ( | _name, | |
| ... ) |
#include <zephyr/cleanup.h>
Acquire a guard for the lifetime of the following block.
This macro acquires a scoped guard lock and holds it for the duration of the brace block that immediately follows, releasing it as soon as the block is exited. Unlike scope_guard, whose guard lives until the end of the enclosing scope, the guard created here is bound to its own block.
The block runs exactly once. The lock is released on any exit from the block, including break, return and goto. Note that, because the block is implemented with a hidden loop, break and continue leave the guarded block rather than affecting an enclosing loop.
This macro also accepts a guard defined with SCOPE_COND_GUARD_DEFINE. In that case the block is skipped (and nothing is held) if the lock cannot be acquired; use scoped_cond_guard when you need to run a statement on acquisition failure.
| _name | Name of the guard type (defined by SCOPE_GUARD_DEFINE or SCOPE_COND_GUARD_DEFINE) |
| ... | Arguments to acquire the guard (e.g. the lock object) |
CONFIG_SCOPE_CLEANUP_HELPERS.Usage: