aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/futex.h
blob: 33279ff48318b8c7caa67520ec7c94b2cdd7b03e (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once

#ifdef OS_LINUX

#include <base/types.h>

#include <bit>

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

namespace DB
{

inline Int64 futexWait(void * address, UInt32 value)
{
    return syscall(SYS_futex, address, FUTEX_WAIT_PRIVATE, value, nullptr, nullptr, 0);
}

inline Int64 futexWake(void * address, int count)
{
    return syscall(SYS_futex, address, FUTEX_WAKE_PRIVATE, count, nullptr, nullptr, 0);
}

inline void futexWaitFetch(std::atomic<UInt32> & address, UInt32 & value)
{
    futexWait(&address, value);
    value = address.load();
}

inline void futexWakeOne(std::atomic<UInt32> & address)
{
    futexWake(&address, 1);
}

inline void futexWakeAll(std::atomic<UInt32> & address)
{
     futexWake(&address, INT_MAX);
}

inline constexpr UInt32 lowerHalf(UInt64 value)
{
    return static_cast<UInt32>(value & 0xffffffffull);
}

inline constexpr UInt32 upperHalf(UInt64 value)
{
    return static_cast<UInt32>(value >> 32ull);
}

inline UInt32 * lowerHalfAddress(void * address)
{
    return reinterpret_cast<UInt32 *>(address) + (std::endian::native == std::endian::big);
}

inline UInt32 * upperHalfAddress(void * address)
{
    return reinterpret_cast<UInt32 *>(address) + (std::endian::native == std::endian::little);
}

inline void futexWaitLowerFetch(std::atomic<UInt64> & address, UInt64 & value)
{
    futexWait(lowerHalfAddress(&address), lowerHalf(value));
    value = address.load();
}

inline void futexWakeLowerOne(std::atomic<UInt64> & address)
{
    futexWake(lowerHalfAddress(&address), 1);
}

inline void futexWakeLowerAll(std::atomic<UInt64> & address)
{
    futexWake(lowerHalfAddress(&address), INT_MAX);
}

inline void futexWaitUpperFetch(std::atomic<UInt64> & address, UInt64 & value)
{
    futexWait(upperHalfAddress(&address), upperHalf(value));
    value = address.load();
}

inline void futexWakeUpperOne(std::atomic<UInt64> & address)
{
    futexWake(upperHalfAddress(&address), 1);
}

inline void futexWakeUpperAll(std::atomic<UInt64> & address)
{
    futexWake(upperHalfAddress(&address), INT_MAX);
}

}

#endif