Use one sleep lock per thread insted of a single one shared.
Also renamed WaitLock in SleepLock.
No functional change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
int ActiveThreads;
volatile bool AllThreadsShouldExit;
Thread threads[MAX_THREADS];
int ActiveThreads;
volatile bool AllThreadsShouldExit;
Thread threads[MAX_THREADS];
- Lock MPLock, WaitLock;
- WaitCondition WaitCond[MAX_THREADS];
+ Lock MPLock, SleepLock[MAX_THREADS];
+ WaitCondition SleepCond[MAX_THREADS];
threads[threadID].state = THREAD_AVAILABLE;
// Grab the lock to avoid races with wake_sleeping_thread()
threads[threadID].state = THREAD_AVAILABLE;
// Grab the lock to avoid races with wake_sleeping_thread()
+ lock_grab(&SleepLock[threadID]);
// If we are master and all slaves have finished do not go to sleep
for (i = 0; sp && i < ActiveThreads && !sp->slaves[i]; i++) {}
// If we are master and all slaves have finished do not go to sleep
for (i = 0; sp && i < ActiveThreads && !sp->slaves[i]; i++) {}
if (allFinished || AllThreadsShouldExit)
{
if (allFinished || AllThreadsShouldExit)
{
- lock_release(&WaitLock);
+ lock_release(&SleepLock[threadID]);
break;
}
// Do sleep here after retesting sleep conditions
if (threadID >= ActiveThreads || threads[threadID].state == THREAD_AVAILABLE)
break;
}
// Do sleep here after retesting sleep conditions
if (threadID >= ActiveThreads || threads[threadID].state == THREAD_AVAILABLE)
- cond_wait(&WaitCond[threadID], &WaitLock);
+ cond_wait(&SleepCond[threadID], &SleepLock[threadID]);
- lock_release(&WaitLock);
+ lock_release(&SleepLock[threadID]);
}
// If this thread has been assigned work, launch a search
}
// If this thread has been assigned work, launch a search
// Initialize global locks
lock_init(&MPLock);
// Initialize global locks
lock_init(&MPLock);
for (i = 0; i < MAX_THREADS; i++)
for (i = 0; i < MAX_THREADS; i++)
- cond_init(&WaitCond[i]);
+ {
+ lock_init(&SleepLock[i]);
+ cond_init(&SleepCond[i]);
+ }
// Initialize splitPoints[] locks
for (i = 0; i < MAX_THREADS; i++)
// Initialize splitPoints[] locks
for (i = 0; i < MAX_THREADS; i++)
for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++)
lock_destroy(&(threads[i].splitPoints[j].lock));
for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++)
lock_destroy(&(threads[i].splitPoints[j].lock));
- lock_destroy(&WaitLock);
lock_destroy(&MPLock);
// Now we can safely destroy the wait conditions
for (int i = 0; i < MAX_THREADS; i++)
lock_destroy(&MPLock);
// Now we can safely destroy the wait conditions
for (int i = 0; i < MAX_THREADS; i++)
- cond_destroy(&WaitCond[i]);
+ {
+ lock_destroy(&SleepLock[i]);
+ cond_destroy(&SleepCond[i]);
+ }
- // wake_sleeping_thread() wakes up all sleeping threads when it is time
- // to start a new search from the root.
+ // wake_sleeping_thread() wakes up the thread with the given threadID
+ // when it is time to start a new search.
void ThreadsManager::wake_sleeping_thread(int threadID) {
void ThreadsManager::wake_sleeping_thread(int threadID) {
- lock_grab(&WaitLock);
- cond_signal(&WaitCond[threadID]);
- lock_release(&WaitLock);
+ lock_grab(&SleepLock[threadID]);
+ cond_signal(&SleepCond[threadID]);
+ lock_release(&SleepLock[threadID]);