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. The producer adds entries using | |
59 // blockingPush(), and may block if the queue is "full". The producer | |
60 // uses end() to indicate that no more entries will be added. The | |
61 // consumer removes an item using blockingPop(), which will return | |
62 // nullptr if end() has been called and the queue is empty (it never | |
63 // returns nullptr if the queue contained any items). | |
64 // | |
65 // The MaxSize ctor arg controls the maximum size the queue can grow | |
66 // to. The Sequential arg indicates purely sequential execution in | |
67 // which the single thread should never wait(). | |
68 // | |
69 // Two condition variables are used in the implementation. | |
70 // GrewOrEnded signals a waiting worker that the producer has changed | |
71 // the state of the queue. Shrunk signals a blocked producer that a | |
72 // consumer has changed the state of the queue. | |
73 // | |
74 // The methods begin with Sequential-specific code to be most clear. | |
75 // The lock and condition variables are not used in the Sequential | |
76 // case. | |
77 template <typename T> class BoundedProducerConsumerQueue { | |
78 BoundedProducerConsumerQueue() = delete; | |
79 BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete; | |
80 BoundedProducerConsumerQueue & | |
81 operator=(const BoundedProducerConsumerQueue &) = delete; | |
82 | |
83 public: | |
84 BoundedProducerConsumerQueue(size_t MaxSize, bool Sequential) | |
85 : MaxSize(MaxSize), Sequential(Sequential), IsEnded(false) { | |
86 // Do WorkQueue.reserve(MaxSize) if the underlying container | |
87 // supports it. | |
JF
2015/01/26 17:54:51
TODO
Jim Stichnoth
2015/01/27 00:56:18
Implemented a circular buffer instead.
| |
88 } | |
89 void blockingPush(T *Func) { | |
90 if (Sequential) { | |
91 WorkQueue.push(Func); | |
92 return; | |
93 } | |
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 WorkQueue.size() < MaxSize; }); | |
99 WorkQueue.push(Func); | |
100 } | |
101 GrewOrEnded.notify_one(); | |
102 } | |
103 T *blockingPop() { | |
104 if (Sequential) { | |
105 T *Func = nullptr; | |
106 if (!WorkQueue.empty()) { | |
107 Func = WorkQueue.front(); | |
108 WorkQueue.pop(); | |
109 } | |
110 return Func; | |
111 } | |
112 std::unique_lock<GlobalLockType> L(Lock); | |
113 GrewOrEnded.wait(L, [this] { return IsEnded || !WorkQueue.empty(); }); | |
114 T *Func = nullptr; | |
115 if (!WorkQueue.empty()) { | |
116 Func = WorkQueue.front(); | |
117 WorkQueue.pop(); | |
118 L.unlock(); | |
119 Shrunk.notify_one(); | |
120 } | |
121 return Func; | |
JF
2015/01/26 18:05:26
How about this:
T *Func = nullptr;
bool ShouldNot
Jim Stichnoth
2015/01/27 00:56:18
Done.
| |
122 } | |
123 void end() { | |
124 if (Sequential) | |
125 return; | |
126 { | |
127 std::unique_lock<GlobalLockType> L(Lock); | |
128 IsEnded = true; | |
129 } | |
130 GrewOrEnded.notify_all(); | |
131 } | |
132 | |
133 private: | |
134 // WorkQueue and Lock are read/written by all. | |
135 // TODO(stichnot): Since WorkQueue has an enforced maximum size, | |
136 // implement it on top of something like std::array to minimize | |
137 // contention. | |
138 alignas(MaxCacheLineSize) std::queue<T *> WorkQueue; | |
139 // Lock guards access to WorkQueue and IsEnded. | |
140 alignas(MaxCacheLineSize) GlobalLockType Lock; | |
141 | |
142 // GrewOrEnded is written by the producer and read by the | |
143 // consumers. It is notified (by the producer) when something is | |
144 // added to the queue, in case consumers are waiting for a | |
145 // non-empty queue. | |
146 alignas(MaxCacheLineSize) std::condition_variable GrewOrEnded; | |
147 | |
148 // Shrunk is notified (by the consumer) when something is removed | |
149 // from the queue, in case the producer is waiting for the queue | |
150 // to drop below maximum capacity. It is written by the consumers | |
151 // and read by the producer. | |
152 alignas(MaxCacheLineSize) std::condition_variable Shrunk; | |
153 | |
154 // MaxSize and Sequential are read by all and written by none. | |
155 alignas(MaxCacheLineSize) const size_t MaxSize; | |
156 const bool Sequential; | |
157 // IsEnded is read by the consumers, and only written once by the | |
158 // producer. | |
159 bool IsEnded; | |
160 }; | |
161 | |
57 } // end of namespace Ice | 162 } // end of namespace Ice |
58 | 163 |
59 #endif // SUBZERO_SRC_ICEUTILS_H | 164 #endif // SUBZERO_SRC_ICEUTILS_H |
OLD | NEW |