17 std::cout << line << std::endl;
22 Start( task, pool, priority, onCompletionFunction );
36 std::this_thread::yield();
42TEST_CASE(
"Task Pool Construction Test",
"[threading_tests]" )
51 bool init = test.
init( 0 );
55 init = test.
init( 1 );
59 init = test.
init( std::thread::hardware_concurrency() );
65TEST_CASE(
"Parallel For Test",
"[threading_tests]" )
71 TaskPool test(
"PARALLEL_FOR_TEST" );
73 const bool init = test.
init( std::thread::hardware_concurrency() );
76 constexpr U32 partitionSize = 4;
77 constexpr U32 loopCount = partitionSize * 4 + 2;
79 std::atomic_uint loopCounter = 0;
80 std::atomic_uint totalCounter = 0;
85 Parallel_For( test, descriptor, [&totalCounter, &loopCounter]( [[maybe_unused]]
const Task* parentTask,
const U32 start,
const U32 end )
noexcept
88 for (
U32 i = start; i < end; ++i )
106 const bool init = test.
init( std::thread::hardware_concurrency() );
109 bool testValue =
false;
115 PrintLine(
"TaskCallbackTest: Thread sleeping for 500ms" );
120 PrintLine(
"TaskCallbackTest: Thread waking up (" + std::to_string( durationMS ) +
"ms )" );
125 PrintLine(
"TaskCallbackTest: Callback called!" );
127 PrintLine(
"TaskCallbackTest: Value changed to: [ " + std::string( testValue ?
"true" :
"false" ) +
" ]!" );
130 CHECK_FALSE( testValue );
131 PrintLine(
"TaskCallbackTest: waiting for task!" );
135 CHECK_FALSE( testValue );
137 PrintLine(
"TaskCallbackTest: flushing queue!" );
140 PrintLine(
"TaskCallbackTest: flushing test! Value: " + std::string( testValue ?
"true" :
"false" ) );
153 _testValue.store( state, std::memory_order_release );
158 return _testValue.load( std::memory_order_acquire );
167 const F32 durationMS = Time::MicrosecondsToMilliseconds<F32>( timer.
get() - Time::ProfileTimer::overhead() );
168 PrintLine(
"threadedFunction completed in: " + std::to_string( durationMS ) +
" ms." );
170 setTestValue(
true );
174 std::atomic_bool _testValue{
false };
182 TaskPool test(
"MEMBER_CALLBACK_TEST");
184 const bool init = test.
init(
to_U8( std::thread::hardware_concurrency() ));
189 threadedFunction( parentTask );
192 CHECK_FALSE( getTestValue() );
196 setTestValue(
false );
199 CHECK_FALSE( getTestValue() );
208 const bool finalValue = getTestValue();
210 CHECK_FALSE( finalValue );
219 constexpr size_t loopCountA = 60u * 1000u;
220 constexpr U32 partitionSize = 256u;
221 constexpr U32 loopCountB = partitionSize * 8192u + 2u;
226 const bool init = test.
init(
to_U8( std::thread::hardware_concurrency() ) );
234 for (
size_t i = 0u; i < loopCountA; ++i )
239 StartAndWait( *job, test );
242 const F32 durationMS = Time::MicrosecondsToMilliseconds<F32>( timer.
get() - timerOverhead );
243 PrintLine(
"Threading speed test: " + std::to_string( loopCountA ) +
" tasks completed in: " + std::to_string( durationMS ) +
" ms." );
248 TaskPool test(
"SPEED_TEST_PARALLEL_FOR");
249 const bool init = test.
init(
to_U8( std::thread::hardware_concurrency() ) );
259 Parallel_For( test, descriptor, []( [[maybe_unused]]
const Task* parentTask, [[maybe_unused]]
const U32 start, [[maybe_unused]]
const U32 end )
264 const F32 durationMS = Time::MicrosecondsToMilliseconds<F32>( timer.
get() - timerOverhead );
265 PrintLine(
"Threading speed test (Parallel_For): " + std::to_string( loopCountB / partitionSize ) +
" partitions tasks completed in: " + std::to_string( durationMS ) +
" ms." );
270 TaskPool test(
"SPEED_TEST_PARALLEL_FOR_CURRENT_THREAD");
271 const bool init = test.
init(
to_U8( std::thread::hardware_concurrency() ) );
281 Parallel_For( test, descriptor, []( [[maybe_unused]]
const Task* parentTask, [[maybe_unused]]
const U32 start, [[maybe_unused]]
const U32 end )
287 const F32 durationMS = Time::MicrosecondsToMilliseconds<F32>( timer.
get() - timerOverhead );
288 PrintLine(
"Threading speed test (Parallel_For - use current thread): " + std::to_string( loopCountB / partitionSize ) +
" partitions tasks completed in: " + std::to_string( durationMS ) +
" ms." );
299 const bool init = test.
init( std::thread::hardware_concurrency() );
302 U32 callbackValue = 0u;
320 job =
CreateTask( [&callbackValue]( [[maybe_unused]]
const Task& parentTask )
325 StartAndWait( *job, test );
332 job =
CreateTask( [&callbackValue]( [[maybe_unused]]
const Task& parentTask )
size_t flushCallbackQueue()
Returns the number of callbacks processed.
bool init(size_t threadCount, const DELEGATE< void, const std::thread::id & > &onThreadCreate={})
D64 ElapsedMilliseconds() noexcept
constexpr T Milliseconds(T a)
void PrintLine(const std::string_view line)
void SleepThread(const D64 milliseconds)
void StartAndWait(Task &task, TaskPool &pool, const TaskPriority priority=TaskPriority::DONT_CARE, const DELEGATE< void > &onCompletionFunction={})
Handle console commands that start with a forward slash.
DELEGATE_STD< Ret, Args... > DELEGATE
std::lock_guard< mutex > LockGuard
void Wait(const Task &task, TaskPool &pool)
Task * CreateTask(Predicate &&threadedFunction, bool allowedInIdle=true)
bool Finished(const Task &task) noexcept
constexpr U8 to_U8(const T value)
TEST_CASE_METHOD(ThreadedTest, "Task Class Member Callback Test", "[threading_tests]")
void Start(Task &task, TaskPool &pool, TaskPriority priority=TaskPriority::DONT_CARE, const DELEGATE< void > &onCompletionFunction={})
void Parallel_For(TaskPool &pool, const ParallelForDescriptor &descriptor, const DELEGATE< void, const Task *, U32, U32 > &cbk)
TEST_CASE("ByteBuffer RW Bool", "[byte_buffer]")
static void ToggleFlag(const Flags flag, const bool state)
U32 _partitionSize
How many elements should we process per async task.
bool _useCurrentThread
If true, we'll process a for partition on the calling thread.
U32 _iterCount
For loop iteration count.
void setTestValue(const bool state) noexcept
void threadedFunction(const Task &parentTask)
bool getTestValue() const noexcept
#define CHECK_EQUAL(LHS, RHS)