-namespace { extern "C" {
-
- // start_routine() is the C function which is called when a new thread
- // is launched. It is a wrapper to member function pointed by start_fn
-
- long start_routine(Thread* th) { (th->*(th->start_fn))(); return 0; }
-
-} }
-
-
-// Thread c'tor starts a newly-created thread of execution that will call
-// the idle loop function pointed by start_fn going immediately to sleep.
-
-Thread::Thread(Fn fn) {
-
- is_searching = do_exit = false;
- maxPly = splitPointsCnt = 0;
- curSplitPoint = NULL;
- start_fn = fn;
- threadID = Threads.size();
- do_sleep = (threadID != 0); // Avoid a race with start_thinking()
-
- lock_init(sleepLock);
- cond_init(sleepCond);
-
- for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
- lock_init(splitPoints[j].lock);
-
- if (!thread_create(handle, start_routine, this))
- {
- std::cerr << "Failed to create thread number " << threadID << std::endl;
- ::exit(EXIT_FAILURE);
- }
-}
-
-
-// Thread d'tor will wait for thread termination before to return.
-
-Thread::~Thread() {
-
- assert(do_sleep);
-
- do_exit = true; // Search must be already finished
- wake_up();
-
- thread_join(handle); // Wait for thread termination
-
- lock_destroy(sleepLock);
- cond_destroy(sleepCond);
-
- for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
- lock_destroy(splitPoints[j].lock);
-}
-
-
-// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
-// then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up.