aboutsummaryrefslogtreecommitdiffstats
path: root/internal-linux.h
blob: 6e6e380e37907e526135905776be8f103e38f7ec (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
39
40
/* 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)\
	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) {\
			/* XXX handle of error in _UNLOCK */\
			abort();\
		}\
	} while (0)

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