FIBER_START_FUNCTION_CLASS_IMPL(TaskScheduler, FiberStart) { GlobalArgs *globalArgs = reinterpret_cast<GlobalArgs *>(arg); TaskScheduler *taskScheduler = &globalArgs->g_taskScheduler; while (!taskScheduler->m_quit.load()) { // Check if any of the waiting tasks are ready WaitingTask waitingTask; bool waitingTaskReady = false; taskScheduler->m_waitingTaskLock.lock(); auto iter = taskScheduler->m_waitingTasks.begin(); for (; iter != taskScheduler->m_waitingTasks.end(); ++iter) { if (iter->Counter->load() == iter->Value) { waitingTaskReady = true; break; } } if (waitingTaskReady) { waitingTask = *iter; // Optimization for removing an item from a vector as suggested by ryeguy on reddit // Explained here: http://stackoverflow.com/questions/4442477/remove-ith-item-from-c-stdvector/4442529#4442529 // Essentially, rather than forcing a memcpy to shift all the remaining elements down after the erase, // we move the last element into the place where the erased element was. Then we pop off the last element // Check that we're not already the last item // Move assignment to self is not defined if (iter != (--taskScheduler->m_waitingTasks.end())) { *iter = std::move(taskScheduler->m_waitingTasks.back()); } taskScheduler->m_waitingTasks.pop_back(); } taskScheduler->m_waitingTaskLock.unlock(); if (waitingTaskReady) { taskScheduler->SwitchFibers(waitingTask.Fiber); } TaskBundle nextTask; if (!taskScheduler->GetNextTask(&nextTask)) { std::this_thread::yield(); } else { nextTask.TaskToExecute.Function(&globalArgs->g_taskScheduler, &globalArgs->g_heap, &globalArgs->g_allocator, nextTask.TaskToExecute.ArgData); nextTask.Counter->fetch_sub(1); } } FTLConvertFiberToThread(FTLGetCurrentFiber()); globalArgs->g_taskScheduler.m_numActiveWorkerThreads.fetch_sub(1); FTLEndCurrentThread(); }
void TaskScheduler::FiberStart(void *arg) { GlobalArgs *globalArgs = (GlobalArgs *)arg; TaskScheduler *taskScheduler = &globalArgs->TaskScheduler; while (!taskScheduler->m_quit.load()) { // Check if any of the waiting tasks are ready WaitingTask waitingTask; bool waitingTaskReady = false; EnterCriticalSection(&taskScheduler->m_waitingTaskLock); auto iter = taskScheduler->m_waitingTasks.begin(); for ( ; iter != taskScheduler->m_waitingTasks.end(); ++iter) { if (iter->Counter->load() == iter->Value) { waitingTaskReady = true; break; } } if (waitingTaskReady) { waitingTask = *iter; taskScheduler->m_waitingTasks.erase(iter); } LeaveCriticalSection(&taskScheduler->m_waitingTaskLock); if (waitingTaskReady) { taskScheduler->SwitchFibers(waitingTask.Fiber); } TaskBundle nextTask; if (!taskScheduler->GetNextTask(&nextTask)) { SwitchToThread(); } else { nextTask.Task.Function(&globalArgs->TaskScheduler, &globalArgs->Heap, &globalArgs->Allocator, nextTask.Task.ArgData); nextTask.Counter->fetch_sub(1); } } }
void TaskScheduler::FiberStart(intptr_t arg) { TaskScheduler *taskScheduler = reinterpret_cast<TaskScheduler *>(arg); while (!taskScheduler->m_quit.load(std::memory_order_acquire)) { // Clean up from the last fiber to run on this thread taskScheduler->CleanUpOldFiber(); // Check if any of the waiting tasks are ready std::size_t waitingFiberIndex = FTL_INVALID_INDEX; for (std::size_t i = 0; i < taskScheduler->m_fiberPoolSize; ++i) { // Double lock if (!taskScheduler->m_waitingFibers[i].load(std::memory_order_relaxed)) { continue; } if (!taskScheduler->m_waitingFibers[i].load(std::memory_order_acquire)) { continue; } // Found a waiting fiber // Test if it's ready WaitingBundle *bundle = &taskScheduler->m_waitingBundles[i]; if (bundle->Counter->load(std::memory_order_relaxed) != bundle->TargetValue) { continue; } bool expected = true; if (std::atomic_compare_exchange_weak_explicit(&taskScheduler->m_waitingFibers[i], &expected, false, std::memory_order_release, std::memory_order_relaxed)) { waitingFiberIndex = i; break; } } if (waitingFiberIndex != FTL_INVALID_INDEX) { // Found a waiting task that is ready to continue ThreadLocalStorage &tls = taskScheduler->m_tls[taskScheduler->GetCurrentThreadIndex()]; tls.OldFiberIndex = tls.CurrentFiberIndex; tls.CurrentFiberIndex = waitingFiberIndex; tls.OldFiberDestination = FiberDestination::ToPool; // Switch taskScheduler->m_fibers[tls.OldFiberIndex].SwitchToFiber(&taskScheduler->m_fibers[tls.CurrentFiberIndex]); // And we're back } else { // Get a new task from the queue, and execute it TaskBundle nextTask; if (!taskScheduler->GetNextTask(&nextTask)) { // Spin } else { nextTask.TaskToExecute.Function(taskScheduler, nextTask.TaskToExecute.ArgData); nextTask.Counter->fetch_sub(1); } } } // Start the quit sequence // Switch to the thread fibers ThreadLocalStorage &tls = taskScheduler->m_tls[taskScheduler->GetCurrentThreadIndex()]; taskScheduler->m_fibers[tls.CurrentFiberIndex].SwitchToFiber(&tls.ThreadFiber); // We should never get here printf("Error: FiberStart should never return"); }