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 #pragma once
24 
25 #include <MTTools.h>
26 #include <MTPlatform.h>
27 #include <MTTaskQueue.h>
28 #include <MTConcurrentRingBuffer.h>
29 #include <MTGroupedTask.h>
30 
31 
32 #ifdef MT_INSTRUMENTED_BUILD
33 
34 #define MT_SYSTEM_TASK_COLOR (MT::Color::Yellow)
35 #define MT_SYSTEM_TASK_NAME "SchedulerTask"
36 #define MT_SYSTEM_TASK_FIBER_NAME "IdleFiber"
37 
38 
39 #endif
40 
41 
42 namespace MT
43 {
44 	class FiberContext;
45 	class TaskScheduler;
46 
47 
48 
49 	namespace internal
50 	{
51 		static const size_t TASK_BUFFER_CAPACITY = 4096;
52 
53 
54 		namespace ThreadState
55 		{
56 			const uint32 ALIVE = 0;
57 			const uint32 EXIT = 1;
58 		};
59 
60 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
61 		// Thread (Scheduler fiber) context
62 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63 		struct ThreadContext
64 		{
65 			FiberContext* lastActiveFiberContext;
66 
67 			// pointer to task manager
68 			TaskScheduler* taskScheduler;
69 
70 			// thread
71 			Thread thread;
72 
73 			// scheduler fiber
74 			Fiber schedulerFiber;
75 
76 			// task queue awaiting execution
77 			TaskQueue<internal::GroupedTask> queue;
78 
79 			// new task has arrived to queue event
80 			Event hasNewTasksEvent;
81 
82 			// thread is alive or not
83 			Atomic32<int32> state;
84 
85 			// Temporary buffer, fixed size = TASK_BUFFER_CAPACITY
86 			void* descBuffer;
87 
88 			// Thread index
89 			uint32 workerIndex;
90 
91 			// Thread random number generator
92 			LcgRandom random;
93 
94 			// prevent false cache sharing between threads
95 			uint8 cacheline[64];
96 
97 			ThreadContext();
98 			~ThreadContext();
99 
100 			void SetThreadIndex(uint32 threadIndex);
101 
102 #ifdef MT_INSTRUMENTED_BUILD
103 
104 			void NotifyThreadCreate(uint32 threadIndex);
105 			void NotifyThreadStart(uint32 threadIndex);
106 			void NotifyThreadStop(uint32 threadIndex);
107 
108 			void NotifyTaskBeginExecute(MT::Color::Type debugColor, const mt_char* debugID);
109 			void NotifyTaskEndExecute(MT::Color::Type debugColor, const mt_char* debugID);
110 
111 			void NotifyThreadIdleBegin(uint32 threadIndex);
112 			void NotifyThreadIdleEnd(uint32 threadIndex);
113 
114 #endif
115 		};
116 
117 	}
118 
119 }
120