]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Revert C++11 merge
[stockfish] / src / thread.cpp
index a466df87cbb9a2dfa5fa2b9280a858414de71706..786258a2ed564961fdb75f67024944aaf8c25d49 100644 (file)
@@ -33,13 +33,19 @@ extern void check_time();
 
 namespace {
 
+ // start_routine() is the C function which is called when a new thread
+ // is launched. It is a wrapper to the virtual function idle_loop().
+
+ extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } }
+
+
  // Helpers to launch a thread after creation and joining before delete. Must be
  // outside Thread c'tor and d'tor because the object must be fully initialized
  // when start_routine (and hence virtual idle_loop) is called and when joining.
 
  template<typename T> T* new_thread() {
    T* th = new T();
-   th->nativeThread = std::thread(&ThreadBase::idle_loop, th); // Will go to sleep
+   thread_create(th->handle, start_routine, th); // Will go to sleep
    return th;
  }
 
@@ -50,7 +56,7 @@ namespace {
    th->mutex.unlock();
 
    th->notify_one();
-   th->nativeThread.join(); // Wait for thread termination
+   thread_join(th->handle); // Wait for thread termination
    delete th;
  }
 
@@ -61,8 +67,9 @@ namespace {
 
 void ThreadBase::notify_one() {
 
-  std::unique_lock<std::mutex>(this->mutex);
+  mutex.lock();
   sleepCondition.notify_one();
+  mutex.unlock();
 }
 
 
@@ -70,8 +77,9 @@ void ThreadBase::notify_one() {
 
 void ThreadBase::wait_for(volatile const bool& condition) {
 
-  std::unique_lock<std::mutex> lk(mutex);
-  sleepCondition.wait(lk, [&]{ return condition; });
+  mutex.lock();
+  while (!condition) sleepCondition.wait(mutex);
+  mutex.unlock();
 }
 
 
@@ -83,8 +91,8 @@ Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in
   searching = false;
   maxPly = 0;
   splitPointsSize = 0;
-  activeSplitPoint = nullptr;
-  activePosition = nullptr;
+  activeSplitPoint = NULL;
+  activePosition = NULL;
   idx = Threads.size(); // Starts from 0
 }
 
@@ -171,12 +179,12 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
   sp.allSlavesSearching = true; // Must be set under lock protection
   ++splitPointsSize;
   activeSplitPoint = &sp;
-  activePosition = nullptr;
+  activePosition = NULL;
 
   Thread* slave;
 
   while (    sp.slavesMask.count() < MAX_SLAVES_PER_SPLITPOINT
-         && (slave = Threads.available_slave(this)) != nullptr)
+         && (slave = Threads.available_slave(this)) != NULL)
   {
       sp.slavesMask.set(slave->idx);
       slave->activeSplitPoint = &sp;
@@ -225,12 +233,12 @@ void TimerThread::idle_loop() {
 
   while (!exit)
   {
-      std::unique_lock<std::mutex> lk(mutex);
+      mutex.lock();
 
       if (!exit)
-          sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX));
+          sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX);
 
-      lk.unlock();
+      mutex.unlock();
 
       if (run)
           check_time();
@@ -245,17 +253,17 @@ void MainThread::idle_loop() {
 
   while (!exit)
   {
-      std::unique_lock<std::mutex> lk(mutex);
+      mutex.lock();
 
       thinking = false;
 
       while (!thinking && !exit)
       {
           Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed
-          sleepCondition.wait(lk);
+          sleepCondition.wait(mutex);
       }
 
-      lk.unlock();
+      mutex.unlock();
 
       if (!exit)
       {
@@ -291,8 +299,8 @@ void ThreadPool::exit() {
 
   delete_thread(timer); // As first because check_time() accesses threads data
 
-  for (Thread* th : *this)
-      delete_thread(th);
+  for (iterator it = begin(); it != end(); ++it)
+      delete_thread(*it);
 }
 
 
@@ -329,11 +337,11 @@ void ThreadPool::read_uci_options() {
 
 Thread* ThreadPool::available_slave(const Thread* master) const {
 
-  for (Thread* th : *this)
-      if (th->available_to(master))
-          return th;
+  for (const_iterator it = begin(); it != end(); ++it)
+      if ((*it)->available_to(master))
+          return *it;
 
-  return nullptr;
+  return NULL;
 }
 
 
@@ -341,8 +349,10 @@ Thread* ThreadPool::available_slave(const Thread* master) const {
 
 void ThreadPool::wait_for_think_finished() {
 
-  std::unique_lock<std::mutex> lk(main()->mutex);
-  sleepCondition.wait(lk, [&]{ return !main()->thinking; });
+  MainThread* th = main();
+  th->mutex.lock();
+  while (th->thinking) sleepCondition.wait(th->mutex);
+  th->mutex.unlock();
 }
 
 
@@ -363,14 +373,14 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
   Limits = limits;
   if (states.get()) // If we don't set a new position, preserve current state
   {
-      SetupStates = std::move(states); // Ownership transfer here
+      SetupStates = states; // Ownership transfer here
       assert(!states.get());
   }
 
-  for (const auto& m : MoveList<LEGAL>(pos))
+  for (MoveList<LEGAL> it(pos); *it; ++it)
       if (   limits.searchmoves.empty()
-          || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
-          RootMoves.push_back(RootMove(m));
+          || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it))
+          RootMoves.push_back(RootMove(*it));
 
   main()->thinking = true;
   main()->notify_one(); // Starts main thread