-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathbutex_interface.h
73 lines (56 loc) · 2.1 KB
/
butex_interface.h
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
#pragma once
#include "babylon/concurrent/sched_interface.h"
#include "absl/time/clock.h"
#include "bthread/bthread.h"
#include "bthread/butex.h"
BABYLON_NAMESPACE_BEGIN
struct ButexInterface : public SchedInterface {
inline constexpr static bool futex_need_create() noexcept;
inline static uint32_t* create_futex() noexcept;
inline static void destroy_futex(uint32_t* futex) noexcept;
inline static int futex_wait(uint32_t* futex, uint32_t val,
const struct ::timespec* timeout) noexcept;
inline static int futex_wake_one(uint32_t* futex) noexcept;
inline static int futex_wake_all(uint32_t* futex) noexcept;
inline static void usleep(int64_t us) noexcept;
inline static void yield() noexcept;
};
struct BsleepInterface : public SchedInterface {
inline static void usleep(int64_t us) noexcept;
};
////////////////////////////////////////////////////////////////////////////////
inline constexpr bool ButexInterface::futex_need_create() noexcept {
return true;
}
inline uint32_t* ButexInterface::create_futex() noexcept {
return ::bthread::butex_create_checked<uint32_t>();
}
inline void ButexInterface::destroy_futex(uint32_t* butex) noexcept {
return ::bthread::butex_destroy(butex);
}
inline int ButexInterface::futex_wait(
uint32_t* butex, uint32_t val, const struct ::timespec* timeout) noexcept {
if (timeout == nullptr) {
return ::bthread::butex_wait(butex, val, nullptr);
} else {
auto abstime = ::absl::ToTimespec(::absl::Now() +
::absl::DurationFromTimespec(*timeout));
return ::bthread::butex_wait(butex, val, &abstime);
}
}
inline int ButexInterface::futex_wake_one(uint32_t* butex) noexcept {
return ::bthread::butex_wake(butex);
}
inline int ButexInterface::futex_wake_all(uint32_t* butex) noexcept {
return ::bthread::butex_wake_all(butex);
}
inline void ButexInterface::usleep(int64_t us) noexcept {
::bthread_usleep(us);
}
inline void ButexInterface::yield() noexcept {
::bthread_yield();
}
inline void BsleepInterface::usleep(int64_t us) noexcept {
::bthread_usleep(us);
}
BABYLON_NAMESPACE_END