RTE OSAdapter POSIX 0.1.0 build 41b3eb3
POSIX/Linux OAL OSAdapter for Platform_RTE
Loading...
Searching...
No Matches
rte_posix_osadapter_lock.c File Reference

Bounded wait for a pthread mutex (OSA rule R2), shared by the mutex OSAdapter's lock_timed (REQ-OAL-MUTEX-014) and the memory OSAdapter's pool lock (REQ-OAL-MEM-015). Semantics and the two host variants are documented on posix_osa_lock_bounded() in rte_posix_osadapter_lock.h. Moved here unchanged from rte_posix_osadapter_mutex.c (2026-10-03) so both callers use one implementation. More...

#include "rte_posix_osadapter_lock.h"
#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
Include dependency graph for rte_posix_osadapter_lock.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
#define POSIX_LOCK_NS_PER_MS   1000000L
#define POSIX_LOCK_NS_PER_S   1000000000L
#define POSIX_LOCK_POLL_NS   1000000L

Functions

static rte_status_t try_lock_once (pthread_mutex_t *mutex)
static rte_status_t lock_until_deadline (pthread_mutex_t *mutex, rte_duration_ms_t timeout_ms)
static void deadline_add_ms (struct timespec *ts, rte_duration_ms_t ms)
rte_status_t posix_osa_lock_bounded (pthread_mutex_t *mutex, rte_duration_ms_t timeout_ms)
 Waits at most timeout_ms for a pthread mutex.

Detailed Description

Bounded wait for a pthread mutex (OSA rule R2), shared by the mutex OSAdapter's lock_timed (REQ-OAL-MUTEX-014) and the memory OSAdapter's pool lock (REQ-OAL-MEM-015). Semantics and the two host variants are documented on posix_osa_lock_bounded() in rte_posix_osadapter_lock.h. Moved here unchanged from rte_posix_osadapter_mutex.c (2026-10-03) so both callers use one implementation.

Definition in file rte_posix_osadapter_lock.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 3 of file rte_posix_osadapter_lock.c.

◆ POSIX_LOCK_NS_PER_MS

#define POSIX_LOCK_NS_PER_MS   1000000L

Local makros

Definition at line 24 of file rte_posix_osadapter_lock.c.

◆ POSIX_LOCK_NS_PER_S

#define POSIX_LOCK_NS_PER_S   1000000000L

Definition at line 25 of file rte_posix_osadapter_lock.c.

◆ POSIX_LOCK_POLL_NS

#define POSIX_LOCK_POLL_NS   1000000L

Sleep between two trylock attempts of the polling timed lock (1 ms).

Definition at line 28 of file rte_posix_osadapter_lock.c.

Function Documentation

◆ deadline_add_ms()

void deadline_add_ms ( struct timespec * ts,
rte_duration_ms_t ms )
static

Adds ms to ts, keeping tv_nsec in [0, 1 s).

Definition at line 176 of file rte_posix_osadapter_lock.c.

◆ lock_until_deadline()

rte_status_t lock_until_deadline ( pthread_mutex_t * mutex,
rte_duration_ms_t timeout_ms )
static

trylock loop against a CLOCK_MONOTONIC deadline; each sleep is at most POSIX_LOCK_POLL_NS and never past the deadline, so the loop ends at the deadline (no PI boost while polling, see the header).

Definition at line 123 of file rte_posix_osadapter_lock.c.

◆ posix_osa_lock_bounded()

rte_status_t posix_osa_lock_bounded ( pthread_mutex_t * mutex,
rte_duration_ms_t timeout_ms )

Waits at most timeout_ms for a pthread mutex.

Local variables declarations Global variables declarations Global functions

timeout_ms == 0 is one pthread_mutex_trylock(). Otherwise:

  • Linux: pthread_mutex_timedlock() against an absolute CLOCK_REALTIME deadline - a blocking wait that keeps the priority-inheritance boost. Known limit: the deadline is on the wall clock, so a clock step while a task waits shortens or lengthens that one wait. pthread_mutex_clocklock(CLOCK_MONOTONIC) is not used: glibc accepts it on a PI mutex only when kernel and libc support FUTEX_LOCK_PI2.
  • Other hosts (macOS has no pthread_mutex_timedlock): a pthread_mutex_trylock() loop against a CLOCK_MONOTONIC deadline with a sleep of at most 1 ms between tries, never past the deadline. No priority-inheritance boost while polling (the holder is not boosted by a task that is not blocked on the mutex) - development host only.

A failed clock read returns RTE_STATUS_INTERNAL_ERROR, never an unbounded wait.

Parameters
mutexInitialised pthread mutex to lock. Must not be NULL.
timeout_msMaximum time to wait, in milliseconds; 0 is one non-blocking try.
Returns
RTE_STATUS_OK with the mutex held; RTE_STATUS_TIMEOUT when it was not acquired within timeout_ms (the mutex is then not held); RTE_STATUS_INVALID_PARAM for a NULL mutex; RTE_STATUS_INTERNAL_ERROR for any other platform failure (a failed clock read, an error from the pthread call). REQ-OAL-MUTEX-014, REQ-OAL-MEM-015

Definition at line 44 of file rte_posix_osadapter_lock.c.

◆ try_lock_once()

rte_status_t try_lock_once ( pthread_mutex_t * mutex)
static

Local types declarations Local function declarations

One pthread_mutex_trylock(): OK when acquired, TIMEOUT when another task holds the mutex, INTERNAL_ERROR else.

Definition at line 68 of file rte_posix_osadapter_lock.c.