1 
2 #include <stdbool.h>
3 #include <stdint.h>
4 
5 #include <syscall.h>
6 #include <core/mutex.h>
7 
8 void
CoreMutex_Init(CoreMutex * mtx)9 CoreMutex_Init(CoreMutex *mtx)
10 {
11     mtx->lock = 0;
12 }
13 
14 
15 void
CoreMutex_Lock(CoreMutex * mtx)16 CoreMutex_Lock(CoreMutex *mtx)
17 {
18     while (__sync_lock_test_and_set(&mtx->lock, 1) == 1) {
19 	OSThreadSleep(0);
20     }
21 }
22 
23 bool
CoreMutex_TryLock(CoreMutex * mtx)24 CoreMutex_TryLock(CoreMutex *mtx)
25 {
26     if (__sync_lock_test_and_set(&mtx->lock, 1) == 1) {
27 	return false;
28     } else {
29 	return true;
30     }
31 }
32 
33 void
CoreMutex_Unlock(CoreMutex * mtx)34 CoreMutex_Unlock(CoreMutex *mtx)
35 {
36     __sync_lock_release(&mtx->lock);
37 }
38 
39