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 __ZTFASTRECURSIVELOCK_H__ |
---|
24 | #define __ZTFASTRECURSIVELOCK_H__ |
---|
25 | |
---|
26 | #include "../FastLock.h" |
---|
27 | #include "../ThreadOps.h" |
---|
28 | #include <assert.h> |
---|
29 | |
---|
30 | namespace ZThread { |
---|
31 | |
---|
32 | /** |
---|
33 | * @class FastRecursiveLock |
---|
34 | * |
---|
35 | * @author Eric Crahen <http://www.code-foo.com> |
---|
36 | * @date <2003-07-16T23:30:59-0400> |
---|
37 | * @version 2.2.8 |
---|
38 | * |
---|
39 | * This implementation of a FastRecursiveLock uses the which ever FastLock |
---|
40 | * that is selected to create a recursive spin lock. |
---|
41 | */ |
---|
42 | class FastRecursiveLock : private NonCopyable { |
---|
43 | |
---|
44 | FastLock _lock; |
---|
45 | |
---|
46 | ThreadOps _owner; |
---|
47 | |
---|
48 | volatile unsigned int _count; |
---|
49 | |
---|
50 | public: |
---|
51 | |
---|
52 | inline FastRecursiveLock() : _owner(ThreadOps::INVALID), _count(0) {} |
---|
53 | |
---|
54 | inline ~FastRecursiveLock() { |
---|
55 | |
---|
56 | assert(_owner == ThreadOps::INVALID); |
---|
57 | assert(_count == 0); |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | inline void acquire() { |
---|
62 | |
---|
63 | ThreadOps self(ThreadOps::self()); |
---|
64 | bool wasLocked = false; |
---|
65 | |
---|
66 | do { |
---|
67 | |
---|
68 | _lock.acquire(); |
---|
69 | |
---|
70 | // If there is no owner, or the owner is the caller |
---|
71 | // update the count |
---|
72 | if(_owner == ThreadOps::INVALID || _owner == self) { |
---|
73 | |
---|
74 | _owner = self; |
---|
75 | _count++; |
---|
76 | |
---|
77 | wasLocked = true; |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | _lock.release(); |
---|
82 | |
---|
83 | } while(!wasLocked); |
---|
84 | |
---|
85 | assert(_owner == ThreadOps::self()); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | inline void release() { |
---|
90 | |
---|
91 | assert(_owner == ThreadOps::self()); |
---|
92 | |
---|
93 | _lock.acquire(); |
---|
94 | |
---|
95 | if(--_count == 0) |
---|
96 | _owner = ThreadOps::INVALID; |
---|
97 | |
---|
98 | _lock.release(); |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | inline bool tryAcquire(unsigned long timeout=0) { |
---|
103 | |
---|
104 | ThreadOps self(ThreadOps::self()); |
---|
105 | bool wasLocked = false; |
---|
106 | |
---|
107 | _lock.acquire(); |
---|
108 | |
---|
109 | if(_owner == ThreadOps::INVALID || _owner == self) { |
---|
110 | |
---|
111 | _owner = self; |
---|
112 | _count++; |
---|
113 | |
---|
114 | wasLocked = true; |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | _lock.release(); |
---|
119 | |
---|
120 | assert(!wasLocked || _owner == ThreadOps::self()); |
---|
121 | return wasLocked; |
---|
122 | |
---|
123 | } |
---|
124 | |
---|
125 | }; /* FastRecursiveLock */ |
---|
126 | |
---|
127 | |
---|
128 | } // namespace ZThread |
---|
129 | |
---|
130 | #endif |
---|