Skip to content

Commit 10096a0

Browse files
committed
👀 Made semaphore's class name shorter.
Looks easier on the eyes.
1 parent a72e6d0 commit 10096a0

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

‎cpp/deadlock.cpp‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ class scoped_semaphore {
4949
string by_;
5050
};
5151
// The semaphore class. Simulate a fixed amount of resources (threads).
52-
class with_semaphore {
52+
class sema {
5353
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) {}
5556
scoped_semaphore acquire_semaphore(string by) {
5657
// Using copy elision, to avoid acquiring and releasing and acquiring.
5758
return scoped_semaphore(sem_, by);
@@ -167,20 +168,19 @@ class cache : public compute {
167168
// For example, when budget = 1, a depending on b,
168169
// a would require a thread to run, and then b would require a thread to run,
169170
// exceeding the budget (a runs first before b).
170-
class task_literal : public literal, with_semaphore {
171+
class task_literal : public literal, sema {
171172
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) {}
174174

175175
int get() override {
176176
auto sem{acquire_semaphore("task_lit_" + str())};
177177
return literal::get();
178178
}
179179
};
180-
class task_summation : public summation, with_semaphore {
180+
class task_summation : public summation, sema {
181181
public:
182182
task_summation(vector<shared_ptr<compute>> op, counting_semaphore<>& sem)
183-
: summation(op), with_semaphore(sem) {}
183+
: summation(op), sema(sem) {}
184184

185185
int get() override {
186186
auto sem{acquire_semaphore("task_sum_" + str())};
@@ -191,20 +191,19 @@ class task_summation : public summation, with_semaphore {
191191
// Lazy classes doesn't cause deadlocks,
192192
// because they are reduced from the leaves of the expression tree,
193193
// 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 {
195195
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) {}
198197
int get() override {
199198
// As literal has no children, this can be the same as `literal_task`.
200199
auto sem{acquire_semaphore("lazy_lit_" + str())};
201200
return literal::get();
202201
}
203202
};
204-
class lazy_summation : public summation, with_semaphore {
203+
class lazy_summation : public summation, sema {
205204
public:
206205
lazy_summation(vector<shared_ptr<compute>> op, counting_semaphore<>& sem)
207-
: summation(op), with_semaphore(sem) {}
206+
: summation(op), sema(sem) {}
208207
int get() override {
209208
vector<int> values;
210209

0 commit comments

Comments
 (0)