@@ -49,9 +49,10 @@ class scoped_semaphore {
49
49
string by_;
50
50
};
51
51
// The semaphore class. Simulate a fixed amount of resources (threads).
52
- class with_semaphore {
52
+ class sema {
53
53
protected:
54
- with_semaphore (counting_semaphore<>& sem) : sem_(sem) {}
54
+ // Using a reference s.t. the semaphore don't get copied.
55
+ sema (counting_semaphore<>& sem) : sem_(sem) {}
55
56
scoped_semaphore acquire_semaphore (string by) {
56
57
// Using copy elision, to avoid acquiring and releasing and acquiring.
57
58
return scoped_semaphore (sem_, by);
@@ -167,20 +168,19 @@ class cache : public compute {
167
168
// For example, when budget = 1, a depending on b,
168
169
// a would require a thread to run, and then b would require a thread to run,
169
170
// exceeding the budget (a runs first before b).
170
- class task_literal : public literal , with_semaphore {
171
+ class task_literal : public literal , sema {
171
172
public:
172
- task_literal (int i, counting_semaphore<>& sem)
173
- : literal(i), with_semaphore(sem) {}
173
+ task_literal (int i, counting_semaphore<>& sem) : literal(i), sema(sem) {}
174
174
175
175
int get () override {
176
176
auto sem{acquire_semaphore (" task_lit_" + str ())};
177
177
return literal::get ();
178
178
}
179
179
};
180
- class task_summation : public summation , with_semaphore {
180
+ class task_summation : public summation , sema {
181
181
public:
182
182
task_summation (vector<shared_ptr<compute>> op, counting_semaphore<>& sem)
183
- : summation(op), with_semaphore (sem) {}
183
+ : summation(op), sema (sem) {}
184
184
185
185
int get () override {
186
186
auto sem{acquire_semaphore (" task_sum_" + str ())};
@@ -191,20 +191,19 @@ class task_summation : public summation, with_semaphore {
191
191
// Lazy classes doesn't cause deadlocks,
192
192
// because they are reduced from the leaves of the expression tree,
193
193
// which can be linearly ordered (no deadlock so long as semaphore > 1).
194
- class lazy_literal : public literal , with_semaphore {
194
+ class lazy_literal : public literal , sema {
195
195
public:
196
- lazy_literal (int i, counting_semaphore<>& sem)
197
- : literal(i), with_semaphore(sem) {}
196
+ lazy_literal (int i, counting_semaphore<>& sem) : literal(i), sema(sem) {}
198
197
int get () override {
199
198
// As literal has no children, this can be the same as `literal_task`.
200
199
auto sem{acquire_semaphore (" lazy_lit_" + str ())};
201
200
return literal::get ();
202
201
}
203
202
};
204
- class lazy_summation : public summation , with_semaphore {
203
+ class lazy_summation : public summation , sema {
205
204
public:
206
205
lazy_summation (vector<shared_ptr<compute>> op, counting_semaphore<>& sem)
207
- : summation(op), with_semaphore (sem) {}
206
+ : summation(op), sema (sem) {}
208
207
int get () override {
209
208
vector<int > values;
210
209
0 commit comments