Skip to content

Commit 38b77dc

Browse files
committed
disabled the sleep mechanism in pool_work_until()
1 parent 59e25b4 commit 38b77dc

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/nanothread.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,11 @@ Task *task_submit_dep(Pool *pool, const Task *const *parent,
337337
}
338338

339339
static void pool_execute_task(Pool *pool, bool (*stopping_criterion)(void *),
340-
void *payload) {
340+
void *payload, bool may_sleep) {
341341
Task *task;
342342
uint32_t index;
343-
std::tie(task, index) = pool->queue.pop_or_sleep(stopping_criterion, payload);
343+
std::tie(task, index) =
344+
pool->queue.pop_or_sleep(stopping_criterion, payload, may_sleep);
344345

345346
if (task) {
346347
if (task->func) {
@@ -375,7 +376,7 @@ void pool_work_until(Pool *pool, bool (*stopping_criterion)(void *), void *paylo
375376
if (!pool)
376377
return;
377378
while (!stopping_criterion(payload))
378-
pool_execute_task(pool, stopping_criterion, payload);
379+
pool_execute_task(pool, stopping_criterion, payload, false);
379380
}
380381

381382
#if defined(__SSE2__)
@@ -415,7 +416,7 @@ void task_wait(Task *task) {
415416

416417
// Help executing work units in the meantime
417418
while (!stopping_criterion(task))
418-
pool_execute_task(pool, stopping_criterion, task);
419+
pool_execute_task(pool, stopping_criterion, task, true);
419420

420421
task->wait_count--;
421422

@@ -496,7 +497,8 @@ void Worker::run() {
496497
FTZGuard ftz_guard(ftz);
497498
while (!stop)
498499
pool_execute_task(
499-
pool, [](void *ptr) -> bool { return *((bool *) ptr); }, &stop);
500+
pool, [](void *ptr) -> bool { return *((bool *) ptr); }, &stop,
501+
true);
500502

501503
NT_TRACE("worker stopped");
502504

src/queue.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,11 @@ double time_milliseconds() {
415415
}
416416
#endif
417417

418-
std::pair<Task *, uint32_t> TaskQueue::pop_or_sleep(bool (*stopping_criterion)(void *), void *payload) {
419-
std::pair<Task *, uint32_t> result(nullptr, 0);
420-
uint32_t attempts = 0;
418+
std::pair<Task *, uint32_t>
419+
TaskQueue::pop_or_sleep(bool (*stopping_criterion)(void *), void *payload,
420+
bool may_sleep) {
421+
std::pair<Task *, uint32_t> result(nullptr, 0);
422+
uint32_t attempts = 0;
421423

422424
#if defined(NT_DEBUG)
423425
double start = time_milliseconds();
@@ -431,7 +433,7 @@ std::pair<Task *, uint32_t> TaskQueue::pop_or_sleep(bool (*stopping_criterion)(v
431433

432434
attempts++;
433435

434-
if (attempts >= NANOTHREAD_MAX_ATTEMPTS) {
436+
if (may_sleep && attempts >= NANOTHREAD_MAX_ATTEMPTS) {
435437
std::unique_lock<std::mutex> guard(sleep_mutex);
436438

437439
uint64_t value = ++sleep_state, phase = value & high_mask;

src/queue.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,15 @@ struct TaskQueue {
212212
* \breif Fetch a task from the queue, or sleep
213213
*
214214
* This function repeatedly tries to fetch work from the queue and sleeps
215-
* if no work is available for an extended amount of time (~50 ms).
215+
* if no work is available for an extended amount of time (~50 ms) and
216+
* the \c may_sleep parameter is set to \c true.
216217
*
217218
* The function stops trying to acquire work and returns <tt>(nullptr,
218219
* 0)</tt> when the supplied function <tt>stopping_criterion(payload)</tt>
219220
* evaluates to true.
220221
*/
221-
std::pair<Task *, uint32_t> pop_or_sleep(bool (*stopping_criterion)(void *), void *payload);
222+
std::pair<Task *, uint32_t> pop_or_sleep(bool (*stopping_criterion)(void *),
223+
void *payload, bool may_sleep);
222224

223225
/// Wake sleeping threads
224226
void wakeup();

0 commit comments

Comments
 (0)