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 } 853d930776Ss.makeev_local 863d930776Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 873d930776Ss.makeev_local 883d930776Ss.makeev_local struct LongTask 893d930776Ss.makeev_local { 903d930776Ss.makeev_local MT_DECLARE_TASK(LongTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 913d930776Ss.makeev_local 923d930776Ss.makeev_local void Do(MT::FiberContext&) 933d930776Ss.makeev_local { 94e0717aaeSs.makeev_local MT::Thread::Sleep(1); 953d930776Ss.makeev_local } 963d930776Ss.makeev_local }; 973d930776Ss.makeev_local 983d930776Ss.makeev_local TEST(TimeoutWaitAllTest) 993d930776Ss.makeev_local { 1003d930776Ss.makeev_local MT::TaskScheduler scheduler; 1013d930776Ss.makeev_local 102*4dcd71e3Ss.makeev_local LongTask tasks[4000]; 1033d930776Ss.makeev_local scheduler.RunAsync(MT::TaskGroup::Default(), &tasks[0], MT_ARRAY_SIZE(tasks)); 1043d930776Ss.makeev_local 1053d930776Ss.makeev_local int64 startTime = MT::GetTimeMicroSeconds(); 1063d930776Ss.makeev_local 1073d930776Ss.makeev_local CHECK(scheduler.WaitAll(33) == false); 1083d930776Ss.makeev_local 1093d930776Ss.makeev_local int64 endTime = MT::GetTimeMicroSeconds(); 1103d930776Ss.makeev_local int32 waitTime = (int32)(endTime - startTime); 1113d930776Ss.makeev_local printf("WaitAll(33) = %3.2f ms\n", waitTime / 1000.0f); 1123d930776Ss.makeev_local } 1133d930776Ss.makeev_local 1143d930776Ss.makeev_local TEST(TimeoutWaitGroupTest) 1153d930776Ss.makeev_local { 1163d930776Ss.makeev_local MT::TaskScheduler scheduler; 1173d930776Ss.makeev_local 1183d930776Ss.makeev_local MT::TaskGroup myGroup = scheduler.CreateGroup(); 1193d930776Ss.makeev_local 120*4dcd71e3Ss.makeev_local LongTask tasks[4000]; 1213d930776Ss.makeev_local scheduler.RunAsync(myGroup, &tasks[0], MT_ARRAY_SIZE(tasks)); 1223d930776Ss.makeev_local 1233d930776Ss.makeev_local int64 startTime = MT::GetTimeMicroSeconds(); 1243d930776Ss.makeev_local 1253d930776Ss.makeev_local CHECK(scheduler.WaitGroup(myGroup, 33) == false); 1263d930776Ss.makeev_local 1273d930776Ss.makeev_local int64 endTime = MT::GetTimeMicroSeconds(); 1283d930776Ss.makeev_local int32 waitTime = (int32)(endTime - startTime); 1293d930776Ss.makeev_local printf("WaitGroup(33) = %3.2f ms\n", waitTime / 1000.0f); 1303d930776Ss.makeev_local } 1313d930776Ss.makeev_local 1323d930776Ss.makeev_local 1333d930776Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1343d930776Ss.makeev_local 1353d930776Ss.makeev_local 1363d930776Ss.makeev_local MT::Atomic32<uint32> finishedTaskCount; 1373d930776Ss.makeev_local 1383d930776Ss.makeev_local struct SecondaryTask 1393d930776Ss.makeev_local { 1403d930776Ss.makeev_local MT_DECLARE_TASK(SecondaryTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 1413d930776Ss.makeev_local 1423d930776Ss.makeev_local void Do(MT::FiberContext&) 1433d930776Ss.makeev_local { 1443d930776Ss.makeev_local MT::ThreadBase::SpinSleepMicroSeconds(20); 1453d930776Ss.makeev_local finishedTaskCount.IncFetch(); 1463d930776Ss.makeev_local } 1473d930776Ss.makeev_local }; 1483d930776Ss.makeev_local 1493d930776Ss.makeev_local 1503d930776Ss.makeev_local struct PrimaryTask 1513d930776Ss.makeev_local { 1523d930776Ss.makeev_local MT::TaskGroup secondaryGroup; 1533d930776Ss.makeev_local 1543d930776Ss.makeev_local MT_DECLARE_TASK(PrimaryTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue); 1553d930776Ss.makeev_local 1563d930776Ss.makeev_local PrimaryTask(MT::TaskGroup _secondaryGroup) 1573d930776Ss.makeev_local : secondaryGroup(_secondaryGroup) 1583d930776Ss.makeev_local { 1593d930776Ss.makeev_local } 1603d930776Ss.makeev_local 1613d930776Ss.makeev_local void Do(MT::FiberContext& ctx) 1623d930776Ss.makeev_local { 1633d930776Ss.makeev_local SecondaryTask tasks[64]; 1643d930776Ss.makeev_local ctx.RunAsync(secondaryGroup, &tasks[0], MT_ARRAY_SIZE(tasks)); 1653d930776Ss.makeev_local 1663d930776Ss.makeev_local //CHECK(ctx.WaitGroup(secondaryGroup, 10000)); 1673d930776Ss.makeev_local 1683d930776Ss.makeev_local finishedTaskCount.IncFetch(); 1693d930776Ss.makeev_local } 1703d930776Ss.makeev_local }; 1713d930776Ss.makeev_local 1723d930776Ss.makeev_local 1733d930776Ss.makeev_local TEST(RunOneSimpleWaitTaskFromTask) 1743d930776Ss.makeev_local { 1753d930776Ss.makeev_local finishedTaskCount.Store(0); 1763d930776Ss.makeev_local 1773d930776Ss.makeev_local MT::TaskScheduler scheduler; 1783d930776Ss.makeev_local 1793d930776Ss.makeev_local MT::TaskGroup mainGroup = scheduler.CreateGroup(); 1803d930776Ss.makeev_local MT::TaskGroup secondaryGroup = scheduler.CreateGroup(); 1813d930776Ss.makeev_local 1823d930776Ss.makeev_local PrimaryTask task(secondaryGroup); 1833d930776Ss.makeev_local scheduler.RunAsync(mainGroup, &task, 1); 1843d930776Ss.makeev_local 1853d930776Ss.makeev_local CHECK(scheduler.WaitGroup(mainGroup, 10000)); 1863d930776Ss.makeev_local } 1873d930776Ss.makeev_local 1883d930776Ss.makeev_local 1892f083884Ss.makeev_local } 1902f083884Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1912f083884Ss.makeev_local } 1922f083884Ss.makeev_local 193