@@ -1422,46 +1422,13 @@ void Thread::idle_loop() {
14221422
14231423 assert (!this_sp || (this_sp->masterThread == this && searching));
14241424
1425- while (true )
1425+ while (!exit )
14261426 {
1427- // If we are not searching, wait for a condition to be signaled instead of
1428- // wasting CPU time polling for work.
1429- while (!searching || exit)
1430- {
1431- if (exit)
1432- {
1433- assert (!this_sp);
1434- return ;
1435- }
1436-
1437- // Grab the lock to avoid races with Thread::notify_one()
1438- mutex.lock ();
1439-
1440- // If we are master and all slaves have finished then exit idle_loop
1441- if (this_sp && this_sp->slavesMask .none ())
1442- {
1443- mutex.unlock ();
1444- break ;
1445- }
1446-
1447- // Do sleep after retesting sleep conditions under lock protection. In
1448- // particular we need to avoid a deadlock in case a master thread has,
1449- // in the meanwhile, allocated us and sent the notify_one() call before
1450- // we had the chance to grab the lock.
1451- if (!searching && !exit)
1452- sleepCondition.wait (mutex);
1453-
1454- mutex.unlock ();
1455- }
1456-
14571427 // If this thread has been assigned work, launch a search
14581428 if (searching)
14591429 {
1460- assert (!exit);
1461-
14621430 Threads.mutex .lock ();
14631431
1464- assert (searching);
14651432 assert (activeSplitPoint);
14661433 SplitPoint* sp = activeSplitPoint;
14671434
@@ -1545,16 +1512,23 @@ void Thread::idle_loop() {
15451512 }
15461513 }
15471514
1548- // If this thread is the master of a split point and all slaves have finished
1549- // their work at this split point, return from the idle loop.
1515+ // Grab the lock to avoid races with Thread::notify_one()
1516+ mutex.lock ();
1517+
1518+ // If we are master and all slaves have finished then exit idle_loop
15501519 if (this_sp && this_sp->slavesMask .none ())
15511520 {
1552- this_sp->mutex .lock ();
1553- bool finished = this_sp->slavesMask .none (); // Retest under lock protection
1554- this_sp->mutex .unlock ();
1555- if (finished)
1556- return ;
1521+ assert (!searching);
1522+ mutex.unlock ();
1523+ break ;
15571524 }
1525+
1526+ // If we are not searching, wait for a condition to be signaled instead of
1527+ // wasting CPU time polling for work.
1528+ if (!searching && !exit)
1529+ sleepCondition.wait (mutex);
1530+
1531+ mutex.unlock ();
15581532 }
15591533}
15601534
0 commit comments