RTE OSAdapter POSIX 0.1.0 build 41b3eb3
POSIX/Linux OAL OSAdapter for Platform_RTE
Loading...
Searching...
No Matches
rte_posix_osadapter_mutex.c
Go to the documentation of this file.
1/* _POSIX_C_SOURCE: glibc declares PTHREAD_PRIO_INHERIT and pthread_mutexattr_setprotocol() only under a POSIX
2 * feature-test macro (this product builds strict C99), same as task.c's own. */
3#define _POSIX_C_SOURCE 200809L
4
32
33#include <pthread.h>
34#include "rte/oal/memory/rte_mem_util.h"
35
37
39typedef struct
40{
41 pthread_mutex_t mutex;
42 rte_mutex_handle_t self_handle;
44
45/* C99-compatible static assert: posix_mutex_state_t must fit inside
46 * rte_mutex_storage_t's reserved bytes (a negative array size is a
47 * compile error) - same convention as rte_posix_osadapter_timer.c's own. */
48typedef char posix_mutex_storage_fits_[(sizeof(posix_mutex_state_t) <= sizeof(rte_mutex_storage_t)) ? 1 : -1];
49
51static rte_status_t osadapter_create(rte_mutex_storage_t *storage, rte_mutex_handle_t *out_handle);
52static rte_status_t osadapter_lock(rte_mutex_handle_t handle);
53static rte_status_t osadapter_unlock(rte_mutex_handle_t handle);
54static rte_status_t osadapter_destroy(rte_mutex_handle_t handle);
55static rte_status_t osadapter_lock_timed(rte_mutex_handle_t handle, rte_duration_ms_t timeout_ms);
56
58static const rte_osadapter_mutex_t s_posix_mutex_osadapter = { osadapter_create, osadapter_lock, osadapter_unlock,
59 osadapter_destroy, osadapter_lock_timed };
60
62
63
65const rte_osadapter_mutex_t *rte_posix_osadapter_mutex(void)
66{
68}
69
70
71/****Local functions ****/
72
73static rte_status_t osadapter_create(rte_mutex_storage_t *storage, rte_mutex_handle_t *out_handle)
74{
75 rte_status_t status = RTE_STATUS_OK;
77 pthread_mutexattr_t attr;
78
79 if ((storage == NULL) || (out_handle == NULL))
80 {
81 status = RTE_STATUS_INVALID_PARAM;
82 }
83 else
84 {
85 state = (posix_mutex_state_t *)(void *)storage;
86 rte_mem_set(state, 0, sizeof(*state));
87
88 /* OSA rule R2 (REQ-OAL-MUTEX-004): every mutex uses priority inheritance. Requested at run time on every
89 * host - no _POSIX_THREAD_PRIO_INHERIT compile gate: macOS declares it -1 yet accepts the protocol at run
90 * time, so a gate would refuse every macOS mutex. A refusal at any of the three calls fails the create;
91 * there is no fallback to a plain mutex. */
92 if (pthread_mutexattr_init(&attr) != 0)
93 {
94 status = RTE_STATUS_INTERNAL_ERROR;
95 }
96 else
97 {
98 if (pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT) != 0)
99 {
100 status = RTE_STATUS_INTERNAL_ERROR;
101 }
102 else if (pthread_mutex_init(&state->mutex, &attr) != 0)
103 {
104 status = RTE_STATUS_INTERNAL_ERROR;
105 }
106 else
107 {
108 /* priority-inheritance mutex initialised */
109 }
110 (void)pthread_mutexattr_destroy(&attr);
111 }
112
113 if (status == RTE_STATUS_OK)
114 {
115 state->self_handle = (rte_mutex_handle_t)(void *)storage;
116 *out_handle = state->self_handle;
117 }
118 else
119 {
120 /* OSA rule R6: report the violation; *out_handle stays unwritten and the caller's setup fails. */
121 POSIX_OSA_REPORT_RT_ATTRIBUTE("mutex refused priority inheritance - create failed");
122 }
123 }
124
125 return status;
126}
127
128static rte_status_t osadapter_lock(rte_mutex_handle_t handle)
129{
130 posix_mutex_state_t *state = (posix_mutex_state_t *)(void *)handle;
131
132 if (state == NULL)
133 {
134 return RTE_STATUS_INVALID_PARAM;
135 }
136 if (pthread_mutex_lock(&state->mutex) != 0)
137 {
138 return RTE_STATUS_INTERNAL_ERROR;
139 }
140 return RTE_STATUS_OK;
141}
142
143static rte_status_t osadapter_unlock(rte_mutex_handle_t handle)
144{
145 posix_mutex_state_t *state = (posix_mutex_state_t *)(void *)handle;
146
147 if (state == NULL)
148 {
149 return RTE_STATUS_INVALID_PARAM;
150 }
151 if (pthread_mutex_unlock(&state->mutex) != 0)
152 {
153 return RTE_STATUS_INTERNAL_ERROR;
154 }
155 return RTE_STATUS_OK;
156}
157
158static rte_status_t osadapter_destroy(rte_mutex_handle_t handle)
159{
160 posix_mutex_state_t *state = (posix_mutex_state_t *)(void *)handle;
161
162 if (state == NULL)
163 {
164 return RTE_STATUS_INVALID_PARAM;
165 }
166 if (pthread_mutex_destroy(&state->mutex) != 0)
167 {
168 return RTE_STATUS_INTERNAL_ERROR;
169 }
170 return RTE_STATUS_OK;
171}
172
173static rte_status_t osadapter_lock_timed(rte_mutex_handle_t handle, rte_duration_ms_t timeout_ms)
174{
175 posix_mutex_state_t *state = (posix_mutex_state_t *)(void *)handle;
176 rte_status_t status;
177
178 if (state == NULL)
179 {
180 status = RTE_STATUS_INVALID_PARAM;
181 }
182 else
183 {
184 /* REQ-OAL-MUTEX-014: the bounded wait (timeout_ms == 0 is one non-blocking try) is shared with the memory
185 * OSAdapter's pool lock, see rte_posix_osadapter_lock.h. */
186 status = posix_osa_lock_bounded(&state->mutex, timeout_ms);
187 }
188 return status;
189}
Real POSIX/Linux OSAdapter for every OAL service (ADR-018).
const rte_osadapter_mutex_t * rte_posix_osadapter_mutex(void)
The POSIX rte_mutex OSAdapter (a thin pthread_mutex_t wrapper, ADR-033).
static rte_status_t osadapter_create(rte_ipc_storage_t *storage, const rte_ipc_config_t *config, rte_ipc_handle_t *out_handle)
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.
Private helper: a bounded wait for a pthread mutex (OSA rule R2: every wait bounded).
static const rte_osadapter_mutex_t s_posix_mutex_osadapter
Private helper: reports a real-time attribute this OSAdapter could not apply (OSA rules R1/R2/R6).