1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22
23 #include "Tests.h"
24 #include <UnitTest++.h>
25 #include <MTScheduler.h>
26
SUITE(StackSizeTests)27 SUITE(StackSizeTests)
28 {
29 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30 struct StandartStackSizeTask
31 {
32 MT_DECLARE_TASK(StandartStackSizeTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue);
33
34 void Do(MT::FiberContext&)
35 {
36 //byte stackData[28000];
37
38 // Looks like OSX ASAN took too many stack space
39 byte stackData[20000];
40 for (uint32 i = 0; i < MT_ARRAY_SIZE(stackData); i++)
41 {
42 stackData[i] = 0x0D;
43 }
44 }
45 };
46
47 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
48 struct ExtendedStackSizeTask
49 {
50 MT_DECLARE_TASK(ExtendedStackSizeTask, MT::StackRequirements::EXTENDED, MT::TaskPriority::NORMAL, MT::Color::Red);
51
52 void Do(MT::FiberContext&)
53 {
54 byte stackData[262144];
55 for (uint32 i = 0; i < MT_ARRAY_SIZE(stackData); i++)
56 {
57 stackData[i] = 0x0D;
58 }
59 }
60
61 };
62
63
64 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65 TEST(RunStandartTasks)
66 {
67 MT::TaskScheduler scheduler;
68
69 StandartStackSizeTask tasks[100];
70 scheduler.RunAsync(MT::TaskGroup::Default(), &tasks[0], MT_ARRAY_SIZE(tasks));
71 CHECK(scheduler.WaitAll(1000));
72 }
73
74 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75 TEST(RunExtendedTasks)
76 {
77 MT::TaskScheduler scheduler;
78
79 ExtendedStackSizeTask tasks[100];
80 scheduler.RunAsync(MT::TaskGroup::Default(), &tasks[0], MT_ARRAY_SIZE(tasks));
81 CHECK(scheduler.WaitAll(1000));
82 }
83
84 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 TEST(RunMixedTasks)
86 {
87 MT::TaskScheduler scheduler;
88
89 MT::TaskPool<ExtendedStackSizeTask, 64> extendedTaskPool;
90 MT::TaskPool<StandartStackSizeTask, 64> standardTaskPool;
91
92 MT::TaskHandle taskHandles[100];
93 for (size_t i = 0; i < MT_ARRAY_SIZE(taskHandles); ++i)
94 {
95 MT::TaskHandle handle;
96
97 if (i & 1)
98 {
99 handle = extendedTaskPool.Alloc(ExtendedStackSizeTask());
100 } else
101 {
102 handle = standardTaskPool.Alloc(StandartStackSizeTask());
103 }
104 taskHandles[i] = handle;
105 }
106
107 scheduler.RunAsync(MT::TaskGroup::Default(), &taskHandles[0], MT_ARRAY_SIZE(taskHandles));
108 CHECK(scheduler.WaitAll(1000));
109 }
110
111
112
113 }
114