1 /* 2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. 3 * Copyright 2005-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 #ifndef KERNEL_UTIL_AUTO_LOCKER_H 8 #define KERNEL_UTIL_AUTO_LOCKER_H 9 10 11 #include <KernelExport.h> 12 13 #include <shared/AutoLocker.h> 14 15 #include <int.h> 16 #include <lock.h> 17 18 19 namespace BPrivate { 20 21 // MutexLocking 22 class MutexLocking { 23 public: 24 inline bool Lock(mutex *lockable) 25 { 26 return mutex_lock(lockable) == B_OK; 27 } 28 29 inline void Unlock(mutex *lockable) 30 { 31 mutex_unlock(lockable); 32 } 33 }; 34 35 // MutexLocker 36 typedef AutoLocker<mutex, MutexLocking> MutexLocker; 37 38 // RecursiveLockLocking 39 class RecursiveLockLocking { 40 public: 41 inline bool Lock(recursive_lock *lockable) 42 { 43 return recursive_lock_lock(lockable) == B_OK; 44 } 45 46 inline void Unlock(recursive_lock *lockable) 47 { 48 recursive_lock_unlock(lockable); 49 } 50 }; 51 52 // RecursiveLocker 53 typedef AutoLocker<recursive_lock, RecursiveLockLocking> RecursiveLocker; 54 55 class ReadWriteLockReadLocking { 56 public: 57 inline bool Lock(rw_lock *lockable) 58 { 59 return rw_lock_read_lock(lockable) == B_OK; 60 } 61 62 inline void Unlock(rw_lock *lockable) 63 { 64 rw_lock_read_unlock(lockable); 65 } 66 }; 67 68 class ReadWriteLockWriteLocking { 69 public: 70 inline bool Lock(rw_lock *lockable) 71 { 72 return rw_lock_write_lock(lockable) == B_OK; 73 } 74 75 inline void Unlock(rw_lock *lockable) 76 { 77 rw_lock_write_unlock(lockable); 78 } 79 }; 80 81 typedef AutoLocker<rw_lock, ReadWriteLockReadLocking> ReadLocker; 82 typedef AutoLocker<rw_lock, ReadWriteLockWriteLocking> WriteLocker; 83 84 // InterruptsLocking 85 class InterruptsLocking { 86 public: 87 inline bool Lock(int* lockable) 88 { 89 *lockable = disable_interrupts(); 90 return true; 91 } 92 93 inline void Unlock(int* lockable) 94 { 95 restore_interrupts(*lockable); 96 } 97 }; 98 99 // InterruptsLocker 100 class InterruptsLocker : public AutoLocker<int, InterruptsLocking> { 101 public: 102 inline InterruptsLocker(bool alreadyLocked = false, 103 bool lockIfNotLocked = true) 104 : AutoLocker<int, InterruptsLocking>(&fState, alreadyLocked, 105 lockIfNotLocked) 106 { 107 } 108 109 private: 110 int fState; 111 }; 112 113 // SpinLocking 114 class SpinLocking { 115 public: 116 inline bool Lock(spinlock* lockable) 117 { 118 acquire_spinlock(lockable); 119 return true; 120 } 121 122 inline void Unlock(spinlock* lockable) 123 { 124 release_spinlock(lockable); 125 } 126 }; 127 128 // SpinLocker 129 typedef AutoLocker<spinlock, SpinLocking> SpinLocker; 130 131 // InterruptsSpinLocking 132 class InterruptsSpinLocking { 133 public: 134 struct State { 135 State(spinlock* lock) 136 : lock(lock) 137 { 138 } 139 140 int state; 141 spinlock* lock; 142 }; 143 144 inline bool Lock(spinlock* lockable) 145 { 146 fState = disable_interrupts(); 147 acquire_spinlock(lockable); 148 return true; 149 } 150 151 inline void Unlock(spinlock* lockable) 152 { 153 release_spinlock(lockable); 154 restore_interrupts(fState); 155 } 156 157 private: 158 int fState; 159 }; 160 161 // InterruptsSpinLocker 162 typedef AutoLocker<spinlock, InterruptsSpinLocking> InterruptsSpinLocker; 163 164 } // namespace BPrivate 165 166 using BPrivate::AutoLocker; 167 using BPrivate::MutexLocker; 168 using BPrivate::RecursiveLocker; 169 using BPrivate::ReadLocker; 170 using BPrivate::WriteLocker; 171 using BPrivate::InterruptsLocker; 172 using BPrivate::SpinLocker; 173 using BPrivate::InterruptsSpinLocker; 174 175 #endif // KERNEL_UTIL_AUTO_LOCKER_H 176