| 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 "../ThreadOps.h" |
|---|
| 28 | #include <assert.h> |
|---|
| 29 | #include <asm/atomic.h> |
|---|
| 30 | |
|---|
| 31 | #if !defined(NDEBUG) |
|---|
| 32 | # include <pthread.h> |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | namespace ZThread { |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * @class FastLock |
|---|
| 39 | * |
|---|
| 40 | * @author Eric Crahen <http://www.code-foo.com> |
|---|
| 41 | * @date <2003-07-16T23:27:03-0400> |
|---|
| 42 | * @version 2.2.0 |
|---|
| 43 | * |
|---|
| 44 | * This implementation of a FastLock uses the atomic operations that |
|---|
| 45 | * linux provides with its kernel sources. This demonstrates how to implement |
|---|
| 46 | * a spinlock with a decrement and test primative. |
|---|
| 47 | */ |
|---|
| 48 | class FastLock : private NonCopyable { |
|---|
| 49 | |
|---|
| 50 | atomic_t _value; |
|---|
| 51 | |
|---|
| 52 | #if !defined(NDEBUG) |
|---|
| 53 | pthread_t _owner; |
|---|
| 54 | #endif |
|---|
| 55 | |
|---|
| 56 | public: |
|---|
| 57 | |
|---|
| 58 | inline FastLock() { |
|---|
| 59 | |
|---|
| 60 | atomic_t tmp = ATOMIC_INIT(1); |
|---|
| 61 | _value = tmp; |
|---|
| 62 | |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | inline ~FastLock() { |
|---|
| 66 | |
|---|
| 67 | assert(atomic_read(&_value) == 1); |
|---|
| 68 | assert(_owner == 0); |
|---|
| 69 | |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | inline void acquire() { |
|---|
| 73 | |
|---|
| 74 | while(!atomic_dec_and_test(&_value)) { |
|---|
| 75 | |
|---|
| 76 | atomic_inc(&_value); |
|---|
| 77 | ThreadOps::yield(); |
|---|
| 78 | |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | #if !defined(NDEBUG) |
|---|
| 82 | _owner = pthread_self(); |
|---|
| 83 | #endif |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | inline void release() { |
|---|
| 87 | |
|---|
| 88 | #if !defined(NDEBUG) |
|---|
| 89 | assert(pthread_equal(_owner, pthread_self()) != 0); |
|---|
| 90 | #endif |
|---|
| 91 | |
|---|
| 92 | atomic_inc(&_value); |
|---|
| 93 | _owner = 0; |
|---|
| 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | inline bool tryAcquire(unsigned long timeout=0) { |
|---|
| 98 | |
|---|
| 99 | bool wasLocked = atomic_dec_and_test(&_value); |
|---|
| 100 | if(!wasLocked) |
|---|
| 101 | atomic_inc(&_value); |
|---|
| 102 | |
|---|
| 103 | #if !defined(NDEBUG) |
|---|
| 104 | if(wasLocked) |
|---|
| 105 | _owner = pthread_self(); |
|---|
| 106 | #endif |
|---|
| 107 | |
|---|
| 108 | return wasLocked; |
|---|
| 109 | |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | }; /* FastLock */ |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | } // namespace ZThread |
|---|
| 116 | |
|---|
| 117 | #endif |
|---|