1
0
mirror of https://github.com/Zygo/bees.git synced 2025-06-15 17:26:15 +02:00

Task: convert print_fn to a string

Since we are now unconditionally rendering the print_fn as a static
string, there is no need for it to be a function.  We also need it to
be brief and mostly constant.

Use a string instead.  Put the string before the function in the Task
constructor arguments so that the title string appears as a heading in
code, since we are making a breaking API change already.

Drop TASK_MACRO as it is broken by this change, but there is no similar
usage of Task anywhere to make it worth fixing.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell
2018-01-25 21:12:31 -05:00
parent 0710208354
commit f64fc78e36
5 changed files with 75 additions and 86 deletions

View File

@ -28,12 +28,17 @@ test_tasks(size_t count)
// Run several tasks in parallel
for (size_t c = 0; c < count; ++c) {
Task t([c, &task_done, &mtx, &cv]() {
unique_lock<mutex> lock(mtx);
// cerr << "Task #" << c << endl;
task_done.at(c) = true;
cv.notify_one();
}, [=](ostream& os) -> ostream& { return os << "task #" << c; });
ostringstream oss;
oss << "task #" << c;
Task t(
oss.str(),
[c, &task_done, &mtx, &cv]() {
unique_lock<mutex> lock(mtx);
// cerr << "Task #" << c << endl;
task_done.at(c) = true;
cv.notify_one();
}
);
t.run();
}
@ -58,20 +63,6 @@ test_tasks(size_t count)
}
}
#define TASK_MACRO(printer, body) Task( \
[=]() { body; }, \
[=](ostream &__os) -> ostream& { return __os << printer; } \
)
void
test_macro()
{
TASK_MACRO(
"A Task",
cerr << "Hello, World!" << endl;
).run();
}
void
test_finish()
{
@ -104,12 +95,17 @@ test_barrier(size_t count)
// Run several tasks in parallel
for (size_t c = 0; c < count; ++c) {
auto bl = b->lock();
Task t([c, &task_done, &mtx, &cv, bl]() mutable {
// cerr << "Task #" << c << endl;
unique_lock<mutex> lock(mtx);
task_done.at(c) = true;
bl.release();
}, [=](ostream& os) -> ostream& { return os << "task #" << c; });
ostringstream oss;
oss << "task #" << c;
Task t(
oss.str(),
[c, &task_done, &mtx, &cv, bl]() mutable {
// cerr << "Task #" << c << endl;
unique_lock<mutex> lock(mtx);
task_done.at(c) = true;
bl.release();
}
);
t.run();
}
@ -121,14 +117,12 @@ test_barrier(size_t count)
bool done_flag = false;
Task completed(
"Waiting for Barrier",
[&mtx, &cv, &done_flag]() {
unique_lock<mutex> lock(mtx);
// cerr << "Running cv notify" << endl;
done_flag = true;
cv.notify_all();
},
[](ostream &os) -> ostream& {
return os << "Waiting for Barrier";
}
);
b->insert_task(completed);
@ -168,33 +162,36 @@ test_exclusion(size_t count)
// Run several tasks in parallel
for (size_t c = 0; c < count; ++c) {
auto bl = b->lock();
Task t([c, &only_one, &mtx, &excl, bl]() mutable {
// cerr << "Task #" << c << endl;
auto lock = excl.try_lock();
if (!lock) {
excl.insert_task(Task::current_task());
return;
ostringstream oss;
oss << "task #" << c;
Task t(
oss.str(),
[c, &only_one, &mtx, &excl, bl]() mutable {
// cerr << "Task #" << c << endl;
auto lock = excl.try_lock();
if (!lock) {
excl.insert_task(Task::current_task());
return;
}
bool locked = only_one.try_lock();
assert(locked);
nanosleep(0.0001);
only_one.unlock();
bl.release();
}
bool locked = only_one.try_lock();
assert(locked);
nanosleep(0.0001);
only_one.unlock();
bl.release();
}, [=](ostream& os) -> ostream& { return os << "task #" << c; });
);
t.run();
}
bool done_flag = false;
Task completed(
"Waiting for Barrier",
[&mtx, &cv, &done_flag]() {
unique_lock<mutex> lock(mtx);
// cerr << "Running cv notify" << endl;
done_flag = true;
cv.notify_all();
},
[](ostream &os) -> ostream& {
return os << "Waiting for Barrier";
}
);
b->insert_task(completed);
@ -217,7 +214,6 @@ main(int, char**)
alarm(9);
RUN_A_TEST(test_tasks(256));
RUN_A_TEST(test_macro());
RUN_A_TEST(test_finish());
RUN_A_TEST(test_unfinish());
RUN_A_TEST(test_barrier(256));