Line | |
---|
1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _WINLOCK_H_ |
---|
4 | #define _WINLOCK_H_ |
---|
5 | |
---|
6 | #if defined(_WIN32) |
---|
7 | |
---|
8 | #include <windows.h> |
---|
9 | |
---|
10 | /** |
---|
11 | * @class WinLockType |
---|
12 | * @brief Locking using Win32 mutexes. |
---|
13 | * |
---|
14 | * Note that this lock type detects whether we are running on a |
---|
15 | * multiprocessor. If not, then we do not use atomic operations. |
---|
16 | */ |
---|
17 | |
---|
18 | #if 1 |
---|
19 | |
---|
20 | namespace HL { |
---|
21 | |
---|
22 | class WinLockType { |
---|
23 | public: |
---|
24 | |
---|
25 | WinLockType (void) |
---|
26 | : mutex (0) |
---|
27 | {} |
---|
28 | |
---|
29 | ~WinLockType (void) |
---|
30 | { |
---|
31 | mutex = 0; |
---|
32 | } |
---|
33 | |
---|
34 | inline void lock (void) { |
---|
35 | #if 1 |
---|
36 | // We're on a multiprocessor - use atomic operations. |
---|
37 | while (InterlockedExchange ((long *) &mutex, 1) != 0) |
---|
38 | Sleep (0); |
---|
39 | #endif |
---|
40 | } |
---|
41 | |
---|
42 | inline void unlock (void) { |
---|
43 | mutex = 0; |
---|
44 | // InterlockedExchange (&mutex, 0); |
---|
45 | } |
---|
46 | |
---|
47 | private: |
---|
48 | unsigned int mutex; |
---|
49 | bool onMultiprocessor (void) { |
---|
50 | SYSTEM_INFO infoReturn[1]; |
---|
51 | GetSystemInfo (infoReturn); |
---|
52 | if (infoReturn->dwNumberOfProcessors == 1) { |
---|
53 | return FALSE; |
---|
54 | } else { |
---|
55 | return TRUE; |
---|
56 | } |
---|
57 | } |
---|
58 | }; |
---|
59 | |
---|
60 | }; |
---|
61 | |
---|
62 | #else |
---|
63 | |
---|
64 | #include <windows.h> |
---|
65 | |
---|
66 | class WinLockType { |
---|
67 | public: |
---|
68 | |
---|
69 | WinLockType (void) |
---|
70 | { |
---|
71 | InitializeCriticalSection(&mutex); |
---|
72 | } |
---|
73 | |
---|
74 | ~WinLockType (void) |
---|
75 | { |
---|
76 | DeleteCriticalSection(&mutex); |
---|
77 | } |
---|
78 | |
---|
79 | inline void lock (void) { |
---|
80 | EnterCriticalSection(&mutex); |
---|
81 | } |
---|
82 | |
---|
83 | inline void unlock (void) { |
---|
84 | LeaveCriticalSection(&mutex); |
---|
85 | } |
---|
86 | |
---|
87 | private: |
---|
88 | CRITICAL_SECTION mutex; |
---|
89 | }; |
---|
90 | |
---|
91 | #endif |
---|
92 | |
---|
93 | #endif |
---|
94 | |
---|
95 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.