Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 1 | //===- subzero/src/IceThreading.h - Threading functions ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares threading-related functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef SUBZERO_SRC_ICETHREADING_H |
| 15 | #define SUBZERO_SRC_ICETHREADING_H |
| 16 | |
| 17 | #include <condition_variable> |
| 18 | #include <mutex> |
| 19 | |
| 20 | #include "IceDefs.h" |
| 21 | |
| 22 | namespace Ice { |
| 23 | |
| 24 | // BoundedProducerConsumerQueue is a work queue that allows multiple |
| 25 | // producers and multiple consumers. A producer adds entries using |
| 26 | // blockingPush(), and may block if the queue is "full". A producer |
| 27 | // uses notifyEnd() to indicate that no more entries will be added. A |
| 28 | // consumer removes an item using blockingPop(), which will return |
| 29 | // nullptr if notifyEnd() has been called and the queue is empty (it |
| 30 | // never returns nullptr if the queue contained any items). |
| 31 | // |
| 32 | // The MaxSize ctor arg controls the maximum size the queue can grow |
| 33 | // to (subject to a hard limit of MaxStaticSize-1). The Sequential |
| 34 | // arg indicates purely sequential execution in which the single |
| 35 | // thread should never wait(). |
| 36 | // |
| 37 | // Two condition variables are used in the implementation. |
| 38 | // GrewOrEnded signals a waiting worker that a producer has changed |
| 39 | // the state of the queue. Shrunk signals a blocked producer that a |
| 40 | // consumer has changed the state of the queue. |
| 41 | // |
| 42 | // The methods begin with Sequential-specific code to be most clear. |
| 43 | // The lock and condition variables are not used in the Sequential |
| 44 | // case. |
| 45 | // |
| 46 | // Internally, the queue is implemented as a circular array of size |
| 47 | // MaxStaticSize, where the queue boundaries are denoted by the Front |
| 48 | // and Back fields. Front==Back indicates an empty queue. |
| 49 | template <typename T, size_t MaxStaticSize = 128> |
| 50 | class BoundedProducerConsumerQueue { |
| 51 | BoundedProducerConsumerQueue() = delete; |
| 52 | BoundedProducerConsumerQueue(const BoundedProducerConsumerQueue &) = delete; |
| 53 | BoundedProducerConsumerQueue & |
| 54 | operator=(const BoundedProducerConsumerQueue &) = delete; |
| 55 | |
| 56 | public: |
| 57 | BoundedProducerConsumerQueue(bool Sequential, size_t MaxSize = MaxStaticSize) |
| 58 | : Back(0), Front(0), MaxSize(std::min(MaxSize, MaxStaticSize)), |
| 59 | Sequential(Sequential), IsEnded(false) {} |
| 60 | void blockingPush(T *Item) { |
| 61 | { |
| 62 | std::unique_lock<GlobalLockType> L(Lock); |
| 63 | // If the work queue is already "full", wait for a consumer to |
| 64 | // grab an element and shrink the queue. |
| 65 | Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; }); |
| 66 | push(Item); |
| 67 | } |
| 68 | GrewOrEnded.notify_one(); |
| 69 | } |
| 70 | T *blockingPop() { |
| 71 | T *Item = nullptr; |
| 72 | bool ShouldNotifyProducer = false; |
| 73 | { |
| 74 | std::unique_lock<GlobalLockType> L(Lock); |
| 75 | GrewOrEnded.wait(L, [this] { return IsEnded || !empty() || Sequential; }); |
| 76 | if (!empty()) { |
| 77 | Item = pop(); |
| 78 | ShouldNotifyProducer = !IsEnded; |
| 79 | } |
| 80 | } |
| 81 | if (ShouldNotifyProducer) |
| 82 | Shrunk.notify_one(); |
| 83 | return Item; |
| 84 | } |
| 85 | void notifyEnd() { |
| 86 | { |
| 87 | std::lock_guard<GlobalLockType> L(Lock); |
| 88 | IsEnded = true; |
| 89 | } |
| 90 | GrewOrEnded.notify_all(); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | const static size_t MaxStaticSizeMask = MaxStaticSize - 1; |
| 95 | static_assert(!(MaxStaticSize & (MaxStaticSize - 1)), |
| 96 | "MaxStaticSize must be a power of 2"); |
| 97 | |
| 98 | // WorkItems and Lock are read/written by all. |
| 99 | ICE_CACHELINE_BOUNDARY; |
| 100 | T *WorkItems[MaxStaticSize]; |
| 101 | ICE_CACHELINE_BOUNDARY; |
| 102 | // Lock guards access to WorkItems, Front, Back, and IsEnded. |
| 103 | GlobalLockType Lock; |
| 104 | |
| 105 | ICE_CACHELINE_BOUNDARY; |
| 106 | // GrewOrEnded is written by the producers and read by the |
| 107 | // consumers. It is notified (by the producer) when something is |
| 108 | // added to the queue, in case consumers are waiting for a non-empty |
| 109 | // queue. |
| 110 | std::condition_variable GrewOrEnded; |
| 111 | // Back is the index into WorkItems[] of where the next element will |
| 112 | // be pushed. (More precisely, Back&MaxStaticSize is the index.) |
| 113 | // It is written by the producers, and read by all via size() and |
| 114 | // empty(). |
| 115 | size_t Back; |
| 116 | |
| 117 | ICE_CACHELINE_BOUNDARY; |
| 118 | // Shrunk is notified (by the consumer) when something is removed |
| 119 | // 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 |
| 121 | // read by the producers. |
| 122 | std::condition_variable Shrunk; |
| 123 | // Front is the index into WorkItems[] of the oldest element, |
| 124 | // 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 |
| 126 | // via size() and empty(). |
| 127 | size_t Front; |
| 128 | |
| 129 | ICE_CACHELINE_BOUNDARY; |
| 130 | |
| 131 | // MaxSize and Sequential are read by all and written by none. |
| 132 | const size_t MaxSize; |
| 133 | const bool Sequential; |
| 134 | // IsEnded is read by the consumers, and only written once by the |
| 135 | // producer. |
| 136 | bool IsEnded; |
| 137 | |
| 138 | // The lock must be held when the following methods are called. |
| 139 | bool empty() const { return Front == Back; } |
| 140 | size_t size() const { return Back - Front; } |
| 141 | void push(T *Item) { |
| 142 | WorkItems[Back++ & MaxStaticSizeMask] = Item; |
| 143 | assert(size() <= MaxStaticSize); |
| 144 | } |
| 145 | T *pop() { |
| 146 | assert(!empty()); |
| 147 | return WorkItems[Front++ & MaxStaticSizeMask]; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | // EmitterWorkItem is a simple wrapper around a pointer that |
| 152 | // represents a work item to be emitted, i.e. a function or a set of |
| 153 | // global declarations and initializers, and it includes a sequence |
| 154 | // number so that work items can be emitted in a particular order for |
| 155 | // deterministic output. It acts like an interface class, but instead |
| 156 | // of making the classes of interest inherit from EmitterWorkItem, it |
| 157 | // wraps pointers to these classes. Some space is wasted compared to |
| 158 | // storing the pointers in a union, but not too much due to the work |
| 159 | // granularity. |
| 160 | class EmitterWorkItem { |
| 161 | EmitterWorkItem() = delete; |
| 162 | EmitterWorkItem(const EmitterWorkItem &) = delete; |
| 163 | EmitterWorkItem &operator=(const EmitterWorkItem &) = delete; |
| 164 | |
| 165 | public: |
| 166 | // ItemKind can be one of the following: |
| 167 | // |
| 168 | // WI_Nop: No actual work. This is a placeholder to maintain |
| 169 | // sequence numbers in case there is a translation error. |
| 170 | // |
| 171 | // WI_GlobalInits: A list of global declarations and initializers. |
| 172 | // |
| 173 | // WI_Asm: A function that has already had emitIAS() called on it. |
| 174 | // The work is transferred via the Assembler buffer, and the |
| 175 | // originating Cfg has been deleted (to recover lots of memory). |
| 176 | // |
| 177 | // WI_Cfg: A Cfg that has not yet had emit() or emitIAS() called on |
| 178 | // it. This is only used as a debugging configuration when we want |
| 179 | // to emit "readable" assembly code, possibly annotated with |
| 180 | // liveness and other information only available in the Cfg and not |
| 181 | // in the Assembler buffer. |
| 182 | enum ItemKind { WI_Nop, WI_GlobalInits, WI_Asm, WI_Cfg }; |
| 183 | // Constructor for a WI_Nop work item. |
| 184 | explicit EmitterWorkItem(uint32_t Seq); |
| 185 | // Constructor for a WI_GlobalInits work item. |
| 186 | EmitterWorkItem(uint32_t Seq, VariableDeclarationList *D); |
| 187 | // Constructor for a WI_Asm work item. |
| 188 | EmitterWorkItem(uint32_t Seq, Assembler *A); |
| 189 | // Constructor for a WI_Cfg work item. |
| 190 | EmitterWorkItem(uint32_t Seq, Cfg *F); |
| 191 | uint32_t getSequenceNumber() const { return Sequence; } |
| 192 | ItemKind getKind() const { return Kind; } |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 193 | void setGlobalInits(std::unique_ptr<VariableDeclarationList> GloblInits); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 194 | std::unique_ptr<VariableDeclarationList> getGlobalInits(); |
| 195 | std::unique_ptr<Assembler> getAsm(); |
| 196 | std::unique_ptr<Cfg> getCfg(); |
| 197 | |
| 198 | private: |
| 199 | const uint32_t Sequence; |
| 200 | const ItemKind Kind; |
| 201 | std::unique_ptr<VariableDeclarationList> GlobalInits; |
| 202 | std::unique_ptr<Assembler> Function; |
| 203 | std::unique_ptr<Cfg> RawFunc; |
| 204 | }; |
| 205 | |
| 206 | } // end of namespace Ice |
| 207 | |
| 208 | #endif // SUBZERO_SRC_ICETHREADING_H |