12f083884Ss.makeev_local // The MIT License (MIT) 22f083884Ss.makeev_local // 32f083884Ss.makeev_local // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 42f083884Ss.makeev_local // 52f083884Ss.makeev_local // Permission is hereby granted, free of charge, to any person obtaining a copy 62f083884Ss.makeev_local // of this software and associated documentation files (the "Software"), to deal 72f083884Ss.makeev_local // in the Software without restriction, including without limitation the rights 82f083884Ss.makeev_local // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 92f083884Ss.makeev_local // copies of the Software, and to permit persons to whom the Software is 102f083884Ss.makeev_local // furnished to do so, subject to the following conditions: 112f083884Ss.makeev_local // 122f083884Ss.makeev_local // The above copyright notice and this permission notice shall be included in 132f083884Ss.makeev_local // all copies or substantial portions of the Software. 142f083884Ss.makeev_local // 152f083884Ss.makeev_local // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 162f083884Ss.makeev_local // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 172f083884Ss.makeev_local // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 182f083884Ss.makeev_local // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 192f083884Ss.makeev_local // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 202f083884Ss.makeev_local // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 212f083884Ss.makeev_local // THE SOFTWARE. 222f083884Ss.makeev_local 232f083884Ss.makeev_local #include "Tests.h" 242f083884Ss.makeev_local #include <UnitTest++.h> 252f083884Ss.makeev_local #include <MTScheduler.h> 262f083884Ss.makeev_local 272f083884Ss.makeev_local SUITE(WaitTests) 282f083884Ss.makeev_local { 292f083884Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 302f083884Ss.makeev_local namespace SimpleWaitFromSubtask 312f083884Ss.makeev_local { 322f083884Ss.makeev_local MT::Atomic32<int32> subTaskCount; 332f083884Ss.makeev_local MT::Atomic32<int32> taskCount; 342f083884Ss.makeev_local 352f083884Ss.makeev_local MT::TaskGroup testGroup; 362f083884Ss.makeev_local 372f083884Ss.makeev_local struct Subtask 382f083884Ss.makeev_local { 39b23bdf5aSs.makeev_local MT_DECLARE_TASK(Subtask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 402f083884Ss.makeev_local 412f083884Ss.makeev_local void Do(MT::FiberContext&) 422f083884Ss.makeev_local { 432f083884Ss.makeev_local MT::Thread::SpinSleepMilliSeconds(2); 442f083884Ss.makeev_local subTaskCount.IncFetch(); 452f083884Ss.makeev_local } 462f083884Ss.makeev_local }; 472f083884Ss.makeev_local 482f083884Ss.makeev_local 492f083884Ss.makeev_local struct Task 502f083884Ss.makeev_local { 51b23bdf5aSs.makeev_local MT_DECLARE_TASK(Task, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 522f083884Ss.makeev_local 532f083884Ss.makeev_local void Do(MT::FiberContext& ctx) 542f083884Ss.makeev_local { 552f083884Ss.makeev_local Subtask tasks[2]; 569d94a278Ss.makeev_local ctx.RunSubtasksAndYield(testGroup, &tasks[0], MT_ARRAY_SIZE(tasks)); 572f083884Ss.makeev_local 582f083884Ss.makeev_local taskCount.IncFetch(); 592f083884Ss.makeev_local 602f083884Ss.makeev_local } 612f083884Ss.makeev_local }; 622f083884Ss.makeev_local 632f083884Ss.makeev_local 642f083884Ss.makeev_local // Checks one simple task 652f083884Ss.makeev_local TEST(RunOneSimpleWaitTask) 662f083884Ss.makeev_local { 672f083884Ss.makeev_local taskCount.Store(0); 682f083884Ss.makeev_local subTaskCount.Store(0); 692f083884Ss.makeev_local 702f083884Ss.makeev_local MT::TaskScheduler scheduler; 712f083884Ss.makeev_local 722f083884Ss.makeev_local testGroup = scheduler.CreateGroup(); 732f083884Ss.makeev_local 742f083884Ss.makeev_local Task tasks[16]; 752f083884Ss.makeev_local scheduler.RunAsync(MT::TaskGroup::Default(), &tasks[0], MT_ARRAY_SIZE(tasks)); 762f083884Ss.makeev_local 772f083884Ss.makeev_local CHECK(scheduler.WaitAll(2000)); 782f083884Ss.makeev_local 792f083884Ss.makeev_local int subTaskCountFinisehd = subTaskCount.Load(); 802f083884Ss.makeev_local CHECK(subTaskCountFinisehd == MT_ARRAY_SIZE(tasks) * 2); 812f083884Ss.makeev_local 822f083884Ss.makeev_local int taskCountFinished = taskCount.Load(); 832f083884Ss.makeev_local CHECK(taskCountFinished == MT_ARRAY_SIZE(tasks)); 842f083884Ss.makeev_local } 85*3d930776Ss.makeev_local 86*3d930776Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 87*3d930776Ss.makeev_local 88*3d930776Ss.makeev_local struct LongTask 89*3d930776Ss.makeev_local { 90*3d930776Ss.makeev_local MT_DECLARE_TASK(LongTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 91*3d930776Ss.makeev_local 92*3d930776Ss.makeev_local void Do(MT::FiberContext&) 93*3d930776Ss.makeev_local { 94*3d930776Ss.makeev_local MT::ThreadBase::SpinSleepMicroSeconds(300); 95*3d930776Ss.makeev_local } 96*3d930776Ss.makeev_local }; 97*3d930776Ss.makeev_local 98*3d930776Ss.makeev_local TEST(TimeoutWaitAllTest) 99*3d930776Ss.makeev_local { 100*3d930776Ss.makeev_local MT::TaskScheduler scheduler; 101*3d930776Ss.makeev_local 102*3d930776Ss.makeev_local LongTask tasks[4096]; 103*3d930776Ss.makeev_local scheduler.RunAsync(MT::TaskGroup::Default(), &tasks[0], MT_ARRAY_SIZE(tasks)); 104*3d930776Ss.makeev_local 105*3d930776Ss.makeev_local int64 startTime = MT::GetTimeMicroSeconds(); 106*3d930776Ss.makeev_local 107*3d930776Ss.makeev_local CHECK(scheduler.WaitAll(33) == false); 108*3d930776Ss.makeev_local 109*3d930776Ss.makeev_local int64 endTime = MT::GetTimeMicroSeconds(); 110*3d930776Ss.makeev_local int32 waitTime = (int32)(endTime - startTime); 111*3d930776Ss.makeev_local printf("WaitAll(33) = %3.2f ms\n", waitTime / 1000.0f); 112*3d930776Ss.makeev_local } 113*3d930776Ss.makeev_local 114*3d930776Ss.makeev_local TEST(TimeoutWaitGroupTest) 115*3d930776Ss.makeev_local { 116*3d930776Ss.makeev_local MT::TaskScheduler scheduler; 117*3d930776Ss.makeev_local 118*3d930776Ss.makeev_local MT::TaskGroup myGroup = scheduler.CreateGroup(); 119*3d930776Ss.makeev_local 120*3d930776Ss.makeev_local LongTask tasks[4096]; 121*3d930776Ss.makeev_local scheduler.RunAsync(myGroup, &tasks[0], MT_ARRAY_SIZE(tasks)); 122*3d930776Ss.makeev_local 123*3d930776Ss.makeev_local int64 startTime = MT::GetTimeMicroSeconds(); 124*3d930776Ss.makeev_local 125*3d930776Ss.makeev_local CHECK(scheduler.WaitGroup(myGroup, 33) == false); 126*3d930776Ss.makeev_local 127*3d930776Ss.makeev_local int64 endTime = MT::GetTimeMicroSeconds(); 128*3d930776Ss.makeev_local int32 waitTime = (int32)(endTime - startTime); 129*3d930776Ss.makeev_local printf("WaitGroup(33) = %3.2f ms\n", waitTime / 1000.0f); 130*3d930776Ss.makeev_local } 131*3d930776Ss.makeev_local 132*3d930776Ss.makeev_local 133*3d930776Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 134*3d930776Ss.makeev_local 135*3d930776Ss.makeev_local 136*3d930776Ss.makeev_local MT::Atomic32<uint32> finishedTaskCount; 137*3d930776Ss.makeev_local 138*3d930776Ss.makeev_local struct SecondaryTask 139*3d930776Ss.makeev_local { 140*3d930776Ss.makeev_local MT_DECLARE_TASK(SecondaryTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 141*3d930776Ss.makeev_local 142*3d930776Ss.makeev_local void Do(MT::FiberContext&) 143*3d930776Ss.makeev_local { 144*3d930776Ss.makeev_local MT::ThreadBase::SpinSleepMicroSeconds(20); 145*3d930776Ss.makeev_local finishedTaskCount.IncFetch(); 146*3d930776Ss.makeev_local } 147*3d930776Ss.makeev_local }; 148*3d930776Ss.makeev_local 149*3d930776Ss.makeev_local 150*3d930776Ss.makeev_local struct PrimaryTask 151*3d930776Ss.makeev_local { 152*3d930776Ss.makeev_local MT::TaskGroup secondaryGroup; 153*3d930776Ss.makeev_local 154*3d930776Ss.makeev_local MT_DECLARE_TASK(PrimaryTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 155*3d930776Ss.makeev_local 156*3d930776Ss.makeev_local PrimaryTask(MT::TaskGroup _secondaryGroup) 157*3d930776Ss.makeev_local : secondaryGroup(_secondaryGroup) 158*3d930776Ss.makeev_local { 159*3d930776Ss.makeev_local } 160*3d930776Ss.makeev_local 161*3d930776Ss.makeev_local void Do(MT::FiberContext& ctx) 162*3d930776Ss.makeev_local { 163*3d930776Ss.makeev_local SecondaryTask tasks[64]; 164*3d930776Ss.makeev_local ctx.RunAsync(secondaryGroup, &tasks[0], MT_ARRAY_SIZE(tasks)); 165*3d930776Ss.makeev_local 166*3d930776Ss.makeev_local //CHECK(ctx.WaitGroup(secondaryGroup, 10000)); 167*3d930776Ss.makeev_local 168*3d930776Ss.makeev_local finishedTaskCount.IncFetch(); 169*3d930776Ss.makeev_local } 170*3d930776Ss.makeev_local }; 171*3d930776Ss.makeev_local 172*3d930776Ss.makeev_local 173*3d930776Ss.makeev_local TEST(RunOneSimpleWaitTaskFromTask) 174*3d930776Ss.makeev_local { 175*3d930776Ss.makeev_local finishedTaskCount.Store(0); 176*3d930776Ss.makeev_local 177*3d930776Ss.makeev_local MT::TaskScheduler scheduler; 178*3d930776Ss.makeev_local 179*3d930776Ss.makeev_local MT::TaskGroup mainGroup = scheduler.CreateGroup(); 180*3d930776Ss.makeev_local MT::TaskGroup secondaryGroup = scheduler.CreateGroup(); 181*3d930776Ss.makeev_local 182*3d930776Ss.makeev_local PrimaryTask task(secondaryGroup); 183*3d930776Ss.makeev_local scheduler.RunAsync(mainGroup, &task, 1); 184*3d930776Ss.makeev_local 185*3d930776Ss.makeev_local CHECK(scheduler.WaitGroup(mainGroup, 10000)); 186*3d930776Ss.makeev_local } 187*3d930776Ss.makeev_local 188*3d930776Ss.makeev_local 1892f083884Ss.makeev_local } 1902f083884Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1912f083884Ss.makeev_local } 1922f083884Ss.makeev_local 193