2012-06-30 01:10:08 +08:00
|
|
|
//===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-06-30 01:32:18 +08:00
|
|
|
//
|
|
|
|
// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-06-30 01:10:08 +08:00
|
|
|
|
|
|
|
#ifndef SANITIZER_MUTEX_H
|
|
|
|
#define SANITIZER_MUTEX_H
|
|
|
|
|
|
|
|
#include "sanitizer_atomic.h"
|
2012-07-02 15:09:21 +08:00
|
|
|
#include "sanitizer_internal_defs.h"
|
|
|
|
#include "sanitizer_libc.h"
|
2012-06-30 01:10:08 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2012-08-30 18:02:48 +08:00
|
|
|
class StaticSpinMutex {
|
2012-06-30 01:32:18 +08:00
|
|
|
public:
|
2012-08-30 18:02:48 +08:00
|
|
|
void Init() {
|
2012-06-30 01:32:18 +08:00
|
|
|
atomic_store(&state_, 0, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lock() {
|
2013-01-11 19:03:35 +08:00
|
|
|
if (TryLock())
|
2012-07-02 15:09:21 +08:00
|
|
|
return;
|
|
|
|
LockSlow();
|
2012-06-30 01:32:18 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 19:03:35 +08:00
|
|
|
bool TryLock() {
|
|
|
|
return atomic_exchange(&state_, 1, memory_order_acquire) == 0;
|
|
|
|
}
|
|
|
|
|
2012-06-30 01:32:18 +08:00
|
|
|
void Unlock() {
|
|
|
|
atomic_store(&state_, 0, memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
atomic_uint8_t state_;
|
|
|
|
|
2012-07-02 15:09:21 +08:00
|
|
|
void NOINLINE LockSlow() {
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
if (i < 10)
|
|
|
|
proc_yield(10);
|
|
|
|
else
|
|
|
|
internal_sched_yield();
|
|
|
|
if (atomic_load(&state_, memory_order_relaxed) == 0
|
|
|
|
&& atomic_exchange(&state_, 1, memory_order_acquire) == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-08-30 18:02:48 +08:00
|
|
|
};
|
2012-07-02 15:09:21 +08:00
|
|
|
|
2012-08-30 18:02:48 +08:00
|
|
|
class SpinMutex : public StaticSpinMutex {
|
|
|
|
public:
|
|
|
|
SpinMutex() {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-06-30 01:32:18 +08:00
|
|
|
SpinMutex(const SpinMutex&);
|
|
|
|
void operator=(const SpinMutex&);
|
|
|
|
};
|
|
|
|
|
2013-01-14 15:51:39 +08:00
|
|
|
class BlockingMutex {
|
|
|
|
public:
|
|
|
|
explicit BlockingMutex(LinkerInitialized);
|
2013-03-14 21:30:56 +08:00
|
|
|
BlockingMutex();
|
2013-01-14 15:51:39 +08:00
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2013-03-11 23:45:20 +08:00
|
|
|
void CheckLocked();
|
2013-01-14 15:51:39 +08:00
|
|
|
private:
|
|
|
|
uptr opaque_storage_[10];
|
2013-01-14 16:01:58 +08:00
|
|
|
uptr owner_; // for debugging
|
2013-01-14 15:51:39 +08:00
|
|
|
};
|
|
|
|
|
2012-06-30 01:10:08 +08:00
|
|
|
template<typename MutexType>
|
|
|
|
class GenericScopedLock {
|
|
|
|
public:
|
|
|
|
explicit GenericScopedLock(MutexType *mu)
|
|
|
|
: mu_(mu) {
|
|
|
|
mu_->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~GenericScopedLock() {
|
|
|
|
mu_->Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MutexType *mu_;
|
|
|
|
|
|
|
|
GenericScopedLock(const GenericScopedLock&);
|
|
|
|
void operator=(const GenericScopedLock&);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename MutexType>
|
|
|
|
class GenericScopedReadLock {
|
|
|
|
public:
|
|
|
|
explicit GenericScopedReadLock(MutexType *mu)
|
|
|
|
: mu_(mu) {
|
|
|
|
mu_->ReadLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~GenericScopedReadLock() {
|
|
|
|
mu_->ReadUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MutexType *mu_;
|
|
|
|
|
|
|
|
GenericScopedReadLock(const GenericScopedReadLock&);
|
|
|
|
void operator=(const GenericScopedReadLock&);
|
|
|
|
};
|
|
|
|
|
2012-08-30 18:02:48 +08:00
|
|
|
typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock;
|
2013-01-14 15:51:39 +08:00
|
|
|
typedef GenericScopedLock<BlockingMutex> BlockingMutexLock;
|
2012-07-02 14:54:24 +08:00
|
|
|
|
2012-06-30 01:10:08 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_MUTEX_H
|