Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
mutex.h
1/*
2 * mutex.h - Portable mutex HAL.
3 *
4 * Goal: provide a single, statically-allocatable mutex abstraction so
5 * driver code (e.g. FUSB302.c) does not need to carry OS-specific
6 * #ifdefs to protect shared hardware resources (e.g. TCPC_REG_MEASURE).
7 *
8 * Usage:
9 * HAL_MUTEX_DEFINE(measure_lock);
10 *
11 * void some_init(void)
12 * {
13 * hal_mutex_init(&measure_lock);
14 * }
15 *
16 * void some_critical_section(void)
17 * {
18 * hal_mutex_lock(&measure_lock);
19 * ... touch shared register ...
20 * hal_mutex_unlock(&measure_lock);
21 * }
22 */
23
24#ifndef HAL_MUTEX_H
25#define HAL_MUTEX_H
26
27#ifdef __cplusplus
28
29#include <cstdint>
30
31namespace lampda {
32namespace hal {
34namespace mutex {
35
36extern "C" {
37
38#endif
39
40/* --- Status codes returned by all hal_mutex_* functions --- */
41#define HAL_MUTEX_OK (0)
42#define HAL_MUTEX_ERROR (-1)
43#define HAL_MUTEX_TIMEOUT (-2)
44
45#define HAL_MUTEX_OPAQUE_SIZE 128U
46#define HAL_MUTEX_OPAQUE_ALIGN 8U
47
48 typedef struct hal_mutex
49 {
50 /* Backend-owned storage. Never access directly from portable code. */
51 uint8_t opaque[HAL_MUTEX_OPAQUE_SIZE] __attribute__((aligned(HAL_MUTEX_OPAQUE_ALIGN)));
52 /* Set to 1 by hal_mutex_init(); used to catch use-before-init bugs. */
53 uint8_t is_initialized;
55
56 int hal_mutex_init(hal_mutex_t* m);
57
58 void hal_mutex_lock(hal_mutex_t* m);
59
60 void hal_mutex_unlock(hal_mutex_t* m);
61
62 int hal_mutex_trylock(hal_mutex_t* m);
63
64#ifdef __cplusplus
65}
66}
67}
68}
69#endif
70
71#endif /* HAL_MUTEX_H */
Program scope.
Definition: control_fixed_modes.hpp:15
Definition: mutex.h:49