Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_dom_workers_queue_h__ |
michael@0 | 7 | #define mozilla_dom_workers_queue_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "Workers.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/Mutex.h" |
michael@0 | 12 | #include "nsTArray.h" |
michael@0 | 13 | |
michael@0 | 14 | BEGIN_WORKERS_NAMESPACE |
michael@0 | 15 | |
michael@0 | 16 | template <typename T, int TCount> |
michael@0 | 17 | struct StorageWithTArray |
michael@0 | 18 | { |
michael@0 | 19 | typedef nsAutoTArray<T, TCount> StorageType; |
michael@0 | 20 | |
michael@0 | 21 | static void Reverse(StorageType& aStorage) |
michael@0 | 22 | { |
michael@0 | 23 | uint32_t length = aStorage.Length(); |
michael@0 | 24 | for (uint32_t index = 0; index < length / 2; index++) { |
michael@0 | 25 | uint32_t reverseIndex = length - 1 - index; |
michael@0 | 26 | |
michael@0 | 27 | T t1 = aStorage.ElementAt(index); |
michael@0 | 28 | T t2 = aStorage.ElementAt(reverseIndex); |
michael@0 | 29 | |
michael@0 | 30 | aStorage.ReplaceElementsAt(index, 1, t2); |
michael@0 | 31 | aStorage.ReplaceElementsAt(reverseIndex, 1, t1); |
michael@0 | 32 | } |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | static bool IsEmpty(const StorageType& aStorage) |
michael@0 | 36 | { |
michael@0 | 37 | return !!aStorage.IsEmpty(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | static bool Push(StorageType& aStorage, const T& aEntry) |
michael@0 | 41 | { |
michael@0 | 42 | return !!aStorage.AppendElement(aEntry); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | static bool Pop(StorageType& aStorage, T& aEntry) |
michael@0 | 46 | { |
michael@0 | 47 | if (IsEmpty(aStorage)) { |
michael@0 | 48 | return false; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | uint32_t index = aStorage.Length() - 1; |
michael@0 | 52 | aEntry = aStorage.ElementAt(index); |
michael@0 | 53 | aStorage.RemoveElementAt(index); |
michael@0 | 54 | return true; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | static void Clear(StorageType& aStorage) |
michael@0 | 58 | { |
michael@0 | 59 | aStorage.Clear(); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | static void Compact(StorageType& aStorage) |
michael@0 | 63 | { |
michael@0 | 64 | aStorage.Compact(); |
michael@0 | 65 | } |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | class LockingWithMutex |
michael@0 | 69 | { |
michael@0 | 70 | mozilla::Mutex mMutex; |
michael@0 | 71 | |
michael@0 | 72 | protected: |
michael@0 | 73 | LockingWithMutex() |
michael@0 | 74 | : mMutex("LockingWithMutex::mMutex") |
michael@0 | 75 | { } |
michael@0 | 76 | |
michael@0 | 77 | void Lock() |
michael@0 | 78 | { |
michael@0 | 79 | mMutex.Lock(); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void Unlock() |
michael@0 | 83 | { |
michael@0 | 84 | mMutex.Unlock(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | class AutoLock |
michael@0 | 88 | { |
michael@0 | 89 | LockingWithMutex& mHost; |
michael@0 | 90 | |
michael@0 | 91 | public: |
michael@0 | 92 | AutoLock(LockingWithMutex& aHost) |
michael@0 | 93 | : mHost(aHost) |
michael@0 | 94 | { |
michael@0 | 95 | mHost.Lock(); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | ~AutoLock() |
michael@0 | 99 | { |
michael@0 | 100 | mHost.Unlock(); |
michael@0 | 101 | } |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | friend class AutoLock; |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | class NoLocking |
michael@0 | 108 | { |
michael@0 | 109 | protected: |
michael@0 | 110 | void Lock() |
michael@0 | 111 | { } |
michael@0 | 112 | |
michael@0 | 113 | void Unlock() |
michael@0 | 114 | { } |
michael@0 | 115 | |
michael@0 | 116 | class AutoLock |
michael@0 | 117 | { |
michael@0 | 118 | public: |
michael@0 | 119 | AutoLock(NoLocking& aHost) |
michael@0 | 120 | { } |
michael@0 | 121 | |
michael@0 | 122 | ~AutoLock() |
michael@0 | 123 | { } |
michael@0 | 124 | }; |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | template <typename T, |
michael@0 | 128 | int TCount = 256, |
michael@0 | 129 | class LockingPolicy = NoLocking, |
michael@0 | 130 | class StoragePolicy = StorageWithTArray<T, TCount % 2 ? |
michael@0 | 131 | TCount / 2 + 1 : |
michael@0 | 132 | TCount / 2> > |
michael@0 | 133 | class Queue : public LockingPolicy |
michael@0 | 134 | { |
michael@0 | 135 | typedef typename StoragePolicy::StorageType StorageType; |
michael@0 | 136 | typedef typename LockingPolicy::AutoLock AutoLock; |
michael@0 | 137 | |
michael@0 | 138 | StorageType mStorage1; |
michael@0 | 139 | StorageType mStorage2; |
michael@0 | 140 | |
michael@0 | 141 | StorageType* mFront; |
michael@0 | 142 | StorageType* mBack; |
michael@0 | 143 | |
michael@0 | 144 | public: |
michael@0 | 145 | Queue() |
michael@0 | 146 | : mFront(&mStorage1), mBack(&mStorage2) |
michael@0 | 147 | { } |
michael@0 | 148 | |
michael@0 | 149 | bool IsEmpty() |
michael@0 | 150 | { |
michael@0 | 151 | AutoLock lock(*this); |
michael@0 | 152 | return StoragePolicy::IsEmpty(*mFront) && |
michael@0 | 153 | StoragePolicy::IsEmpty(*mBack); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | bool Push(const T& aEntry) |
michael@0 | 157 | { |
michael@0 | 158 | AutoLock lock(*this); |
michael@0 | 159 | return StoragePolicy::Push(*mBack, aEntry); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | bool Pop(T& aEntry) |
michael@0 | 163 | { |
michael@0 | 164 | AutoLock lock(*this); |
michael@0 | 165 | if (StoragePolicy::IsEmpty(*mFront)) { |
michael@0 | 166 | StoragePolicy::Compact(*mFront); |
michael@0 | 167 | StoragePolicy::Reverse(*mBack); |
michael@0 | 168 | StorageType* tmp = mFront; |
michael@0 | 169 | mFront = mBack; |
michael@0 | 170 | mBack = tmp; |
michael@0 | 171 | } |
michael@0 | 172 | return StoragePolicy::Pop(*mFront, aEntry); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | void Clear() |
michael@0 | 176 | { |
michael@0 | 177 | AutoLock lock(*this); |
michael@0 | 178 | StoragePolicy::Clear(*mFront); |
michael@0 | 179 | StoragePolicy::Clear(*mBack); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | // XXX Do we need this? |
michael@0 | 183 | void Lock() |
michael@0 | 184 | { |
michael@0 | 185 | LockingPolicy::Lock(); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | // XXX Do we need this? |
michael@0 | 189 | void Unlock() |
michael@0 | 190 | { |
michael@0 | 191 | LockingPolicy::Unlock(); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | private: |
michael@0 | 195 | // Queue is not copyable. |
michael@0 | 196 | Queue(const Queue&); |
michael@0 | 197 | Queue & operator=(const Queue&); |
michael@0 | 198 | }; |
michael@0 | 199 | |
michael@0 | 200 | END_WORKERS_NAMESPACE |
michael@0 | 201 | |
michael@0 | 202 | #endif /* mozilla_dom_workers_queue_h__ */ |