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