Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: src/IceUtils.h

Issue 870653002: Subzero: Initial implementation of multithreaded translation. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Enhance error codes. Use a circular buffer under the work queue. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
JF 2015/01/27 01:53:34 You should probably pluralize "producer" everywher
Jim Stichnoth 2015/01/27 05:35:11 Changed to "a producer" / "a consumer".
59 // blockingPush(), and may block if the queue is "full". The producer
60 // uses notifyEnd() to indicate that no more entries will be added.
61 // The 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 the 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, and this
82 // implies that the maximum queue size is actually MaxStaticSize-1.
83 template <typename T, size_t MaxStaticSize = 128>
84 class BoundedProducerConsumerQueue {
85 BoundedProducerConsumerQueue() = delete;
86 BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete;
87 BoundedProducerConsumerQueue &
88 operator=(const BoundedProducerConsumerQueue &) = delete;
89
90 public:
91 BoundedProducerConsumerQueue(size_t MaxSize, bool Sequential)
92 : Back(0), Front(0), MaxSize(std::min(MaxSize, MaxStaticSize - 1)),
93 Sequential(Sequential), IsEnded(false) {}
94 void blockingPush(T *Item) {
95 {
96 std::unique_lock<GlobalLockType> L(Lock);
97 // If the work queue is already "full", wait for a consumer to
98 // grab an element and shrink the queue.
99 Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; });
100 push(Item);
101 }
102 GrewOrEnded.notify_one();
103 }
104 T *blockingPop() {
105 T *Item = nullptr;
106 bool ShouldNotifyProducer = false;
107 {
108 std::unique_lock<GlobalLockType> L(Lock);
109 GrewOrEnded.wait(L, [this] { return IsEnded || !empty() || Sequential; });
110 if (!empty()) {
111 Item = pop();
112 ShouldNotifyProducer = !IsEnded;
113 }
114 }
115 if (ShouldNotifyProducer)
116 Shrunk.notify_one();
117 return Item;
118 }
119 void notifyEnd() {
120 {
121 std::lock_guard<GlobalLockType> L(Lock);
122 IsEnded = true;
123 }
124 GrewOrEnded.notify_all();
125 }
126
127 private:
128 const static size_t MaxStaticSizeMask = MaxStaticSize - 1;
129 static_assert(!(MaxStaticSize & (MaxStaticSize - 1)),
130 "MaxStaticSize must be a power of 2");
131
132 // WorkItems and Lock are read/written by all.
133 ICE_CACHELINE_BOUNDARY;
134 T *WorkItems[MaxStaticSize];
135 ICE_CACHELINE_BOUNDARY;
136 // Lock guards access to WorkItems, Front, Back, and IsEnded.
137 GlobalLockType Lock;
138
139 ICE_CACHELINE_BOUNDARY;
140 // GrewOrEnded is written by the producer and read by the
141 // consumers. It is notified (by the producer) when something is
142 // added to the queue, in case consumers are waiting for a
143 // non-empty queue.
144 std::condition_variable GrewOrEnded;
145 // Back is the index into WorkItems[] of where the next element will
146 // be pushed.
JF 2015/01/27 01:53:33 Written by the producer.
Jim Stichnoth 2015/01/27 05:35:11 Done.
147 size_t Back;
148
149 ICE_CACHELINE_BOUNDARY;
150 // Shrunk is notified (by the consumer) when something is removed
151 // from the queue, in case the producer is waiting for the queue
152 // to drop below maximum capacity. It is written by the consumers
153 // and read by the producer.
154 std::condition_variable Shrunk;
155 // Front is the index into WorkItems[] of the oldest element,
156 // i.e. the next to be popped.
JF 2015/01/27 01:53:33 Written by the consumer.
Jim Stichnoth 2015/01/27 05:35:11 Done.
157 size_t Front;
158
159 ICE_CACHELINE_BOUNDARY;
160
161 // MaxSize and Sequential are read by all and written by none.
162 const size_t MaxSize;
163 const bool Sequential;
164 // IsEnded is read by the consumers, and only written once by the
165 // producer.
166 bool IsEnded;
167
168 // The lock must be held when the following methods are called.
169 bool empty() const { return Front == Back; }
170 size_t size() const { return (Back - Front) & MaxStaticSizeMask; }
JF 2015/01/27 01:53:34 It took me a moment to convince myself that this w
Jim Stichnoth 2015/01/27 05:35:11 Me too, because it's too simple, right? Actually,
JF 2015/01/27 06:04:36 Oh yeah that's way better!
171 void push(T *Item) {
172 WorkItems[Back] = Item;
173 Back = (Back + 1) & MaxStaticSizeMask;
174 // If too many items are pushed, it "rolls over" and appears to be
175 // empty.
176 assert(!empty());
177 }
178 T *pop() {
179 assert(!empty());
180 T *Item = WorkItems[Front];
181 Front = (Front + 1) & MaxStaticSizeMask;
182 return Item;
183 }
184 };
185
57 } // end of namespace Ice 186 } // end of namespace Ice
58 187
59 #endif // SUBZERO_SRC_ICEUTILS_H 188 #endif // SUBZERO_SRC_ICEUTILS_H
OLDNEW
« src/IceDefs.h ('K') | « src/IceTranslator.cpp ('k') | src/PNaClTranslator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698