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