Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ConditionalWait.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_CONDITIONAL_WAIT_H_
34#define DVD_CONDITIONAL_WAIT_H_
35
36namespace Divide {
37class PlatformContext;
38
39void InitConditionalWait(PlatformContext&) noexcept;
41
42template<typename T, typename U>
43constexpr void assert_type( const U& )
44{
45 static_assert(std::is_same<U, T>::value, "value type not satisfied");
46}
47
48#define WAIT_FOR_CONDITION_2_ARGS(condition, yld) \
49{ \
50 assert_type<bool>(yld); \
51 \
52 if (yld) [[likely]] { \
53 while (!(condition)) { \
54 PlatformContextIdleCall(); \
55 std::this_thread::yield(); \
56 } \
57 } else { \
58 while(!(condition)) {} \
59 } \
60}
61
62#define WAIT_FOR_CONDITION_1_ARGS(condition) WAIT_FOR_CONDITION_2_ARGS(condition, true)
63
64#define ___DETAIL_WAIT_FOR_CONDITION(...) EXP(GET_3RD_ARG(__VA_ARGS__, WAIT_FOR_CONDITION_2_ARGS, WAIT_FOR_CONDITION_1_ARGS, ))
65#define WAIT_FOR_CONDITION(...) EXP(___DETAIL_WAIT_FOR_CONDITION(__VA_ARGS__)(__VA_ARGS__)); static_assert(true, "")
66
67#define WAIT_FOR_CONDITION_TIMEOUT_3_ARGS(condition, timeoutMS, yld) \
68{ \
69 assert_type<bool>(yld); \
70 assert_type<D64>(timeoutMS); \
71 \
72 if (timeoutMS >= 0.0) { \
73 const D64 start = Time::App::ElapsedMilliseconds(); \
74 \
75 while (!(condition)) { \
76 PlatformContextIdleCall(); \
77 if (Time::App::ElapsedMilliseconds() - start >= timeoutMS) \
78 { \
79 break; \
80 } \
81 \
82 if (yld) [[likely]] { \
83 std::this_thread::yield(); \
84 } \
85 } \
86 } else { \
87 WAIT_FOR_CONDITION(condition, yld); \
88 } \
89}
90
91#define WAIT_FOR_CONDITION_TIMEOUT_2_ARGS(condition, timeoutMS) WAIT_FOR_CONDITION_TIMEOUT_3_ARGS(condition, timeoutMS, true)
92#define WAIT_FOR_CONDITION_TIMEOUT_1_ARGS(condition) WAIT_FOR_CONDITION_TIMEOUT_3_ARGS(condition, 1.0, true)
93
94#define ___DETAIL_WAIT_FOR_CONDITION_TIMEOUT(...) EXP(GET_4TH_ARG(__VA_ARGS__, WAIT_FOR_CONDITION_TIMEOUT_3_ARGS, WAIT_FOR_CONDITION_TIMEOUT_2_ARGS, WAIT_FOR_CONDITION_TIMEOUT_1_ARGS, ))
95#define WAIT_FOR_CONDITION_TIMEOUT(...) EXP(___DETAIL_WAIT_FOR_CONDITION_TIMEOUT(__VA_ARGS__)(__VA_ARGS__)); static_assert(true, "")
96
97#define WAIT_FOR_CONDITION_CALLBACK_4_ARGS(condition, cbk, param, yld) \
98{ \
99 assert_type<bool>(yld); \
100 \
101 while (!(condition)) { \
102 cbk(param); \
103 PlatformContextIdleCall(); \
104 if (yld) [[likely]] { \
105 std::this_thread::yield(); \
106 } \
107 } \
108}
109
110#define WAIT_FOR_CONDITION_CALLBACK_3_ARGS(condition, cbk, param) WAIT_FOR_CONDITION_CALLBACK_4_ARGS(condition, cbk, param, true)
111#define WAIT_FOR_CONDITION_CALLBACK_2_ARGS(condition, cbk) WAIT_FOR_CONDITION_CALLBACK_3_ARGS(condition, cbk, void, true)
112#define WAIT_FOR_CONDITION_CALLBACK_1_ARGS(condition) WAIT_FOR_CONDITION(condition)
113
114#define ___DETAIL_WAIT_FOR_CONDITION_CALLBACK(...) EXP(GET_4TH_ARG(__VA_ARGS__, WAIT_FOR_CONDITION_CALLBACK_3_ARGS, WAIT_FOR_CONDITION_CALLBACK_2_ARGS, WAIT_FOR_CONDITION_CALLBACK_1_ARGS, ))
115#define WAIT_FOR_CONDITION_CALLBACK(...) EXP(___DETAIL_WAIT_FOR_CONDITION_CALLBACK(__VA_ARGS__)(__VA_ARGS__)); static_assert(true, "")
116
117#define WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_5_ARGS(condition, cbk, param, timeoutMS, yld) \
118{ \
119 assert_type<bool>(yld); \
120 assert_type<D64>(timeoutMS); \
121 \
122 if ((timeoutMS) >= 0.0) { \
123 const D64 start = Time::ElapsedMilliseconds(true); \
124 \
125 while (!(condition)) { \
126 cbk(param); \
127 PlatformContextIdleCall(); \
128 \
129 if (Time::ElapsedMilliseconds(true) - start >= (timeoutMS)) { \
130 break; \
131 } \
132 \
133 if (yld) [[likely]] { \
134 std::this_thread::yield(); \
135 } \
136 } \
137 } else { \
138 WAIT_FOR_CONDITION_CALLBACK(condition, cbk, param, yld); \
139 } \
140}
141
142#define WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_4_ARGS(condition, cbk, param, timeoutMS) WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_5_ARGS(condition, cbk, param, timeoutMS, true)
143#define WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_3_ARGS(condition, cbk, param) WAIT_FOR_CONDITION_CALLBACK(condition, cbk, param)
144#define WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_2_ARGS(condition, cbk) WAIT_FOR_CONDITION_CALLBACK(condition, cbk)
145#define WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_1_ARGS(condition) WAIT_FOR_CONDITION(condition)
146
147#define ___DETAIL_WAIT_FOR_CONDITION_CALLBACK_TIMEOUT(...) EXP(GET_5TH_ARG(__VA_ARGS__, WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_4_ARGS, WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_3_ARGS, WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_2_ARGS, WAIT_FOR_CONDITION_CALLBACK_TIMEOUT_1_ARGS, ))
148#define WAIT_FOR_CONDITION_CALLBACK_TIMEOUT(...) EXP(___DETAIL_WAIT_FOR_CONDITION_CALLBACK_TIMEOUT(__VA_ARGS__)(__VA_ARGS__)); static_assert(true, "")
149
150};
151
152#endif //DVD_CONDITIONAL_WAIT_H_
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
void PlatformContextIdleCall()
constexpr void assert_type(const U &)
void InitConditionalWait(PlatformContext &context) noexcept