This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
latch_darwin.h
89 lines (72 loc) · 2.21 KB
/
latch_darwin.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @file latch_darwin.h
* @author github.com/luncliff ([email protected])
*
* @brief C++ 20 <latch> for Darwin (Mac OS X)
* @see N4835
* @see latch.h
* @see POSIX thread
*
* @copyright CC BY 4.0
*/
#pragma once
#if !defined(__APPLE__)
#error "this header is only for Darwin(macOS) platform"
#endif
#include <atomic>
#include <pthread.h>
class _Pthread_mutex_t final {
pthread_mutex_t impl;
private:
_Pthread_mutex_t(const _Pthread_mutex_t&) = delete;
_Pthread_mutex_t& operator=(const _Pthread_mutex_t&) = delete;
_Pthread_mutex_t(_Pthread_mutex_t&&) = delete;
_Pthread_mutex_t& operator=(_Pthread_mutex_t&&) = delete;
public:
_Pthread_mutex_t() noexcept(false);
~_Pthread_mutex_t() noexcept(false);
pthread_mutex_t* native() noexcept {
return &impl;
}
void lock() noexcept(false);
void unlock() noexcept(false);
bool try_lock() noexcept;
};
class _Pthread_cond_t final {
pthread_cond_t impl;
private:
_Pthread_cond_t(const _Pthread_cond_t&) = delete;
_Pthread_cond_t& operator=(const _Pthread_cond_t&) = delete;
_Pthread_cond_t(_Pthread_cond_t&&) = delete;
_Pthread_cond_t& operator=(_Pthread_cond_t&&) = delete;
public:
_Pthread_cond_t() noexcept(false);
~_Pthread_cond_t() noexcept(false);
int32_t notify_one() noexcept;
int32_t notify_all() noexcept;
int32_t wait(_Pthread_mutex_t& lock);
int32_t wait_for(_Pthread_mutex_t& lock, const timespec& until);
};
namespace std {
class latch {
public:
static constexpr ptrdiff_t max() noexcept {
return 32;
}
public:
explicit latch(ptrdiff_t expected) noexcept(false);
~latch() noexcept(false) = default;
latch(const latch&) = delete;
latch& operator=(const latch&) = delete;
void count_down(ptrdiff_t update = 1) noexcept(false);
bool try_wait() const noexcept;
void wait() const noexcept(false);
void arrive_and_wait(ptrdiff_t update = 1) noexcept(false);
private:
/// @todo member alignment
std::atomic<ptrdiff_t> counter;
mutable _Pthread_mutex_t mtx;
mutable _Pthread_cond_t cv;
};
static_assert(sizeof(latch) <= 128, "the expected size is near 120");
} // namespace std