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 <MTTaskBucket.h> 29 30 31 namespace MT 32 { 33 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 34 // Fiber task status 35 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 36 // Task can be completed for several reasons. 37 // For example task was done or someone call Yield from the Task body. 38 namespace FiberTaskStatus 39 { 40 enum Type 41 { 42 UNKNOWN = 0, 43 RUNNED = 1, 44 FINISHED = 2, 45 AWAITING_GROUP = 3, 46 AWAITING_CHILD = 4, 47 }; 48 } 49 50 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 51 // Fiber context 52 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 53 // Context passed to fiber main function 54 class FiberContext 55 { 56 private: 57 58 void RunSubtasksAndYieldImpl(WrapperArray<internal::TaskBucket>& buckets); 59 60 public: 61 62 FiberContext(); 63 64 template<class TTask> 65 void RunSubtasksAndYield(TaskGroup::Type taskGroup, const TTask* taskArray, size_t taskCount); 66 67 template<class TTask> 68 void RunAsync(TaskGroup::Type taskGroup, TTask* taskArray, size_t taskCount); 69 70 void WaitGroupAndYield(TaskGroup::Type group); 71 72 void Reset(); 73 74 void SetThreadContext(internal::ThreadContext * _threadContext); 75 internal::ThreadContext* GetThreadContext(); 76 77 void SetStatus(FiberTaskStatus::Type _taskStatus); 78 FiberTaskStatus::Type GetStatus() const; 79 80 private: 81 82 // Active thread context (null if fiber context is not executing now) 83 internal::ThreadContext * threadContext; 84 85 // Active task status 86 FiberTaskStatus::Type taskStatus; 87 88 public: 89 90 // Active task attached to this fiber 91 internal::TaskDesc currentTask; 92 93 // Active task group 94 TaskGroup::Type currentGroup; 95 96 // Number of children fibers 97 AtomicInt childrenFibersCount; 98 99 // Parent fiber 100 FiberContext* parentFiber; 101 102 // System fiber 103 Fiber fiber; 104 105 // Prevent false sharing between threads 106 uint8 cacheline[64]; 107 }; 108 109 110 } 111