1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // This file contains a number of synchronization primitives for concurrency.
16 //
17 // You may be tempted to change this code to unlock the mutex before calling
18 // std::condition_variable::notify_[one,all]. Please read
19 // https://issuetracker.google.com/issues/133135427 before making this sort of
20 // change.
21
22 #ifndef sw_Synchronization_hpp
23 #define sw_Synchronization_hpp
24
25 #include "Debug.hpp"
26
27 #include <assert.h>
28 #include <chrono>
29 #include <condition_variable>
30 #include <queue>
31
32 #include "marl/event.h"
33 #include "marl/mutex.h"
34 #include "marl/waitgroup.h"
35
36 namespace sw {
37
38 // CountedEvent is an event that is signalled when the internal counter is
39 // decremented and reaches zero.
40 // The counter is incremented with calls to add() and decremented with calls to
41 // done().
42 class CountedEvent
43 {
44 public:
45 // Constructs the CountedEvent with the initial signalled state set to the
46 // provided value.
CountedEvent(bool signalled=false)47 CountedEvent(bool signalled = false)
48 : ev(marl::Event::Mode::Manual, signalled)
49 {}
50
51 // add() increments the internal counter.
52 // add() must not be called when the event is already signalled.
add() const53 void add() const
54 {
55 ASSERT(!ev.isSignalled());
56 wg.add();
57 }
58
59 // done() decrements the internal counter, signalling the event if the new
60 // counter value is zero.
61 // done() must not be called when the event is already signalled.
done() const62 void done() const
63 {
64 ASSERT(!ev.isSignalled());
65 if(wg.done())
66 {
67 ev.signal();
68 }
69 }
70
71 // reset() clears the signal state.
72 // done() must not be called when the internal counter is non-zero.
reset() const73 void reset() const
74 {
75 ev.clear();
76 }
77
78 // signalled() returns the current signal state.
signalled() const79 bool signalled() const
80 {
81 return ev.isSignalled();
82 }
83
84 // wait() waits until the event is signalled.
wait() const85 void wait() const
86 {
87 ev.wait();
88 }
89
90 // wait() waits until the event is signalled or the timeout is reached.
91 // If the timeout was reached, then wait() return false.
92 template<class CLOCK, class DURATION>
wait(const std::chrono::time_point<CLOCK,DURATION> & timeout) const93 bool wait(const std::chrono::time_point<CLOCK, DURATION> &timeout) const
94 {
95 return ev.wait_until(timeout);
96 }
97
98 // event() returns the internal marl event.
event()99 const marl::Event &event() { return ev; }
100
101 private:
102 const marl::WaitGroup wg;
103 const marl::Event ev;
104 };
105
106 // Chan is a thread-safe FIFO queue of type T.
107 // Chan takes its name after Golang's chan.
108 template<typename T>
109 class Chan
110 {
111 public:
112 Chan();
113
114 // take returns the next item in the chan, blocking until an item is
115 // available.
116 T take();
117
118 // tryTake returns a <T, bool> pair.
119 // If the chan is not empty, then the next item and true are returned.
120 // If the chan is empty, then a default-initialized T and false are returned.
121 std::pair<T, bool> tryTake();
122
123 // put places an item into the chan, blocking if the chan is bounded and
124 // full.
125 void put(const T &v);
126
127 // Returns the number of items in the chan.
128 // Note: that this may change as soon as the function returns, so should
129 // only be used for debugging.
130 size_t count();
131
132 private:
133 marl::mutex mutex;
134 std::queue<T> queue GUARDED_BY(mutex);
135 std::condition_variable added;
136 };
137
138 template<typename T>
Chan()139 Chan<T>::Chan()
140 {}
141
142 template<typename T>
take()143 T Chan<T>::take()
144 {
145 marl::lock lock(mutex);
146 // Wait for item to be added.
147 lock.wait(added, [this]() REQUIRES(mutex) { return queue.size() > 0; });
148 T out = queue.front();
149 queue.pop();
150 return out;
151 }
152
153 template<typename T>
tryTake()154 std::pair<T, bool> Chan<T>::tryTake()
155 {
156 marl::lock lock(mutex);
157 if(queue.size() == 0)
158 {
159 return std::make_pair(T{}, false);
160 }
161 T out = queue.front();
162 queue.pop();
163 return std::make_pair(out, true);
164 }
165
166 template<typename T>
put(const T & item)167 void Chan<T>::put(const T &item)
168 {
169 marl::lock lock(mutex);
170 queue.push(item);
171 added.notify_one();
172 }
173
174 template<typename T>
count()175 size_t Chan<T>::count()
176 {
177 marl::lock lock(mutex);
178 return queue.size();
179 }
180
181 } // namespace sw
182
183 #endif // sw_Synchronization_hpp
184