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
107
108
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include "threads/CriticalSection.h"
#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <utility>
namespace XbmcThreads
{
/**
* This is a thin wrapper around std::condition_variable_any. It is subject
* to "spurious returns"
*/
class ConditionVariable
{
private:
std::condition_variable_any cond;
ConditionVariable(const ConditionVariable&) = delete;
ConditionVariable& operator=(const ConditionVariable&) = delete;
public:
ConditionVariable() = default;
inline void wait(CCriticalSection& lock, std::function<bool()> predicate)
{
int count = lock.count;
lock.count = 0;
cond.wait(lock.get_underlying(), std::move(predicate));
lock.count = count;
}
inline void wait(CCriticalSection& lock)
{
int count = lock.count;
lock.count = 0;
cond.wait(lock.get_underlying());
lock.count = count;
}
template<typename Rep, typename Period>
inline bool wait(CCriticalSection& lock,
std::chrono::duration<Rep, Period> duration,
std::function<bool()> predicate)
{
int count = lock.count;
lock.count = 0;
bool ret = cond.wait_for(lock.get_underlying(), duration, predicate);
lock.count = count;
return ret;
}
template<typename Rep, typename Period>
inline bool wait(CCriticalSection& lock, std::chrono::duration<Rep, Period> duration)
{
int count = lock.count;
lock.count = 0;
std::cv_status res = cond.wait_for(lock.get_underlying(), duration);
lock.count = count;
return res == std::cv_status::no_timeout;
}
inline void wait(std::unique_lock<CCriticalSection>& lock, std::function<bool()> predicate)
{
cond.wait(*lock.mutex(), std::move(predicate));
}
inline void wait(std::unique_lock<CCriticalSection>& lock) { wait(*lock.mutex()); }
template<typename Rep, typename Period>
inline bool wait(std::unique_lock<CCriticalSection>& lock,
std::chrono::duration<Rep, Period> duration,
std::function<bool()> predicate)
{
return wait(*lock.mutex(), duration, predicate);
}
template<typename Rep, typename Period>
inline bool wait(std::unique_lock<CCriticalSection>& lock,
std::chrono::duration<Rep, Period> duration)
{
return wait(*lock.mutex(), duration);
}
inline void notifyAll()
{
cond.notify_all();
}
inline void notify()
{
cond.notify_one();
}
};
}
|