-
Notifications
You must be signed in to change notification settings - Fork 6
/
emutex.h
35 lines (30 loc) · 1.18 KB
/
emutex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* emutex implements a minimalistic mutex in standard C11. While written for
* embedded systems it is fully portable and can be used on any system
* supporting C11. (e.g. on POSIX/x86 as the included tests do.)
*
* emutex_trylock returns false if it fails to allocate the mutex, while
* emutex_lock implements a spin lock and blocks until the mutex is aquired.
*
* WARNING: As of GCC 8.2.0 the code generated for ARMv6-m (e.g. Cortex-M0)
* does not look safe, since the architecture doesn't support the
* LDREX/STREX instructions and GCC doesn't generate code to protect
* against interrupts. Though ARMv7-m (e.g. Cortex-M3 and onwards)
* has been tested and is fully supported.
*
* Written by Elias Oenal <[email protected]>, released as public domain.
*/
#ifndef EMUTEX_H
#define EMUTEX_H
/* EM_ASSERT
* Enable runtime checks
*/
//#define EM_ASSERT
#include <stdbool.h>
#include <stdatomic.h>
typedef atomic_flag emutex;
void emutex_init(emutex* const restrict mutex);
bool emutex_trylock(emutex* const restrict mutex);
void emutex_lock(emutex* const restrict mutex);
void emutex_unlock(emutex* const restrict mutex);
#endif /* EMUTEX_H */