]> git.sesse.net Git - stockfish/commitdiff
Add thread_win32.h header
authorMarco Costalba <mcostalba@gmail.com>
Tue, 10 Mar 2015 11:42:40 +0000 (12:42 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Tue, 10 Mar 2015 11:42:40 +0000 (12:42 +0100)
Workaround slow std::thread implementation in mingw
and gcc for Windows with our own old low level thread
functions.

No functional change.

src/misc.cpp
src/search.cpp
src/thread.cpp
src/thread.h
src/thread_win32.h [new file with mode: 0644]

index f09694cc043607a6cd17d0409baf1e96c9537229..7df4287a3203df480b706672be1552493a35b627 100644 (file)
@@ -146,7 +146,7 @@ void dbg_print() {
 
 std::ostream& operator<<(std::ostream& os, SyncCout sc) {
 
-  static std::mutex m;
+  static Mutex m;
 
   if (sc == IO_LOCK)
       m.lock();
index 681acb8141a0e1b5d7c8def9569ce28e8bb15ffc..3b35e3a061842f667b386d853e7e659529cf3238 100644 (file)
@@ -1636,7 +1636,7 @@ void Thread::idle_loop() {
       }
 
       // Avoid races with notify_one() fired from last slave of the split point
-      std::unique_lock<std::mutex> lk(mutex);
+      std::unique_lock<Mutex> lk(mutex);
 
       // If we are master and all slaves have finished then exit idle_loop
       if (this_sp && this_sp->slavesMask.none())
index ff52576b062785c49f8eae6bfa360b0380dbae71..6ddbf3f2e85f82ee085cf82db5f46cf76657b78d 100644 (file)
@@ -61,7 +61,7 @@ namespace {
 
 void ThreadBase::notify_one() {
 
-  std::unique_lock<std::mutex>(this->mutex);
+  std::unique_lock<Mutex>(this->mutex);
   sleepCondition.notify_one();
 }
 
@@ -70,7 +70,7 @@ void ThreadBase::notify_one() {
 
 void ThreadBase::wait_for(volatile const bool& condition) {
 
-  std::unique_lock<std::mutex> lk(mutex);
+  std::unique_lock<Mutex> lk(mutex);
   sleepCondition.wait(lk, [&]{ return condition; });
 }
 
@@ -224,7 +224,7 @@ void TimerThread::idle_loop() {
 
   while (!exit)
   {
-      std::unique_lock<std::mutex> lk(mutex);
+      std::unique_lock<Mutex> lk(mutex);
 
       if (!exit)
           sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX));
@@ -244,7 +244,7 @@ void MainThread::idle_loop() {
 
   while (!exit)
   {
-      std::unique_lock<std::mutex> lk(mutex);
+      std::unique_lock<Mutex> lk(mutex);
 
       thinking = false;
 
@@ -340,7 +340,7 @@ Thread* ThreadPool::available_slave(const SplitPoint* sp) const {
 
 void ThreadPool::wait_for_think_finished() {
 
-  std::unique_lock<std::mutex> lk(main()->mutex);
+  std::unique_lock<Mutex> lk(main()->mutex);
   sleepCondition.wait(lk, [&]{ return !main()->thinking; });
 }
 
index b6809f4e37221dcca2329b62980f07a7e42c5238..b4aad5cb7f42321c8954ee641452c5908f9d3cb5 100644 (file)
@@ -32,6 +32,7 @@
 #include "pawns.h"
 #include "position.h"
 #include "search.h"
+#include "thread_win32.h"
 
 struct Thread;
 
@@ -98,8 +99,8 @@ struct ThreadBase {
   void wait_for(volatile const bool& b);
 
   std::thread nativeThread;
-  std::mutex mutex;
-  std::condition_variable sleepCondition;
+  Mutex mutex;
+  ConditionVariable sleepCondition;
   volatile bool exit = false;
 };
 
@@ -167,7 +168,7 @@ struct ThreadPool : public std::vector<Thread*> {
 
   Depth minimumSplitDepth;
   Spinlock spinlock;
-  std::condition_variable sleepCondition;
+  ConditionVariable sleepCondition;
   TimerThread* timer;
 };
 
diff --git a/src/thread_win32.h b/src/thread_win32.h
new file mode 100644 (file)
index 0000000..f0c33fb
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+  Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
+  Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
+
+  Stockfish is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  Stockfish is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef THREAD_WIN32_H_INCLUDED
+#define THREAD_WIN32_H_INCLUDED
+
+/// STL thread library uded by gcc and mingw compilers is implemented above
+/// POSIX pthread. Unfortunatly this yields to a much slower speed (about 30%)
+/// than the native Win32 calls. So use our own implementation that relies on
+/// the Windows specific low level calls.
+
+#if defined(_WIN32) && !defined(_MSC_VER)
+
+#ifndef NOMINMAX
+#  define NOMINMAX // disable macros min() and max()
+#endif
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#undef WIN32_LEAN_AND_MEAN
+#undef NOMINMAX
+
+// We use critical sections on Windows to support Windows XP and older versions.
+// Unfortunately, cond_wait() is racy between lock_release() and WaitForSingleObject()
+// but apart from this they have the same speed performance of SRW locks.
+typedef CRITICAL_SECTION Lock;
+typedef HANDLE WaitCondition;
+typedef HANDLE NativeHandle;
+
+// On Windows 95 and 98 parameter lpThreadId may not be null
+inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
+
+#  define lock_init(x) InitializeCriticalSection(&(x))
+#  define lock_grab(x) EnterCriticalSection(&(x))
+#  define lock_release(x) LeaveCriticalSection(&(x))
+#  define lock_destroy(x) DeleteCriticalSection(&(x))
+#  define cond_init(x) { x = CreateEvent(0, FALSE, FALSE, 0); }
+#  define cond_destroy(x) CloseHandle(x)
+#  define cond_signal(x) SetEvent(x)
+#  define cond_wait(x,y) { lock_release(y); WaitForSingleObject(x, INFINITE); lock_grab(y); }
+#  define cond_timedwait(x,y,z) { lock_release(y); WaitForSingleObject(x,z); lock_grab(y); }
+
+/// Mutex and ConditionVariable struct are wrappers of the low level locking
+/// machinery and are modeled after the corresponding C++11 classes.
+
+struct Mutex {
+  Mutex() { lock_init(l); }
+ ~Mutex() { lock_destroy(l); }
+
+  void lock() { lock_grab(l); }
+  void unlock() { lock_release(l); }
+
+private:
+  friend struct ConditionVariable;
+
+  Lock l;
+};
+
+struct ConditionVariable {
+  ConditionVariable() { cond_init(c); }
+ ~ConditionVariable() { cond_destroy(c); }
+
+  void notify_one() { cond_signal(c); }
+  void wait(std::unique_lock<Mutex>& lk) { cond_wait(c, lk.mutex()->l); }
+
+  template<class Predicate>
+  void wait(std::unique_lock<Mutex>& lk, Predicate p) { while (!p()) this->wait(lk); }
+
+  void wait_for(std::unique_lock<Mutex>& lk, const std::chrono::milliseconds& ms) {
+    cond_timedwait(c, lk.mutex()->l, ms.count());
+  }
+
+private:
+  WaitCondition c;
+};
+
+#else // Default case: use STL classes
+
+typedef std::mutex Mutex;
+typedef std::condition_variable ConditionVariable;
+
+#endif
+
+#endif // #ifndef THREAD_WIN32_H_INCLUDED