1*2f083884Ss.makeev_local // The MIT License (MIT)
2*2f083884Ss.makeev_local //
3*2f083884Ss.makeev_local // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4*2f083884Ss.makeev_local //
5*2f083884Ss.makeev_local // 	Permission is hereby granted, free of charge, to any person obtaining a copy
6*2f083884Ss.makeev_local // 	of this software and associated documentation files (the "Software"), to deal
7*2f083884Ss.makeev_local // 	in the Software without restriction, including without limitation the rights
8*2f083884Ss.makeev_local // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9*2f083884Ss.makeev_local // 	copies of the Software, and to permit persons to whom the Software is
10*2f083884Ss.makeev_local // 	furnished to do so, subject to the following conditions:
11*2f083884Ss.makeev_local //
12*2f083884Ss.makeev_local //  The above copyright notice and this permission notice shall be included in
13*2f083884Ss.makeev_local // 	all copies or substantial portions of the Software.
14*2f083884Ss.makeev_local //
15*2f083884Ss.makeev_local // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2f083884Ss.makeev_local // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2f083884Ss.makeev_local // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18*2f083884Ss.makeev_local // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2f083884Ss.makeev_local // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*2f083884Ss.makeev_local // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21*2f083884Ss.makeev_local // 	THE SOFTWARE.
22*2f083884Ss.makeev_local 
23*2f083884Ss.makeev_local #include "Tests.h"
24*2f083884Ss.makeev_local #include <UnitTest++.h>
25*2f083884Ss.makeev_local #include <MTScheduler.h>
26*2f083884Ss.makeev_local 
27*2f083884Ss.makeev_local SUITE(WaitTests)
28*2f083884Ss.makeev_local {
29*2f083884Ss.makeev_local ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30*2f083884Ss.makeev_local namespace SimpleWaitFromSubtask
31*2f083884Ss.makeev_local {
32*2f083884Ss.makeev_local 	MT::Atomic32<int32> subTaskCount;
33*2f083884Ss.makeev_local 	MT::Atomic32<int32> taskCount;
34*2f083884Ss.makeev_local 
35*2f083884Ss.makeev_local 	MT::TaskGroup testGroup;
36*2f083884Ss.makeev_local 
37*2f083884Ss.makeev_local 	struct Subtask
38*2f083884Ss.makeev_local 	{
39*2f083884Ss.makeev_local 		MT_DECLARE_TASK(Subtask, MT::StackRequirements::STANDARD, MT::Color::Blue);
40*2f083884Ss.makeev_local 
41*2f083884Ss.makeev_local 		void Do(MT::FiberContext&)
42*2f083884Ss.makeev_local 		{
43*2f083884Ss.makeev_local 			MT::Thread::SpinSleepMilliSeconds(2);
44*2f083884Ss.makeev_local 			subTaskCount.IncFetch();
45*2f083884Ss.makeev_local 		}
46*2f083884Ss.makeev_local 	};
47*2f083884Ss.makeev_local 
48*2f083884Ss.makeev_local 
49*2f083884Ss.makeev_local 	struct Task
50*2f083884Ss.makeev_local 	{
51*2f083884Ss.makeev_local 		MT_DECLARE_TASK(Task, MT::StackRequirements::STANDARD, MT::Color::Blue);
52*2f083884Ss.makeev_local 
53*2f083884Ss.makeev_local 		void Do(MT::FiberContext& ctx)
54*2f083884Ss.makeev_local 		{
55*2f083884Ss.makeev_local 			Subtask tasks[2];
56*2f083884Ss.makeev_local 			ctx.RunAsync(testGroup, &tasks[0], MT_ARRAY_SIZE(tasks));
57*2f083884Ss.makeev_local 			ctx.WaitGroupAndYield(testGroup);
58*2f083884Ss.makeev_local 
59*2f083884Ss.makeev_local 			taskCount.IncFetch();
60*2f083884Ss.makeev_local 
61*2f083884Ss.makeev_local 		}
62*2f083884Ss.makeev_local 	};
63*2f083884Ss.makeev_local 
64*2f083884Ss.makeev_local 
65*2f083884Ss.makeev_local 	// Checks one simple task
66*2f083884Ss.makeev_local 	TEST(RunOneSimpleWaitTask)
67*2f083884Ss.makeev_local 	{
68*2f083884Ss.makeev_local 		taskCount.Store(0);
69*2f083884Ss.makeev_local 		subTaskCount.Store(0);
70*2f083884Ss.makeev_local 
71*2f083884Ss.makeev_local 		MT::TaskScheduler scheduler;
72*2f083884Ss.makeev_local 
73*2f083884Ss.makeev_local 		testGroup = scheduler.CreateGroup();
74*2f083884Ss.makeev_local 
75*2f083884Ss.makeev_local 		Task tasks[16];
76*2f083884Ss.makeev_local 		scheduler.RunAsync(MT::TaskGroup::Default(), &tasks[0], MT_ARRAY_SIZE(tasks));
77*2f083884Ss.makeev_local 
78*2f083884Ss.makeev_local 		CHECK(scheduler.WaitAll(2000));
79*2f083884Ss.makeev_local 
80*2f083884Ss.makeev_local 		int subTaskCountFinisehd = subTaskCount.Load();
81*2f083884Ss.makeev_local 		CHECK(subTaskCountFinisehd == MT_ARRAY_SIZE(tasks) * 2);
82*2f083884Ss.makeev_local 
83*2f083884Ss.makeev_local 		int taskCountFinished = taskCount.Load();
84*2f083884Ss.makeev_local 		CHECK(taskCountFinished == MT_ARRAY_SIZE(tasks));
85*2f083884Ss.makeev_local 	}
86*2f083884Ss.makeev_local }
87*2f083884Ss.makeev_local ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88*2f083884Ss.makeev_local }
89*2f083884Ss.makeev_local 
90