Index: src/platform-win32.cc |
diff --git a/src/platform-win32.cc b/src/platform-win32.cc |
index c0f049c52d9bacbee9f286c02b0c00f9df609b64..292c24a3dace86c6500acbd6634536d0dea7ddb6 100644 |
--- a/src/platform-win32.cc |
+++ b/src/platform-win32.cc |
@@ -1705,6 +1705,46 @@ void Thread::YieldCPU() { |
// ---------------------------------------------------------------------------- |
+// Win32 mutex support. |
+// |
+// On Win32 mutexes are implemented using CRITICAL_SECTION objects. These are |
+// faster than Win32 Mutex objects because they are implemented using user mode |
+// atomic instructions. Therefore we only do ring transitions if there is lock |
+// contention. |
+ |
+class Win32Mutex : public Mutex { |
+ public: |
+ Win32Mutex() { InitializeCriticalSection(&cs_); } |
+ |
+ virtual ~Win32Mutex() { DeleteCriticalSection(&cs_); } |
+ |
+ virtual int Lock() { |
+ EnterCriticalSection(&cs_); |
+ return 0; |
+ } |
+ |
+ virtual int Unlock() { |
+ LeaveCriticalSection(&cs_); |
+ return 0; |
+ } |
+ |
+ |
+ virtual bool TryLock() { |
+ // Returns non-zero if critical section is entered successfully entered. |
+ return TryEnterCriticalSection(&cs_); |
+ } |
+ |
+ private: |
+ CRITICAL_SECTION cs_; // Critical section used for mutex |
+}; |
+ |
+ |
+Mutex* OS::CreateMutex() { |
+ return new Win32Mutex(); |
+} |
+ |
+ |
+// ---------------------------------------------------------------------------- |
// Win32 semaphore support. |
// |
// On Win32 semaphores are implemented using Win32 Semaphore objects. The |
@@ -1947,7 +1987,7 @@ void OS::SetUp() { |
// call this setup code within the same millisecond. |
uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis()); |
srand(static_cast<unsigned int>(seed)); |
- limit_mutex = new Mutex; |
+ limit_mutex = CreateMutex(); |
} |