-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_mutex.c
37 lines (33 loc) · 906 Bytes
/
sync_mutex.c
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
36
37
#include <sh/debug.h>
#include <sh/print.h>
#include <sync/atomic.h>
#include <sync/mutex.h>
#include <util/misc.h>
static void _wait_set(sh_mutex_t *m, uint32_t new) {
uint32_t val;
while ((val = sh_get_atomic_uint32(&m->state)) != SH_MUTEX_FREE)
mcBusywait(50000);
sh_set_atomic_uint32(&m->state, new);
}
void sh_mutex_acquire(sh_mutex_t *m) {
#if defined(_SH_DEBUG_MUTEX)
sh_debugln(DEBUG_CALL_INFO, "acquiring");
sh_debug_mutex_state(m);
#endif
_wait_set(m, SH_MUTEX_HELD);
}
void sh_mutex_release(sh_mutex_t *m) {
#if defined(_SH_DEBUG_MUTEX)
sh_debugln(DEBUG_CALL_INFO, "releasing");
sh_debug_mutex_state(m);
#endif
sh_set_atomic_uint32(&m->state, SH_MUTEX_FREE);
}
void sh_debug_mutex_state(sh_mutex_t *m) {
#if defined(_SH_DEBUG_MUTEX)
uint32_t l = sh_get_atomic_uint32(&m->state);
sh_debugln(DEBUG_CALL_INFO, "[locked=%u]", l);
return;
#endif
_arg_not_used(m);
}