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 #define MT_SYSTEM_FIBER_INDEX (int32)(-1)
38 
39 #endif
40 
41 
42 namespace MT
43 {
44 	class FiberContext;
45 	class TaskScheduler;
46 
47 
48 #ifdef MT_INSTRUMENTED_BUILD
49 	namespace TaskExecuteState
50 	{
51 		enum Type
52 		{
53 			START = 0,
54 			STOP = 1,
55 			RESUME = 2,
56 			SUSPEND = 3,
57 		};
58 	}
59 
60 #endif
61 
62 	namespace internal
63 	{
64 		static const size_t TASK_BUFFER_CAPACITY = 4096;
65 
66 
67 		namespace ThreadState
68 		{
69 			const uint32 ALIVE = 0;
70 			const uint32 EXIT = 1;
71 		};
72 
73 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
74 		// Thread (Scheduler fiber) context
75 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76 		struct ThreadContext
77 		{
78 			FiberContext* lastActiveFiberContext;
79 
80 			// pointer to task manager
81 			TaskScheduler* taskScheduler;
82 
83 			// thread
84 			Thread thread;
85 
86 			// thread Id
87 			ThreadId threadId;
88 
89 			// scheduler fiber
90 			Fiber schedulerFiber;
91 
92 			// task queue awaiting execution
93 			TaskQueue<internal::GroupedTask, TASK_BUFFER_CAPACITY> queue;
94 
95 			// new task has arrived to queue event
96 			Event hasNewTasksEvent;
97 
98 			// thread is alive or not
99 			Atomic32<int32> state;
100 
101 			// Temporary buffer, fixed size = TASK_BUFFER_CAPACITY
102 			void* descBuffer;
103 
104 			// Thread index
105 			uint32 workerIndex;
106 
107 			// Thread random number generator
108 			LcgRandom random;
109 
110 			bool isExternalDescBuffer;
111 
112 			// prevent false cache sharing between threads
113 			uint8 cacheline[64];
114 
115 			ThreadContext();
116 			ThreadContext(void* externalDescBuffer);
117 			~ThreadContext();
118 
119 			void SetThreadIndex(uint32 threadIndex);
120 
121 #ifdef MT_INSTRUMENTED_BUILD
122 
123 			void NotifyThreadCreated(uint32 threadIndex);
124 			void NotifyThreadStarted(uint32 threadIndex);
125 			void NotifyThreadStoped(uint32 threadIndex);
126 
127 			void NotifyTaskExecuteStateChanged(MT::Color::Type debugColor, const mt_char* debugID, TaskExecuteState::Type type, int32 fiberIndex);
128 
129 			void NotifyThreadIdleStarted(uint32 threadIndex);
130 			void NotifyThreadIdleFinished(uint32 threadIndex);
131 
132 			void NotifyWaitStarted();
133 			void NotifyWaitFinished();
134 
135 			void NotifyTemporaryWorkerThreadJoin();
136 			void NotifyTemporaryWorkerThreadLeave();
137 
138 #endif
139 
140 			static size_t GetMemoryRequrementInBytesForDescBuffer();
141 		};
142 
143 	}
144 
145 }
146