friend void poll();
int ActiveThreads;
- volatile bool AllThreadsShouldExit, AllThreadsShouldSleep;
+ volatile bool AllThreadsShouldExit;
Thread threads[MAX_THREADS];
-
Lock MPLock, WaitLock;
-
-#if !defined(_MSC_VER)
- pthread_cond_t WaitCond[MAX_THREADS];
-#else
- HANDLE SitIdleEvent[MAX_THREADS];
-#endif
-
+ WaitCondition WaitCond[MAX_THREADS];
};
// If we are not thinking, wait for a condition to be signaled
// instead of wasting CPU time polling for work.
- while (AllThreadsShouldSleep || threadID >= ActiveThreads)
+ while (threadID >= ActiveThreads)
{
assert(!sp);
assert(threadID != 0);
#if !defined(_MSC_VER)
lock_grab(&WaitLock);
- if (AllThreadsShouldSleep || threadID >= ActiveThreads)
+ if (threadID >= ActiveThreads)
pthread_cond_wait(&WaitCond[threadID], &WaitLock);
lock_release(&WaitLock);
#else
- WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
+ WaitForSingleObject(WaitCond[threadID], INFINITE);
#endif
}
// If this thread has been assigned work, launch a search
if (threads[threadID].state == THREAD_WORKISWAITING)
{
- assert(!AllThreadsShouldExit && !AllThreadsShouldSleep);
+ assert(!AllThreadsShouldExit);
threads[threadID].state = THREAD_SEARCHING;
volatile int i;
bool ok;
-#if !defined(_MSC_VER)
- pthread_t pthread[1];
-#endif
-
// Initialize global locks
lock_init(&MPLock);
lock_init(&WaitLock);
#if !defined(_MSC_VER)
pthread_cond_init(&WaitCond[i], NULL);
#else
- SitIdleEvent[i] = CreateEvent(0, FALSE, FALSE, 0);
+ WaitCond[i] = CreateEvent(0, FALSE, FALSE, 0);
#endif
// Initialize splitPoints[] locks
AllThreadsShouldExit = false;
// Threads will be put to sleep as soon as created
- AllThreadsShouldSleep = true;
+ ActiveThreads = 1;
// All threads except the main thread should be initialized to THREAD_AVAILABLE
- ActiveThreads = 1;
threads[0].state = THREAD_SEARCHING;
for (i = 1; i < MAX_THREADS; i++)
threads[i].state = THREAD_AVAILABLE;
{
#if !defined(_MSC_VER)
+ pthread_t pthread[1];
ok = (pthread_create(pthread, NULL, init_thread, (void*)(&i)) == 0);
#else
ok = (CreateThread(NULL, 0, init_thread, (LPVOID)(&i), 0, NULL) != NULL);
lock_destroy(&WaitLock);
lock_destroy(&MPLock);
+
+ // Now we can safely destroy the wait conditions
+ for (int i = 0; i < MAX_THREADS; i++)
+ cond_destroy(&WaitCond[i]);
}
assert(threadID > 0);
assert(threads[threadID].state == THREAD_SLEEPING);
- AllThreadsShouldSleep = false; // Avoid the woken up thread comes back to sleep
-
#if !defined(_MSC_VER)
pthread_mutex_lock(&WaitLock);
pthread_cond_signal(&WaitCond[threadID]);
pthread_mutex_unlock(&WaitLock);
#else
- SetEvent(SitIdleEvent[threadID]);
+ SetEvent(WaitCond[threadID]);
#endif
}
void ThreadsManager::put_threads_to_sleep() {
- assert(!AllThreadsShouldSleep || ActiveThreads == 1);
-
// This makes the threads to go to sleep
- AllThreadsShouldSleep = true;
+ ActiveThreads = 1;
}
/// The RootMoveList class