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