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 <MTStackArray.h> 29 #include <MTArrayView.h> 30 31 32 namespace MT 33 { 34 class FiberContext; 35 36 namespace internal 37 { 38 struct ThreadContext; 39 } 40 41 #define ALIVE_STAMP (0x33445566) 42 43 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 44 // Task group 45 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 46 // Application can assign task group to task and later wait until group was finished. 47 class TaskGroup 48 { 49 AtomicInt inProgressTaskCount; 50 Event allDoneEvent; 51 52 //Tasks awaiting group through FiberContext::WaitGroupAndYield call 53 ConcurrentQueueLIFO<FiberContext*> waitTasksQueue; 54 55 uint32 aliveStamp; 56 57 public: 58 59 TaskGroup() 60 { 61 aliveStamp = 0x33445566; 62 63 inProgressTaskCount.Set(0); 64 allDoneEvent.Create( EventReset::MANUAL, true ); 65 } 66 67 ~TaskGroup() 68 { 69 aliveStamp = 0xFDFDFDFD; 70 } 71 72 int GetTaskCount() const 73 { 74 MT_ASSERT(this != nullptr, "AV exception here"); 75 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 76 77 return inProgressTaskCount.Get(); 78 } 79 80 ConcurrentQueueLIFO<FiberContext*> & GetWaitQueue() 81 { 82 MT_ASSERT(this != nullptr, "AV exception here"); 83 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 84 85 return waitTasksQueue; 86 } 87 88 int Dec() 89 { 90 MT_ASSERT(this != nullptr, "AV exception here"); 91 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 92 93 return inProgressTaskCount.Dec(); 94 } 95 96 int Inc() 97 { 98 MT_ASSERT(this != nullptr, "AV exception here"); 99 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 100 101 return inProgressTaskCount.Inc(); 102 } 103 104 int Add(int sum) 105 { 106 MT_ASSERT(this != nullptr, "AV exception here"); 107 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 108 109 return inProgressTaskCount.Add(sum); 110 } 111 112 void Signal() 113 { 114 MT_ASSERT(this != nullptr, "AV exception here"); 115 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 116 117 allDoneEvent.Signal(); 118 } 119 120 void Reset() 121 { 122 MT_ASSERT(this != nullptr, "AV exception here"); 123 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 124 125 allDoneEvent.Reset(); 126 } 127 128 bool Wait(uint32 milliseconds) 129 { 130 MT_ASSERT(this != nullptr, "AV exception here"); 131 MT_ASSERT(aliveStamp == ALIVE_STAMP, "Group already destroyed"); 132 133 return allDoneEvent.Wait(milliseconds); 134 } 135 136 137 138 }; 139 140 141 142 } 143