1 | /* |
---|
2 | * Copyright (c) 2005, Eric Crahen |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
5 | * of this software and associated documentation files (the "Software"), to deal |
---|
6 | * in the Software without restriction, including without limitation the rights |
---|
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
8 | * copies of the Software, and to permit persons to whom the Software is furnished |
---|
9 | * to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in all |
---|
12 | * copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __ZTFASTLOCK_H__ |
---|
24 | #define __ZTFASTLOCK_H__ |
---|
25 | |
---|
26 | #include "zthread/NonCopyable.h" |
---|
27 | #include <windows.h> |
---|
28 | #include <assert.h> |
---|
29 | |
---|
30 | namespace ZThread { |
---|
31 | |
---|
32 | |
---|
33 | /** |
---|
34 | * @class FastLock |
---|
35 | * |
---|
36 | * @author Eric Crahen <http://www.code-foo.com> |
---|
37 | * @date <2003-07-16T23:31:51-0400> |
---|
38 | * @version 2.2.0 |
---|
39 | * |
---|
40 | * This uses a custom spin lock based on the older swap & compare approach |
---|
41 | * using the XCHG instruction. You should only use this is you *absolutely* need |
---|
42 | * to use an older system, like Windows 95. If you can, use the Win32 version. |
---|
43 | * |
---|
44 | * Because Windows 95 and earlier can run on processors prior to the 486, they |
---|
45 | * don't all support the CMPXCHG function, and so Windows 95 an earlier dont support |
---|
46 | * InterlockedCompareExchange. |
---|
47 | * |
---|
48 | * This is asm inlined for microsoft visual c, it needs to be changed in order to |
---|
49 | * compile with gcc, or another win32 compiler - but more likely than not you'll |
---|
50 | * be using the Win32 version on those compilers and this won't be a problem. |
---|
51 | */ |
---|
52 | class FastLock : private NonCopyable { |
---|
53 | |
---|
54 | // Add def for mingw32 or other non-ms compiler to align on 32-bit boundary |
---|
55 | #pragma pack(push, 4) |
---|
56 | unsigned char volatile _lock; |
---|
57 | #pragma pack(pop) |
---|
58 | |
---|
59 | public: |
---|
60 | |
---|
61 | /** |
---|
62 | * Create a new FastLock |
---|
63 | */ |
---|
64 | inline FastLock() : _lock(0) { } |
---|
65 | |
---|
66 | |
---|
67 | inline ~FastLock() { |
---|
68 | assert(_lock == 0); |
---|
69 | } |
---|
70 | |
---|
71 | inline void acquire() { |
---|
72 | |
---|
73 | DWORD dw = (DWORD)&_lock; |
---|
74 | |
---|
75 | _asm { // swap & compare |
---|
76 | spin_lock: |
---|
77 | |
---|
78 | mov al, 1 |
---|
79 | mov esi, dw |
---|
80 | xchg [esi], al |
---|
81 | and al,al |
---|
82 | jz spin_locked |
---|
83 | } |
---|
84 | |
---|
85 | ::Sleep(0); |
---|
86 | |
---|
87 | _asm { |
---|
88 | jmp spin_lock |
---|
89 | spin_locked: |
---|
90 | } |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | inline void release() { |
---|
95 | |
---|
96 | DWORD dw = (DWORD)&_lock; |
---|
97 | |
---|
98 | _asm { |
---|
99 | mov al, 0 |
---|
100 | mov esi, dw |
---|
101 | xchg [esi], al |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | inline bool tryAcquire(unsigned long timeout=0) { |
---|
108 | |
---|
109 | volatile DWORD dw = (DWORD)&_lock; |
---|
110 | volatile DWORD result; |
---|
111 | |
---|
112 | _asm { |
---|
113 | |
---|
114 | mov al, 1 |
---|
115 | mov esi, dw |
---|
116 | xchg [esi], al |
---|
117 | and al,al |
---|
118 | mov esi, result |
---|
119 | xchg [esi], al |
---|
120 | } |
---|
121 | |
---|
122 | return result == 0; |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | }; /* Fast Lock */ |
---|
127 | |
---|
128 | } // namespace ZThread |
---|
129 | |
---|
130 | #endif |
---|