aboutsummaryrefslogtreecommitdiffstats
path: root/internal-linux.h
blob: 553a78304c8eab27add7dfac10d1afc4a3f6482b (plain) (blame)
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
38
/* See LICENSE file for copyright and license details. */

#include <linux/futex.h>
#include <sys/syscall.h>

#include "internal-llmutex.h"


typedef volatile _Atomic uint32_t MUTEX;
_Static_assert(sizeof(MUTEX) == sizeof(uint32_t));


#define INIT_MUTEX(MUTEX)\
	((void)0) /* TODO atomic_init(&(MUTEX), 0) */

#define _TRYLOCK(MUTEX)\
	!atomic_exchange(&(MUTEX), 1)

#define _LOCK(MUTEX)\
	do {\
		while (!_TRYLOCK(MUTEX))\
			_WAIT(MUTEX);\
	} while (0)

#define _UNLOCK(MUTEX)\
	do {\
		atomic_store(&(MUTEX), 0);\
		if (syscall(SYS_futex, &(MUTEX), FUTEX_PRIVATE_FLAG | FUTEX_WAKE, INT_MAX, NULL, 0, 0) < 0) {\
			/* TODO handle of error in _UNLOCK */\
		}\
	} while (0)

#define _WAIT(MUTEX)\
	do {\
		if (syscall(SYS_futex, &(MUTEX), FUTEX_PRIVATE_FLAG | FUTEX_WAIT, 1, NULL, 0, 0) < 0) {\
			/* TODO handle of error in _WAIT */\
		}\
	} while (0)