Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Toggle main menu visibility
Loading...
Searching...
No Matches
sapi_mutex.c
Go to the documentation of this file.
1
7
#include "
safeapi/utils/lifecycle/sapi_lifecycle.h
"
8
#include "
safeapi/oal/mutex/sapi_mutex.h
"
9
#include "
safeapi_backend/mutex/sapi_mutex_backend.h
"
10
12
14
17
static
const
sapi_mutex_backend_t
*
s_backend
= NULL;
18
20
22
23
25
sapi_status_t
sapi_mutex_register_backend
(
const
sapi_mutex_backend_t
*backend)
26
{
27
sapi_status_t
lifecycle_status;
28
29
if
(backend == NULL)
30
{
31
return
SAPI_STATUS_INVALID_PARAM
;
32
}
33
/* REQ-LIFECYCLE-001 (ADR-026): registering a backend is a setup-only
34
* action - refuse once the application's setup phase has been locked. */
35
lifecycle_status =
sapi_lifecycle_check_setup_allowed
();
36
if
(lifecycle_status !=
SAPI_STATUS_OK
)
37
{
38
return
lifecycle_status;
39
}
40
s_backend
= backend;
41
return
SAPI_STATUS_OK
;
42
}
43
44
sapi_status_t
sapi_mutex_create
(sapi_mutex_storage_t *storage,
sapi_mutex_handle_t
*out_handle)
45
{
46
if
((storage == NULL) || (out_handle == NULL))
47
{
48
return
SAPI_STATUS_INVALID_PARAM
;
49
}
50
*out_handle = NULL;
51
/* REQ-LIFECYCLE-001 (ADR-026): a mutex is a setup-only resource - refuse
52
* once the application's setup phase has been locked (sapi_appmanager_run(),
53
* after ops->init() succeeds) - same posture as sapi_timer_create()/
54
* sapi_task_create(). */
55
{
56
sapi_status_t
lifecycle_status =
sapi_lifecycle_check_setup_allowed
();
57
58
if
(lifecycle_status !=
SAPI_STATUS_OK
)
59
{
60
return
lifecycle_status;
61
}
62
}
63
if
(
s_backend
== NULL)
64
{
65
return
SAPI_STATUS_NOT_INITIALIZED
;
66
}
67
if
(
s_backend
->
create
== NULL)
68
{
69
return
SAPI_STATUS_NOT_SUPPORTED
;
70
}
71
return
s_backend
->
create
(storage, out_handle);
72
}
73
74
sapi_status_t
sapi_mutex_lock
(
sapi_mutex_handle_t
handle)
75
{
76
if
(handle == NULL)
77
{
78
return
SAPI_STATUS_INVALID_PARAM
;
79
}
80
if
(
s_backend
== NULL)
81
{
82
return
SAPI_STATUS_NOT_INITIALIZED
;
83
}
84
if
(
s_backend
->
lock
== NULL)
85
{
86
return
SAPI_STATUS_NOT_SUPPORTED
;
87
}
88
return
s_backend
->
lock
(handle);
89
}
90
91
sapi_status_t
sapi_mutex_unlock
(
sapi_mutex_handle_t
handle)
92
{
93
if
(handle == NULL)
94
{
95
return
SAPI_STATUS_INVALID_PARAM
;
96
}
97
if
(
s_backend
== NULL)
98
{
99
return
SAPI_STATUS_NOT_INITIALIZED
;
100
}
101
if
(
s_backend
->
unlock
== NULL)
102
{
103
return
SAPI_STATUS_NOT_SUPPORTED
;
104
}
105
return
s_backend
->
unlock
(handle);
106
}
107
108
sapi_status_t
sapi_mutex_destroy
(
sapi_mutex_handle_t
handle)
109
{
110
if
(handle == NULL)
111
{
112
return
SAPI_STATUS_INVALID_PARAM
;
113
}
114
if
(
s_backend
== NULL)
115
{
116
return
SAPI_STATUS_NOT_INITIALIZED
;
117
}
118
if
(
s_backend
->
destroy
== NULL)
119
{
120
return
SAPI_STATUS_NOT_SUPPORTED
;
121
}
122
return
s_backend
->
destroy
(handle);
123
}
sapi_lifecycle_check_setup_allowed
sapi_status_t sapi_lifecycle_check_setup_allowed(void)
Convenience check for a setup-only constructor: call this as one of the first checks in any function ...
Definition
sapi_lifecycle.c:40
sapi_mutex_register_backend
sapi_status_t sapi_mutex_register_backend(const sapi_mutex_backend_t *backend)
Registers the backend implementation used by every sapi_mutex_* call. This is how an integrator suppl...
Definition
sapi_mutex.c:25
sapi_mutex_handle_t
struct sapi_mutex_impl_s * sapi_mutex_handle_t
Definition
sapi_mutex.h:44
sapi_mutex_destroy
sapi_status_t sapi_mutex_destroy(sapi_mutex_handle_t handle)
Destroys a mutex, releasing any backend resources bound to it. Must not be called while any task hold...
Definition
sapi_mutex.c:108
sapi_mutex_unlock
sapi_status_t sapi_mutex_unlock(sapi_mutex_handle_t handle)
Releases a mutex previously locked by the calling task.
Definition
sapi_mutex.c:91
sapi_mutex_create
sapi_status_t sapi_mutex_create(sapi_mutex_storage_t *storage, sapi_mutex_handle_t *out_handle)
Creates (and initializes, unlocked) a mutex bound to caller-owned storage.
Definition
sapi_mutex.c:44
sapi_mutex_lock
sapi_status_t sapi_mutex_lock(sapi_mutex_handle_t handle)
Blocks the calling task until it holds the mutex.
Definition
sapi_mutex.c:74
sapi_status_t
sapi_status_t
Common result/status codes.
Definition
sapi_status.h:27
SAPI_STATUS_NOT_SUPPORTED
@ SAPI_STATUS_NOT_SUPPORTED
Definition
sapi_status.h:34
SAPI_STATUS_INVALID_PARAM
@ SAPI_STATUS_INVALID_PARAM
Definition
sapi_status.h:29
SAPI_STATUS_NOT_INITIALIZED
@ SAPI_STATUS_NOT_INITIALIZED
Definition
sapi_status.h:30
SAPI_STATUS_OK
@ SAPI_STATUS_OK
Definition
sapi_status.h:28
s_backend
static const sapi_clocksync_backend_t * s_backend
Definition
sapi_clocksync.c:18
sapi_lifecycle.h
Process-wide application setup-phase lock (ADR-026).
sapi_mutex.h
OS Abstraction Layer - Mutual exclusion service.
sapi_mutex_backend.h
OS-backend adaptation surface for the Mutex service (ADR-033, ADR-021).
sapi_mutex_backend_t
Backend vtable: an integrator's implementation of the mutex service for a specific OS/RTOS/BSP target...
Definition
sapi_mutex_backend.h:35
sapi_mutex_backend_t::unlock
sapi_status_t(*) unlock(sapi_mutex_handle_t handle)
Backend implementation of sapi_mutex_unlock(). May be NULL.
Definition
sapi_mutex_backend.h:41
sapi_mutex_backend_t::destroy
sapi_status_t(*) destroy(sapi_mutex_handle_t handle)
Backend implementation of sapi_mutex_destroy(). May be NULL.
Definition
sapi_mutex_backend.h:43
sapi_mutex_backend_t::create
sapi_status_t(*) create(sapi_mutex_storage_t *storage, sapi_mutex_handle_t *out_handle)
Backend implementation of sapi_mutex_create(). May be NULL.
Definition
sapi_mutex_backend.h:37
sapi_mutex_backend_t::lock
sapi_status_t(*) lock(sapi_mutex_handle_t handle)
Backend implementation of sapi_mutex_lock(). May be NULL.
Definition
sapi_mutex_backend.h:39
src
oal
mutex
sapi_mutex.c
Generated by
1.18.0