OLD | NEW |
1 //===- subzero/src/IceThreading.h - Threading functions ---------*- C++ -*-===// | 1 //===- subzero/src/IceThreading.h - Threading 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 threading-related functions. | 10 // This file declares threading-related functions. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // and Back fields. Front==Back indicates an empty queue. | 48 // and Back fields. Front==Back indicates an empty queue. |
49 template <typename T, size_t MaxStaticSize = 128> | 49 template <typename T, size_t MaxStaticSize = 128> |
50 class BoundedProducerConsumerQueue { | 50 class BoundedProducerConsumerQueue { |
51 BoundedProducerConsumerQueue() = delete; | 51 BoundedProducerConsumerQueue() = delete; |
52 BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete; | 52 BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete; |
53 BoundedProducerConsumerQueue & | 53 BoundedProducerConsumerQueue & |
54 operator=(const BoundedProducerConsumerQueue &) = delete; | 54 operator=(const BoundedProducerConsumerQueue &) = delete; |
55 | 55 |
56 public: | 56 public: |
57 BoundedProducerConsumerQueue(bool Sequential, size_t MaxSize = MaxStaticSize) | 57 BoundedProducerConsumerQueue(bool Sequential, size_t MaxSize = MaxStaticSize) |
58 : Back(0), Front(0), MaxSize(std::min(MaxSize, MaxStaticSize)), | 58 : MaxSize(std::min(MaxSize, MaxStaticSize)), Sequential(Sequential) {} |
59 Sequential(Sequential), IsEnded(false) {} | |
60 void blockingPush(T *Item) { | 59 void blockingPush(T *Item) { |
61 { | 60 { |
62 std::unique_lock<GlobalLockType> L(Lock); | 61 std::unique_lock<GlobalLockType> L(Lock); |
63 // If the work queue is already "full", wait for a consumer to | 62 // If the work queue is already "full", wait for a consumer to |
64 // grab an element and shrink the queue. | 63 // grab an element and shrink the queue. |
65 Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; }); | 64 Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; }); |
66 push(Item); | 65 push(Item); |
67 } | 66 } |
68 GrewOrEnded.notify_one(); | 67 GrewOrEnded.notify_one(); |
69 } | 68 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 ICE_CACHELINE_BOUNDARY; | 104 ICE_CACHELINE_BOUNDARY; |
106 // GrewOrEnded is written by the producers and read by the | 105 // GrewOrEnded is written by the producers and read by the |
107 // consumers. It is notified (by the producer) when something is | 106 // consumers. It is notified (by the producer) when something is |
108 // added to the queue, in case consumers are waiting for a non-empty | 107 // added to the queue, in case consumers are waiting for a non-empty |
109 // queue. | 108 // queue. |
110 std::condition_variable GrewOrEnded; | 109 std::condition_variable GrewOrEnded; |
111 // Back is the index into WorkItems[] of where the next element will | 110 // Back is the index into WorkItems[] of where the next element will |
112 // be pushed. (More precisely, Back&MaxStaticSize is the index.) | 111 // be pushed. (More precisely, Back&MaxStaticSize is the index.) |
113 // It is written by the producers, and read by all via size() and | 112 // It is written by the producers, and read by all via size() and |
114 // empty(). | 113 // empty(). |
115 size_t Back; | 114 size_t Back = 0; |
116 | 115 |
117 ICE_CACHELINE_BOUNDARY; | 116 ICE_CACHELINE_BOUNDARY; |
118 // Shrunk is notified (by the consumer) when something is removed | 117 // Shrunk is notified (by the consumer) when something is removed |
119 // from the queue, in case a producer is waiting for the queue to | 118 // from the queue, in case a producer is waiting for the queue to |
120 // drop below maximum capacity. It is written by the consumers and | 119 // drop below maximum capacity. It is written by the consumers and |
121 // read by the producers. | 120 // read by the producers. |
122 std::condition_variable Shrunk; | 121 std::condition_variable Shrunk; |
123 // Front is the index into WorkItems[] of the oldest element, | 122 // Front is the index into WorkItems[] of the oldest element, |
124 // i.e. the next to be popped. (More precisely Front&MaxStaticSize | 123 // i.e. the next to be popped. (More precisely Front&MaxStaticSize |
125 // is the index.) It is written by the consumers, and read by all | 124 // is the index.) It is written by the consumers, and read by all |
126 // via size() and empty(). | 125 // via size() and empty(). |
127 size_t Front; | 126 size_t Front = 0; |
128 | 127 |
129 ICE_CACHELINE_BOUNDARY; | 128 ICE_CACHELINE_BOUNDARY; |
130 | 129 |
131 // MaxSize and Sequential are read by all and written by none. | 130 // MaxSize and Sequential are read by all and written by none. |
132 const size_t MaxSize; | 131 const size_t MaxSize; |
133 const bool Sequential; | 132 const bool Sequential; |
134 // IsEnded is read by the consumers, and only written once by the | 133 // IsEnded is read by the consumers, and only written once by the |
135 // producer. | 134 // producer. |
136 bool IsEnded; | 135 bool IsEnded = false; |
137 | 136 |
138 // The lock must be held when the following methods are called. | 137 // The lock must be held when the following methods are called. |
139 bool empty() const { return Front == Back; } | 138 bool empty() const { return Front == Back; } |
140 size_t size() const { return Back - Front; } | 139 size_t size() const { return Back - Front; } |
141 void push(T *Item) { | 140 void push(T *Item) { |
142 WorkItems[Back++ & MaxStaticSizeMask] = Item; | 141 WorkItems[Back++ & MaxStaticSizeMask] = Item; |
143 assert(size() <= MaxStaticSize); | 142 assert(size() <= MaxStaticSize); |
144 } | 143 } |
145 T *pop() { | 144 T *pop() { |
146 assert(!empty()); | 145 assert(!empty()); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 const uint32_t Sequence; | 198 const uint32_t Sequence; |
200 const ItemKind Kind; | 199 const ItemKind Kind; |
201 std::unique_ptr<VariableDeclarationList> GlobalInits; | 200 std::unique_ptr<VariableDeclarationList> GlobalInits; |
202 std::unique_ptr<Assembler> Function; | 201 std::unique_ptr<Assembler> Function; |
203 std::unique_ptr<Cfg> RawFunc; | 202 std::unique_ptr<Cfg> RawFunc; |
204 }; | 203 }; |
205 | 204 |
206 } // end of namespace Ice | 205 } // end of namespace Ice |
207 | 206 |
208 #endif // SUBZERO_SRC_ICETHREADING_H | 207 #endif // SUBZERO_SRC_ICETHREADING_H |
OLD | NEW |