kernel: Add sequential lock implementation

This commit is contained in:
Pawel Dziepak
2013-11-05 04:16:13 +01:00
parent 958f6d00aa
commit 4824f7630b
4 changed files with 158 additions and 0 deletions
+45
View File
@@ -103,4 +103,49 @@ release_spinlock_inline(spinlock* lock)
#endif // !DEBUG_SPINLOCKS && !B_DEBUG_SPINLOCK_CONTENTION
static inline bool
try_acquire_write_seqlock_inline(seqlock* lock) {
bool succeed = try_acquire_spinlock(&lock->lock);
if (succeed)
atomic_add(&lock->count, 1);
return succeed;
}
static inline void
acquire_write_seqlock_inline(seqlock* lock) {
acquire_spinlock(&lock->lock);
atomic_add(&lock->count, 1);
}
static inline void
release_write_seqlock_inline(seqlock* lock) {
atomic_add(&lock->count, 1);
release_spinlock(&lock->lock);
}
static inline uint32
acquire_read_seqlock_inline(seqlock* lock) {
return atomic_get(&lock->count);
}
static inline bool
release_read_seqlock_inline(seqlock* lock, uint32 count) {
uint32 current = atomic_get(&lock->count);
return count % 2 == 0 && current == count;
}
#define try_acquire_write_seqlock(lock) try_acquire_write_seqlock_inline(lock)
#define acquire_write_seqlock(lock) acquire_write_seqlock_inline(lock)
#define release_write_seqlock(lock) release_write_seqlock_inline(lock)
#define acquire_read_seqlock(lock) acquire_read_seqlock_inline(lock)
#define release_read_seqlock(lock, count) \
release_read_seqlock_inline(lock, count)
#endif /* KERNEL_SMP_H */
+48
View File
@@ -160,6 +160,52 @@ private:
typedef AutoLocker<spinlock, InterruptsSpinLocking> InterruptsSpinLocker;
class WriteSequentialLocking {
public:
inline bool Lock(seqlock* lockable)
{
acquire_write_seqlock(lockable);
return true;
}
inline void Unlock(seqlock* lockable)
{
release_write_seqlock(lockable);
}
};
typedef AutoLocker<seqlock, WriteSequentialLocking> WriteSequentialLocker;
class InterruptsWriteSequentialLocking {
public:
InterruptsWriteSequentialLocking()
:
fState(0)
{
}
inline bool Lock(seqlock* lockable)
{
fState = disable_interrupts();
acquire_write_seqlock(lockable);
return true;
}
inline void Unlock(seqlock* lockable)
{
release_write_seqlock(lockable);
restore_interrupts(fState);
}
private:
int fState;
};
typedef AutoLocker<seqlock, InterruptsWriteSequentialLocking>
InterruptsWriteSequentialLocker;
class ThreadCPUPinLocking {
public:
inline bool Lock(Thread* thread)
@@ -191,6 +237,8 @@ using BPrivate::WriteLocker;
using BPrivate::InterruptsLocker;
using BPrivate::SpinLocker;
using BPrivate::InterruptsSpinLocker;
using BPrivate::WriteSequentialLocker;
using BPrivate::InterruptsWriteSequentialLocker;
using BPrivate::ThreadCPUPinner;
using BPrivate::TeamLocker;
using BPrivate::ThreadLocker;