···9898 //! Number of tasks that is pending or being worked on in this group.
9999 size_t current_submitted_tasks_count;
100100101101- //! Number of threads that have been released or newly entered wait.
101101+ /*!
102102+ * Number of waiting threads that have been released by a worker,
103103+ * or a thread that has started waiting (see u_worker_group_wait_all).
104104+ */
102105 size_t released_count;
103106104107 struct
···202205 */
203206204207static bool
205205-locked_group_should_enter_wait_loop(struct pool *p, struct group *g)
208208+locked_group_has_tasks_waiting_or_inflight(const struct group *g)
206209{
207210 if (g->current_submitted_tasks_count == 0) {
208211 return false;
209212 }
210210-211211- // Enter the loop as a released thread.
212212- g->released_count++;
213213214214 return true;
215215}
···238238 */
239239240240 // Tasks available.
241241- if (g->current_submitted_tasks_count > 0) {
241241+ if (locked_group_has_tasks_waiting_or_inflight(g)) {
242242243243 // We have been released or newly entered the loop.
244244 if (g->released_count > 0) {
···266266locked_group_wake_waiter_if_allowed(struct pool *p, struct group *g)
267267{
268268 // Are there still outstanding tasks?
269269- if (g->current_submitted_tasks_count > 0) {
269269+ if (locked_group_has_tasks_waiting_or_inflight(g)) {
270270 return;
271271 }
272272···534534 os_mutex_lock(&p->mutex);
535535536536 // Can we early out?
537537- if (!locked_group_should_enter_wait_loop(p, g)) {
537537+ if (!locked_group_has_tasks_waiting_or_inflight(g)) {
538538 os_mutex_unlock(&p->mutex);
539539 return;
540540 }
541541+542542+ /*
543543+ * The released_count is tied to the decrement of worker_limit, that is
544544+ * when a waiting thread is woken up the worker_limit is decreased, and
545545+ * released_count is increased. The waiting thread will then double
546546+ * check that it can be released or not, if it can not be released it
547547+ * will once again donate this thread and increase the worker_limit.
548548+ *
549549+ * If it can be released it will decrement released_count and exit the
550550+ * loop below.
551551+ *
552552+ * So if we increment it here, the loop will increase worker_limit
553553+ * which is what we want.
554554+ */
555555+ g->released_count++;
541556542557 // Wait here until all work been started and completed.
543558 while (locked_group_should_wait(p, g)) {