2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "threads/CriticalSection.h"
14 #include <condition_variable>
23 * This is a thin wrapper around std::condition_variable_any. It is subject
24 * to "spurious returns"
26 class ConditionVariable
29 std::condition_variable_any cond
;
30 ConditionVariable(const ConditionVariable
&) = delete;
31 ConditionVariable
& operator=(const ConditionVariable
&) = delete;
34 ConditionVariable() = default;
36 inline void wait(CCriticalSection
& lock
, std::function
<bool()> predicate
)
38 int count
= lock
.count
;
40 cond
.wait(lock
.get_underlying(), std::move(predicate
));
44 inline void wait(CCriticalSection
& lock
)
46 int count
= lock
.count
;
48 cond
.wait(lock
.get_underlying());
52 template<typename Rep
, typename Period
>
53 inline bool wait(CCriticalSection
& lock
,
54 std::chrono::duration
<Rep
, Period
> duration
,
55 std::function
<bool()> predicate
)
57 int count
= lock
.count
;
59 bool ret
= cond
.wait_for(lock
.get_underlying(), duration
, predicate
);
64 template<typename Rep
, typename Period
>
65 inline bool wait(CCriticalSection
& lock
, std::chrono::duration
<Rep
, Period
> duration
)
67 int count
= lock
.count
;
69 std::cv_status res
= cond
.wait_for(lock
.get_underlying(), duration
);
71 return res
== std::cv_status::no_timeout
;
74 inline void wait(std::unique_lock
<CCriticalSection
>& lock
, std::function
<bool()> predicate
)
76 cond
.wait(*lock
.mutex(), std::move(predicate
));
79 inline void wait(std::unique_lock
<CCriticalSection
>& lock
) { wait(*lock
.mutex()); }
81 template<typename Rep
, typename Period
>
82 inline bool wait(std::unique_lock
<CCriticalSection
>& lock
,
83 std::chrono::duration
<Rep
, Period
> duration
,
84 std::function
<bool()> predicate
)
86 return wait(*lock
.mutex(), duration
, predicate
);
89 template<typename Rep
, typename Period
>
90 inline bool wait(std::unique_lock
<CCriticalSection
>& lock
,
91 std::chrono::duration
<Rep
, Period
> duration
)
93 return wait(*lock
.mutex(), duration
);
96 inline void notifyAll()