| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_WINLOCK_H |
| 19 | #define HL_WINLOCK_H |
| 20 | |
| 21 | #if defined(_WIN32) |
| 22 | |
| 23 | #include <windows.h> |
| 24 | #include <winnt.h> |
| 25 | |
| 26 | /** |
| 27 | * @class WinLockType |
| 28 | * @brief Locking using Win32 mutexes. |
| 29 | * |
| 30 | * Note that this lock type detects whether we are running on a |
| 31 | * multiprocessor. If not, then we do not use atomic operations. |
| 32 | */ |
| 33 | |
| 34 | namespace HL { |
| 35 | |
| 36 | class WinLockType { |
| 37 | public: |
| 38 | |
| 39 | WinLockType (void) |
| 40 | : mutex (0) |
| 41 | {} |
| 42 | |
| 43 | ~WinLockType (void) |
| 44 | { |
| 45 | mutex = 0; |
| 46 | } |
| 47 | |
| 48 | inline void lock (void) { |
| 49 | int spinCount = 0; |
| 50 | while (InterlockedExchange ((long *) &mutex, 1) != 0) { |
| 51 | while (mutex != 0) { |
| 52 | YieldProcessor(); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | inline void unlock (void) { |
| 58 | mutex = 0; |
| 59 | // InterlockedExchange (&mutex, 0); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | unsigned int mutex; |
| 64 | bool onMultiprocessor (void) { |
| 65 | SYSTEM_INFO infoReturn[1]; |
| 66 | GetSystemInfo (infoReturn); |
| 67 | if (infoReturn->dwNumberOfProcessors == 1) { |
| 68 | return FALSE; |
| 69 | } else { |
| 70 | return TRUE; |
| 71 | } |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | #endif |
| 78 | |
| 79 | #endif |