-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition_variable.cpp
More file actions
106 lines (87 loc) · 2.6 KB
/
Copy pathcondition_variable.cpp
File metadata and controls
106 lines (87 loc) · 2.6 KB
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
98
99
100
101
102
103
104
105
106
/**
* Copyright 2025, Aleksandar Colic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "condition_variable.hpp"
#include <condition_variable>
#include <memory>
#include <mutex>
#include <vector>
#include "mutex.hpp"
#include "spinlock.hpp"
#include "util.hpp"
#include "worker.hpp"
namespace ums {
Condition_variable::~Condition_variable() noexcept
{
notify_all();
}
void Condition_variable::wait(std::unique_lock<Mutex>& lock)
{
wait_internal(lock, Time_point::max());
}
void Condition_variable::notify_one() noexcept
{
const std::scoped_lock<Spinlock> lock{m_waiters_lock};
if (!m_waiters.empty())
m_waiters.front().notify_waiter();
}
void Condition_variable::notify_all() noexcept
{
const std::scoped_lock<Spinlock> lock{m_waiters_lock};
for (auto& waiter : m_waiters)
waiter.notify_waiter();
}
void Condition_variable::add_waiter()
{
const std::scoped_lock<Spinlock> lock{m_waiters_lock};
m_waiters.push_back(*worker::get());
}
void Condition_variable::remove_waiter()
{
const std::scoped_lock<Spinlock> lock{m_waiters_lock};
m_waiters.remove(*worker::get());
}
void Condition_variable::wait_internal(std::unique_lock<Mutex>& lock, const Time_point& abs_time)
{
add_waiter();
{
worker::get()->set_wait_info(false, abs_time);
const Scoped_unlock<std::unique_lock<Mutex>> unlock{lock};
worker::get()->wait_cond_or_sleep();
}
remove_waiter();
}
std::cv_status Condition_variable::wait_until_internal(std::unique_lock<Mutex>& lock,
const Time_point& abs_time)
{
wait_internal(lock, abs_time);
return now() >= abs_time ? std::cv_status::timeout : std::cv_status::no_timeout;
}
Condition_variable_any::Condition_variable_any() : m_mtx{std::make_shared<Mutex>()} {}
void Condition_variable_any::notify_one() noexcept
{
{
const std::scoped_lock<Mutex> lock{*m_mtx};
}
m_cv.notify_one();
}
void Condition_variable_any::notify_all() noexcept
{
{
const std::scoped_lock<Mutex> lock{*m_mtx};
}
m_cv.notify_all();
}
} // namespace ums