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 #include <MTScheduler.h>
24 #include <string.h> // for memset
25 
26 namespace MT
27 {
28 
29 #ifdef MT_INSTRUMENTED_BUILD
30 	TaskScheduler::TaskScheduler(uint32 workerThreadsCount, IProfilerEventListener* listener)
31 #else
32 	TaskScheduler::TaskScheduler(uint32 workerThreadsCount)
33 #endif
34 		: roundRobinThreadIndex(0)
35 		, startedThreadsCount(0)
36 	{
37 
38 #ifdef MT_INSTRUMENTED_BUILD
39 		profilerEventListener = listener;
40 		webServerPort = profilerWebServer.Serve(8080, 8090);
41 		//initialize start time
42 		startTime = MT::GetTimeMicroSeconds();
43 #endif
44 
45 		if (workerThreadsCount != 0)
46 		{
47 			threadsCount = MT::Clamp(workerThreadsCount, (uint32)1, (uint32)MT_MAX_THREAD_COUNT);
48 		} else
49 		{
50 			//query number of processor
51 			threadsCount = (uint32)MT::Clamp(Thread::GetNumberOfHardwareThreads() - 2, 1, (int)MT_MAX_THREAD_COUNT);
52 		}
53 
54 		// create fiber pool
55 		for (uint32 i = 0; i < MT_MAX_FIBERS_COUNT; i++)
56 		{
57 			FiberContext& context = fiberContext[i];
58 			context.fiber.Create(MT_FIBER_STACK_SIZE, FiberMain, &context);
59 			availableFibers.Push( &context );
60 		}
61 
62 		for (uint32 i = 0; i < TaskGroup::MT_MAX_GROUPS_COUNT; i++)
63 		{
64 			if (i != TaskGroup::DEFAULT)
65 			{
66 				availableGroups.Push( TaskGroup(i) );
67 			}
68 		}
69 
70 		groupStats[TaskGroup::DEFAULT].debugIsFree = false;
71 
72 		// create worker thread pool
73 		for (uint32 i = 0; i < threadsCount; i++)
74 		{
75 			threadContext[i].SetThreadIndex(i);
76 			threadContext[i].taskScheduler = this;
77 			threadContext[i].thread.Start( MT_SCHEDULER_STACK_SIZE, ThreadMain, &threadContext[i] );
78 		}
79 	}
80 
81 	TaskScheduler::~TaskScheduler()
82 	{
83 		for (uint32 i = 0; i < threadsCount; i++)
84 		{
85 			threadContext[i].state.Store(internal::ThreadState::EXIT);
86 			threadContext[i].hasNewTasksEvent.Signal();
87 		}
88 
89 		for (uint32 i = 0; i < threadsCount; i++)
90 		{
91 			threadContext[i].thread.Stop();
92 		}
93 	}
94 
95 	FiberContext* TaskScheduler::RequestFiberContext(internal::GroupedTask& task)
96 	{
97 		FiberContext *fiberContext = task.awaitingFiber;
98 		if (fiberContext)
99 		{
100 			task.awaitingFiber = nullptr;
101 			return fiberContext;
102 		}
103 
104 		if (!availableFibers.TryPopBack(fiberContext))
105 		{
106 			MT_REPORT_ASSERT("Fibers pool is empty. Too many fibers running simultaneously.");
107 		}
108 
109 		fiberContext->currentTask = task.desc;
110 		fiberContext->currentGroup = task.group;
111 		fiberContext->parentFiber = task.parentFiber;
112 		return fiberContext;
113 	}
114 
115 	void TaskScheduler::ReleaseFiberContext(FiberContext* fiberContext)
116 	{
117 		MT_ASSERT(fiberContext != nullptr, "Can't release nullptr Fiber");
118 		fiberContext->Reset();
119 		availableFibers.Push(fiberContext);
120 	}
121 
122 	FiberContext* TaskScheduler::ExecuteTask(internal::ThreadContext& threadContext, FiberContext* fiberContext)
123 	{
124 		MT_ASSERT(threadContext.thread.IsCurrentThread(), "Thread context sanity check failed");
125 
126 		MT_ASSERT(fiberContext, "Invalid fiber context");
127 		MT_ASSERT(fiberContext->currentTask.IsValid(), "Invalid task");
128 
129 		// Set actual thread context to fiber
130 		fiberContext->SetThreadContext(&threadContext);
131 
132 		// Update task status
133 		fiberContext->SetStatus(FiberTaskStatus::RUNNED);
134 
135 		MT_ASSERT(fiberContext->GetThreadContext()->thread.IsCurrentThread(), "Thread context sanity check failed");
136 
137 		void * poolUserData = fiberContext->currentTask.userData;
138 		TPoolTaskDestroy poolDestroyFunc = fiberContext->currentTask.poolDestroyFunc;
139 
140 		// Run current task code
141 		Fiber::SwitchTo(threadContext.schedulerFiber, fiberContext->fiber);
142 
143 		// If task was done
144 		FiberTaskStatus::Type taskStatus = fiberContext->GetStatus();
145 		if (taskStatus == FiberTaskStatus::FINISHED)
146 		{
147 			//destroy task (call dtor) for "fire and forget" type of task from TaskPool
148 			if (poolDestroyFunc != nullptr)
149 			{
150 				poolDestroyFunc(poolUserData);
151 			}
152 
153 
154 			TaskGroup taskGroup = fiberContext->currentGroup;
155 
156 			TaskScheduler::TaskGroupDescription  & groupDesc = threadContext.taskScheduler->GetGroupDesc(taskGroup);
157 
158 			// Update group status
159 			int groupTaskCount = groupDesc.Dec();
160 			MT_ASSERT(groupTaskCount >= 0, "Sanity check failed!");
161 			if (groupTaskCount == 0)
162 			{
163 				// Restore awaiting tasks
164 				threadContext.RestoreAwaitingTasks(taskGroup);
165 
166 				// All restored tasks can be already finished on this line.
167 				// That's why you can't release groups from worker threads, if worker thread release group, than you can't Signal to released group.
168 
169 				// Signal pending threads that group work is finished. Group can be destroyed after this call.
170 				groupDesc.Signal();
171 
172 				fiberContext->currentGroup = TaskGroup::INVALID;
173 			}
174 
175 			// Update total task count
176 			int allGroupTaskCount = threadContext.taskScheduler->allGroups.Dec();
177 			MT_ASSERT(allGroupTaskCount >= 0, "Sanity check failed!");
178 			if (allGroupTaskCount == 0)
179 			{
180 				// Notify all tasks in all group finished
181 				threadContext.taskScheduler->allGroups.Signal();
182 			}
183 
184 			FiberContext* parentFiberContext = fiberContext->parentFiber;
185 			if (parentFiberContext != nullptr)
186 			{
187 				int childrenFibersCount = parentFiberContext->childrenFibersCount.DecFetch();
188 				MT_ASSERT(childrenFibersCount >= 0, "Sanity check failed!");
189 
190 				if (childrenFibersCount == 0)
191 				{
192 					// This is a last subtask. Restore parent task
193 					MT_ASSERT(threadContext.thread.IsCurrentThread(), "Thread context sanity check failed");
194 					MT_ASSERT(parentFiberContext->GetThreadContext() == nullptr, "Inactive parent should not have a valid thread context");
195 
196 					// WARNING!! Thread context can changed here! Set actual current thread context.
197 					parentFiberContext->SetThreadContext(&threadContext);
198 
199 					MT_ASSERT(parentFiberContext->GetThreadContext()->thread.IsCurrentThread(), "Thread context sanity check failed");
200 
201 					// All subtasks is done.
202 					// Exiting and return parent fiber to scheduler
203 					return parentFiberContext;
204 				} else
205 				{
206 					// Other subtasks still exist
207 					// Exiting
208 					return nullptr;
209 				}
210 			} else
211 			{
212 				// Task is finished and no parent task
213 				// Exiting
214 				return nullptr;
215 			}
216 		}
217 
218 		MT_ASSERT(taskStatus != FiberTaskStatus::RUNNED, "Incorrect task status")
219 		return nullptr;
220 	}
221 
222 
223 	void TaskScheduler::FiberMain(void* userData)
224 	{
225 		FiberContext& fiberContext = *(FiberContext*)(userData);
226 		for(;;)
227 		{
228 			MT_ASSERT(fiberContext.currentTask.IsValid(), "Invalid task in fiber context");
229 			MT_ASSERT(fiberContext.GetThreadContext(), "Invalid thread context");
230 			MT_ASSERT(fiberContext.GetThreadContext()->thread.IsCurrentThread(), "Thread context sanity check failed");
231 
232 			fiberContext.currentTask.taskFunc( fiberContext, fiberContext.currentTask.userData );
233 
234 			fiberContext.SetStatus(FiberTaskStatus::FINISHED);
235 
236 #ifdef MT_INSTRUMENTED_BUILD
237 			fiberContext.GetThreadContext()->NotifyTaskFinished(fiberContext.currentTask);
238 #endif
239 
240 			Fiber::SwitchTo(fiberContext.fiber, fiberContext.GetThreadContext()->schedulerFiber);
241 		}
242 
243 	}
244 
245 
246 	bool TaskScheduler::TryStealTask(internal::ThreadContext& threadContext, internal::GroupedTask & task, uint32 workersCount)
247 	{
248 		if (workersCount <= 1)
249 		{
250 			return false;
251 		}
252 
253 		uint32 victimIndex = threadContext.random.Get();
254 
255 		for (uint32 attempt = 0; attempt < workersCount; attempt++)
256 		{
257 			uint32 index = victimIndex % workersCount;
258 			if (index == threadContext.workerIndex)
259 			{
260 				victimIndex++;
261 				index = victimIndex % workersCount;
262 			}
263 
264 			internal::ThreadContext& victimContext = threadContext.taskScheduler->threadContext[index];
265 			if (victimContext.queue.TryPopFront(task))
266 			{
267 				return true;
268 			}
269 
270 			victimIndex++;
271 		}
272 		return false;
273 	}
274 
275 	void TaskScheduler::ThreadMain( void* userData )
276 	{
277 		internal::ThreadContext& context = *(internal::ThreadContext*)(userData);
278 		MT_ASSERT(context.taskScheduler, "Task scheduler must be not null!");
279 
280 #ifdef MT_INSTRUMENTED_BUILD
281 		context.NotifyThreadCreate(context.workerIndex);
282 #endif
283 
284 		context.schedulerFiber.CreateFromThread(context.thread);
285 
286 		uint32 workersCount = context.taskScheduler->GetWorkerCount();
287 
288 		context.taskScheduler->startedThreadsCount.IncFetch();
289 
290 		//Spinlock until all threads started and initialized
291 		for(;;)
292 		{
293 			if (context.taskScheduler->startedThreadsCount.Load() == (int)context.taskScheduler->threadsCount)
294 			{
295 				break;
296 			}
297 			Thread::Sleep(1);
298 		}
299 
300 
301 #ifdef MT_INSTRUMENTED_BUILD
302 		context.NotifyThreadStart(context.workerIndex);
303 #endif
304 
305 		while(context.state.Load() != internal::ThreadState::EXIT)
306 		{
307 			internal::GroupedTask task;
308 			if (context.queue.TryPopBack(task) || TryStealTask(context, task, workersCount) )
309 			{
310 				// There is a new task
311 				FiberContext* fiberContext = context.taskScheduler->RequestFiberContext(task);
312 				MT_ASSERT(fiberContext, "Can't get execution context from pool");
313 				MT_ASSERT(fiberContext->currentTask.IsValid(), "Sanity check failed");
314 
315 				while(fiberContext)
316 				{
317 #ifdef MT_INSTRUMENTED_BUILD
318 					context.NotifyTaskResumed(fiberContext->currentTask);
319 #endif
320 
321 					// prevent invalid fiber resume from child tasks, before ExecuteTask is done
322 					fiberContext->childrenFibersCount.IncFetch();
323 
324 					FiberContext* parentFiber = ExecuteTask(context, fiberContext);
325 
326 					FiberTaskStatus::Type taskStatus = fiberContext->GetStatus();
327 
328 					//release guard
329 					int childrenFibersCount = fiberContext->childrenFibersCount.DecFetch();
330 
331 					// Can drop fiber context - task is finished
332 					if (taskStatus == FiberTaskStatus::FINISHED)
333 					{
334 						MT_ASSERT( childrenFibersCount == 0, "Sanity check failed");
335 						context.taskScheduler->ReleaseFiberContext(fiberContext);
336 
337 						// If parent fiber is exist transfer flow control to parent fiber, if parent fiber is null, exit
338 						fiberContext = parentFiber;
339 					} else
340 					{
341 						MT_ASSERT( childrenFibersCount >= 0, "Sanity check failed");
342 
343 						// No subtasks here and status is not finished, this mean all subtasks already finished before parent return from ExecuteTask
344 						if (childrenFibersCount == 0)
345 						{
346 							MT_ASSERT(parentFiber == nullptr, "Sanity check failed");
347 						} else
348 						{
349 							// If subtasks still exist, drop current task execution. task will be resumed when last subtask finished
350 							break;
351 						}
352 
353 						// If task is in await state drop execution. task will be resumed when RestoreAwaitingTasks called
354 						if (taskStatus == FiberTaskStatus::AWAITING_GROUP)
355 						{
356 							break;
357 						}
358 					}
359 				} //while(fiberContext)
360 
361 			} else
362 			{
363 #ifdef MT_INSTRUMENTED_BUILD
364 				uint64 waitFrom = context.taskScheduler->GetTimeStamp();
365 #endif
366 
367 				// Queue is empty and stealing attempt failed
368 				// Wait new events
369 				context.hasNewTasksEvent.Wait(2000);
370 
371 #ifdef MT_INSTRUMENTED_BUILD
372 				uint64 waitTo = context.taskScheduler->GetTimeStamp();
373 				context.NotifyThreadAwait(waitFrom, waitTo, context.workerIndex);
374 #endif
375 
376 			}
377 
378 		} // main thread loop
379 
380 #ifdef MT_INSTRUMENTED_BUILD
381 		context.NotifyThreadStop(context.workerIndex);
382 #endif
383 
384 	}
385 
386 	void TaskScheduler::RunTasksImpl(ArrayView<internal::TaskBucket>& buckets, FiberContext * parentFiber, bool restoredFromAwaitState)
387 	{
388 		// This storage is necessary to calculate how many tasks we add to different groups
389 		int newTaskCountInGroup[TaskGroup::MT_MAX_GROUPS_COUNT];
390 
391 		// Default value is 0
392 		memset(&newTaskCountInGroup[0], 0, sizeof(newTaskCountInGroup));
393 
394 		// Set parent fiber pointer
395 		// Calculate the number of tasks per group
396 		// Calculate total number of tasks
397 		size_t count = 0;
398 		for (size_t i = 0; i < buckets.Size(); ++i)
399 		{
400 			internal::TaskBucket& bucket = buckets[i];
401 			for (size_t taskIndex = 0; taskIndex < bucket.count; taskIndex++)
402 			{
403 				internal::GroupedTask & task = bucket.tasks[taskIndex];
404 
405 				task.parentFiber = parentFiber;
406 
407 				int idx = task.group.GetValidIndex();
408 				MT_ASSERT(idx >= 0 && idx < TaskGroup::MT_MAX_GROUPS_COUNT, "Invalid index");
409 				newTaskCountInGroup[idx]++;
410 			}
411 
412 			count += bucket.count;
413 		}
414 
415 		// Increments child fibers count on parent fiber
416 		if (parentFiber)
417 		{
418 			parentFiber->childrenFibersCount.AddFetch((int)count);
419 		}
420 
421 		if (restoredFromAwaitState == false)
422 		{
423 			// Increase the number of active tasks in the group using data from temporary storage
424 			for (size_t i = 0; i < TaskGroup::MT_MAX_GROUPS_COUNT; i++)
425 			{
426 				int groupNewTaskCount = newTaskCountInGroup[i];
427 				if (groupNewTaskCount > 0)
428 				{
429 					groupStats[i].Reset();
430 					groupStats[i].Add((uint32)groupNewTaskCount);
431 				}
432 			}
433 
434 			// Increments all task in progress counter
435 			allGroups.Reset();
436 			allGroups.Add((uint32)count);
437 		} else
438 		{
439 			// If task's restored from await state, counters already in correct state
440 		}
441 
442 		// Add to thread queue
443 		for (size_t i = 0; i < buckets.Size(); ++i)
444 		{
445 			int bucketIndex = roundRobinThreadIndex.IncFetch() % threadsCount;
446 			internal::ThreadContext & context = threadContext[bucketIndex];
447 
448 			internal::TaskBucket& bucket = buckets[i];
449 
450 			context.queue.PushRange(bucket.tasks, bucket.count);
451 			context.hasNewTasksEvent.Signal();
452 		}
453 	}
454 
455 	void TaskScheduler::RunAsync(TaskGroup group, TaskHandle* taskHandleArray, uint32 taskHandleCount)
456 	{
457 		MT_ASSERT(!IsWorkerThread(), "Can't use RunAsync inside Task. Use FiberContext.RunAsync() instead.");
458 
459 		ArrayView<internal::GroupedTask> buffer(MT_ALLOCATE_ON_STACK(sizeof(internal::GroupedTask) * taskHandleCount), taskHandleCount);
460 
461 		size_t bucketCount = MT::Min(threadsCount, taskHandleCount);
462 		ArrayView<internal::TaskBucket> buckets(MT_ALLOCATE_ON_STACK(sizeof(internal::TaskBucket) * bucketCount), bucketCount);
463 
464 		internal::DistibuteDescriptions(group, taskHandleArray, buffer, buckets);
465 		RunTasksImpl(buckets, nullptr, false);
466 	}
467 
468 	bool TaskScheduler::WaitGroup(TaskGroup group, uint32 milliseconds)
469 	{
470 		MT_VERIFY(IsWorkerThread() == false, "Can't use WaitGroup inside Task. Use FiberContext.WaitGroupAndYield() instead.", return false);
471 
472 		TaskScheduler::TaskGroupDescription  & groupDesc = GetGroupDesc(group);
473 		return groupDesc.Wait(milliseconds);
474 	}
475 
476 	bool TaskScheduler::WaitAll(uint32 milliseconds)
477 	{
478 		MT_VERIFY(IsWorkerThread() == false, "Can't use WaitAll inside Task.", return false);
479 
480 		return allGroups.Wait(milliseconds);
481 	}
482 
483 	bool TaskScheduler::IsEmpty()
484 	{
485 		for (uint32 i = 0; i < MT_MAX_THREAD_COUNT; i++)
486 		{
487 			if (!threadContext[i].queue.IsEmpty())
488 			{
489 				return false;
490 			}
491 		}
492 		return true;
493 	}
494 
495 	uint32 TaskScheduler::GetWorkerCount() const
496 	{
497 		return threadsCount;
498 	}
499 
500 	bool TaskScheduler::IsWorkerThread() const
501 	{
502 		for (uint32 i = 0; i < MT_MAX_THREAD_COUNT; i++)
503 		{
504 			if (threadContext[i].thread.IsCurrentThread())
505 			{
506 				return true;
507 			}
508 		}
509 		return false;
510 	}
511 
512 	TaskGroup TaskScheduler::CreateGroup()
513 	{
514 		MT_ASSERT(IsWorkerThread() == false, "Can't use CreateGroup inside Task.");
515 
516 		TaskGroup group;
517 		if (!availableGroups.TryPopBack(group))
518 		{
519 			MT_REPORT_ASSERT("Group pool is empty");
520 		}
521 
522 		int idx = group.GetValidIndex();
523 
524 		MT_ASSERT(groupStats[idx].debugIsFree == true, "Bad logic!");
525 		groupStats[idx].debugIsFree = false;
526 
527 		return group;
528 	}
529 
530 	void TaskScheduler::ReleaseGroup(TaskGroup group)
531 	{
532 		MT_ASSERT(IsWorkerThread() == false, "Can't use ReleaseGroup inside Task.");
533 		MT_ASSERT(group.IsValid(), "Invalid group ID");
534 
535 		int idx = group.GetValidIndex();
536 
537 		MT_ASSERT(groupStats[idx].debugIsFree == false, "Group already released");
538 		groupStats[idx].debugIsFree = true;
539 
540 		availableGroups.Push(group);
541 	}
542 
543 	TaskScheduler::TaskGroupDescription & TaskScheduler::GetGroupDesc(TaskGroup group)
544 	{
545 		MT_ASSERT(group.IsValid(), "Invalid group ID");
546 
547 		int idx = group.GetValidIndex();
548 		TaskScheduler::TaskGroupDescription & groupDesc = groupStats[idx];
549 
550 		MT_ASSERT(groupDesc.debugIsFree == false, "Invalid group");
551 		return groupDesc;
552 	}
553 
554 
555 #ifdef MT_INSTRUMENTED_BUILD
556 
557 	size_t TaskScheduler::GetProfilerEvents(uint32 workerIndex, ProfileEventDesc * dstBuffer, size_t dstBufferSize)
558 	{
559 		if (workerIndex >= MT_MAX_THREAD_COUNT)
560 		{
561 			return 0;
562 		}
563 
564 		size_t elementsCount = threadContext[workerIndex].profileEvents.PopAll(dstBuffer, dstBufferSize);
565 		return elementsCount;
566 	}
567 
568 	void TaskScheduler::UpdateProfiler()
569 	{
570 		profilerWebServer.Update(*this);
571 	}
572 
573 	int32 TaskScheduler::GetWebServerPort() const
574 	{
575 		return webServerPort;
576 	}
577 
578 
579 
580 #endif
581 }
582