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 <MTConcurrentQueueLIFO.h>
28 #include <MTConcurrentRingBuffer.h>
29 #include <MTGroupedTask.h>
30 
31 namespace MT
32 {
33 	class FiberContext;
34 	class TaskScheduler;
35 
36 
37 
38 	namespace internal
39 	{
40 		static const size_t TASK_BUFFER_CAPACITY = 4096;
41 
42 
43 		namespace ThreadState
44 		{
45 			const uint32 ALIVE = 0;
46 			const uint32 EXIT = 1;
47 		};
48 
49 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50 		// Thread (Scheduler fiber) context
51 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52 		struct ThreadContext
53 		{
54 			FiberContext* lastActiveFiberContext;
55 
56 			// pointer to task manager
57 			TaskScheduler* taskScheduler;
58 
59 			// thread
60 			Thread thread;
61 
62 			// scheduler fiber
63 			Fiber schedulerFiber;
64 
65 			// task queue awaiting execution
66 			ConcurrentQueueLIFO<internal::GroupedTask> queue;
67 
68 			// new task was arrived to queue event
69 			Event hasNewTasksEvent;
70 
71 			// whether thread is alive
72 			Atomic32<int32> state;
73 
74 			// Temporary buffer, fixed size = TASK_BUFFER_CAPACITY
75 			void* descBuffer;
76 
77 			// Thread index
78 			uint32 workerIndex;
79 
80 			// Thread random number generator
81 			LcgRandom random;
82 
83 			// prevent false sharing between threads
84 			uint8 cacheline[64];
85 
86 			ThreadContext();
87 			~ThreadContext();
88 
89 			void RestoreAwaitingTasks(TaskGroup taskGroup);
90 
91 			void SetThreadIndex(uint32 threadIndex);
92 
93 #ifdef MT_INSTRUMENTED_BUILD
94 
95 			void NotifyThreadCreate(uint32 threadIndex);
96 			void NotifyThreadStart(uint32 threadIndex);
97 			void NotifyThreadStop(uint32 threadIndex);
98 
99 			void NotifyTaskFinished(const internal::TaskDesc & desc);
100 			void NotifyTaskResumed(const internal::TaskDesc & desc);
101 			void NotifyTaskYielded(const internal::TaskDesc & desc);
102 
103 			void NotifyThreadIdleBegin(uint32 threadIndex);
104 			void NotifyThreadIdleEnd(uint32 threadIndex);
105 
106 #endif
107 		};
108 
109 	}
110 
111 }
112