Subversion Repositories Games.Chess Giants

Rev

Rev 169 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
  3.   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
  4.   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
  5.   Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
  6.  
  7.   Stockfish is free software: you can redistribute it and/or modify
  8.   it under the terms of the GNU General Public License as published by
  9.   the Free Software Foundation, either version 3 of the License, or
  10.   (at your option) any later version.
  11.  
  12.   Stockfish is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License
  18.   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. #ifndef THREAD_WIN32_H_INCLUDED
  22. #define THREAD_WIN32_H_INCLUDED
  23.  
  24. /// STL thread library used by mingw and gcc when cross compiling for Windows
  25. /// relies on libwinpthread. Currently libwinpthread implements mutexes directly
  26. /// on top of Windows semaphores. Semaphores, being kernel objects, require kernel
  27. /// mode transition in order to lock or unlock, which is very slow compared to
  28. /// interlocked operations (about 30% slower on bench test). To work around this
  29. /// issue, we define our wrappers to the low level Win32 calls. We use critical
  30. /// sections to support Windows XP and older versions. Unfortunately, cond_wait()
  31. /// is racy between unlock() and WaitForSingleObject() but they have the same
  32. /// speed performance as the SRW locks.
  33.  
  34. #include <condition_variable>
  35. #include <mutex>
  36.  
  37. #if defined(_WIN32) && !defined(_MSC_VER)
  38.  
  39. #ifndef NOMINMAX
  40. #  define NOMINMAX // Disable macros min() and max()
  41. #endif
  42.  
  43. #define WIN32_LEAN_AND_MEAN
  44. #include <windows.h>
  45. #undef WIN32_LEAN_AND_MEAN
  46. #undef NOMINMAX
  47.  
  48. /// Mutex and ConditionVariable struct are wrappers of the low level locking
  49. /// machinery and are modeled after the corresponding C++11 classes.
  50.  
  51. struct Mutex {
  52.   Mutex() { InitializeCriticalSection(&cs); }
  53.  ~Mutex() { DeleteCriticalSection(&cs); }
  54.   void lock() { EnterCriticalSection(&cs); }
  55.   void unlock() { LeaveCriticalSection(&cs); }
  56.  
  57. private:
  58.   CRITICAL_SECTION cs;
  59. };
  60.  
  61. typedef std::condition_variable_any ConditionVariable;
  62.  
  63. #else // Default case: use STL classes
  64.  
  65. typedef std::mutex Mutex;
  66. typedef std::condition_variable ConditionVariable;
  67.  
  68. #endif
  69.  
  70. #endif // #ifndef THREAD_WIN32_H_INCLUDED
  71.