OLD | NEW |
1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// | 1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file declares some utility functions | 10 // This file declares some utility functions |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 T limit = static_cast<T>(1) << N; | 47 T limit = static_cast<T>(1) << N; |
48 return (0 <= value) && (value < limit); | 48 return (0 <= value) && (value < limit); |
49 } | 49 } |
50 | 50 |
51 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { | 51 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { |
52 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || | 52 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || |
53 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); | 53 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); |
54 } | 54 } |
55 }; | 55 }; |
56 | 56 |
| 57 // BoundedProducerConsumerQueue is a work queue that allows multiple |
| 58 // producers and multiple consumers. A producer adds entries using |
| 59 // blockingPush(), and may block if the queue is "full". A producer |
| 60 // uses notifyEnd() to indicate that no more entries will be added. A |
| 61 // consumer removes an item using blockingPop(), which will return |
| 62 // nullptr if notifyEnd() has been called and the queue is empty (it |
| 63 // never returns nullptr if the queue contained any items). |
| 64 // |
| 65 // The MaxSize ctor arg controls the maximum size the queue can grow |
| 66 // to (subject to a hard limit of MaxStaticSize-1). The Sequential |
| 67 // arg indicates purely sequential execution in which the single |
| 68 // thread should never wait(). |
| 69 // |
| 70 // Two condition variables are used in the implementation. |
| 71 // GrewOrEnded signals a waiting worker that a producer has changed |
| 72 // the state of the queue. Shrunk signals a blocked producer that a |
| 73 // consumer has changed the state of the queue. |
| 74 // |
| 75 // The methods begin with Sequential-specific code to be most clear. |
| 76 // The lock and condition variables are not used in the Sequential |
| 77 // case. |
| 78 // |
| 79 // Internally, the queue is implemented as a circular array of size |
| 80 // MaxStaticSize, where the queue boundaries are denoted by the Front |
| 81 // and Back fields. Front==Back indicates an empty queue. |
| 82 template <typename T, size_t MaxStaticSize = 128> |
| 83 class BoundedProducerConsumerQueue { |
| 84 BoundedProducerConsumerQueue() = delete; |
| 85 BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete; |
| 86 BoundedProducerConsumerQueue & |
| 87 operator=(const BoundedProducerConsumerQueue &) = delete; |
| 88 |
| 89 public: |
| 90 BoundedProducerConsumerQueue(size_t MaxSize, bool Sequential) |
| 91 : Back(0), Front(0), MaxSize(std::min(MaxSize, MaxStaticSize)), |
| 92 Sequential(Sequential), IsEnded(false) {} |
| 93 void blockingPush(T *Item) { |
| 94 { |
| 95 std::unique_lock<GlobalLockType> L(Lock); |
| 96 // If the work queue is already "full", wait for a consumer to |
| 97 // grab an element and shrink the queue. |
| 98 Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; }); |
| 99 push(Item); |
| 100 } |
| 101 GrewOrEnded.notify_one(); |
| 102 } |
| 103 T *blockingPop() { |
| 104 T *Item = nullptr; |
| 105 bool ShouldNotifyProducer = false; |
| 106 { |
| 107 std::unique_lock<GlobalLockType> L(Lock); |
| 108 GrewOrEnded.wait(L, [this] { return IsEnded || !empty() || Sequential; }); |
| 109 if (!empty()) { |
| 110 Item = pop(); |
| 111 ShouldNotifyProducer = !IsEnded; |
| 112 } |
| 113 } |
| 114 if (ShouldNotifyProducer) |
| 115 Shrunk.notify_one(); |
| 116 return Item; |
| 117 } |
| 118 void notifyEnd() { |
| 119 { |
| 120 std::lock_guard<GlobalLockType> L(Lock); |
| 121 IsEnded = true; |
| 122 } |
| 123 GrewOrEnded.notify_all(); |
| 124 } |
| 125 |
| 126 private: |
| 127 const static size_t MaxStaticSizeMask = MaxStaticSize - 1; |
| 128 static_assert(!(MaxStaticSize & (MaxStaticSize - 1)), |
| 129 "MaxStaticSize must be a power of 2"); |
| 130 |
| 131 // WorkItems and Lock are read/written by all. |
| 132 ICE_CACHELINE_BOUNDARY; |
| 133 T *WorkItems[MaxStaticSize]; |
| 134 ICE_CACHELINE_BOUNDARY; |
| 135 // Lock guards access to WorkItems, Front, Back, and IsEnded. |
| 136 GlobalLockType Lock; |
| 137 |
| 138 ICE_CACHELINE_BOUNDARY; |
| 139 // GrewOrEnded is written by the producers and read by the |
| 140 // consumers. It is notified (by the producer) when something is |
| 141 // added to the queue, in case consumers are waiting for a non-empty |
| 142 // queue. |
| 143 std::condition_variable GrewOrEnded; |
| 144 // Back is the index into WorkItems[] of where the next element will |
| 145 // be pushed. (More precisely, Back&MaxStaticSize is the index.) |
| 146 // It is written by the producers, and read by all via size() and |
| 147 // empty(). |
| 148 size_t Back; |
| 149 |
| 150 ICE_CACHELINE_BOUNDARY; |
| 151 // Shrunk is notified (by the consumer) when something is removed |
| 152 // from the queue, in case a producer is waiting for the queue to |
| 153 // drop below maximum capacity. It is written by the consumers and |
| 154 // read by the producers. |
| 155 std::condition_variable Shrunk; |
| 156 // Front is the index into WorkItems[] of the oldest element, |
| 157 // i.e. the next to be popped. (More precisely Front&MaxStaticSize |
| 158 // is the index.) It is written by the consumers, and read by all |
| 159 // via size() and empty(). |
| 160 size_t Front; |
| 161 |
| 162 ICE_CACHELINE_BOUNDARY; |
| 163 |
| 164 // MaxSize and Sequential are read by all and written by none. |
| 165 const size_t MaxSize; |
| 166 const bool Sequential; |
| 167 // IsEnded is read by the consumers, and only written once by the |
| 168 // producer. |
| 169 bool IsEnded; |
| 170 |
| 171 // The lock must be held when the following methods are called. |
| 172 bool empty() const { return Front == Back; } |
| 173 size_t size() const { return Back - Front; } |
| 174 void push(T *Item) { |
| 175 WorkItems[Back++ & MaxStaticSizeMask] = Item; |
| 176 assert(size() <= MaxStaticSize); |
| 177 } |
| 178 T *pop() { |
| 179 assert(!empty()); |
| 180 return WorkItems[Front++ & MaxStaticSizeMask]; |
| 181 } |
| 182 }; |
| 183 |
57 } // end of namespace Ice | 184 } // end of namespace Ice |
58 | 185 |
59 #endif // SUBZERO_SRC_ICEUTILS_H | 186 #endif // SUBZERO_SRC_ICEUTILS_H |
OLD | NEW |