mirror of https://gitee.com/bigwinds/arangodb
lock unification
This commit is contained in:
parent
e2853e4b52
commit
8d5248932b
|
@ -1,96 +0,0 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// DISCLAIMER
|
||||
///
|
||||
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
|
||||
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
|
||||
///
|
||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
/// you may not use this file except in compliance with the License.
|
||||
/// You may obtain a copy of the License at
|
||||
///
|
||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
///
|
||||
/// Unless required by applicable law or agreed to in writing, software
|
||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
/// See the License for the specific language governing permissions and
|
||||
/// limitations under the License.
|
||||
///
|
||||
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
||||
///
|
||||
/// @author Dr. Frank Celler
|
||||
/// @author Achim Brandt
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "MutexLocker.h"
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
#include "Logger/Logger.h"
|
||||
#endif
|
||||
|
||||
using namespace arangodb;
|
||||
using namespace arangodb::basics;
|
||||
|
||||
/// @brief aquires a lock
|
||||
///
|
||||
/// The constructor aquires a lock, the destructors releases the lock.
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
|
||||
MutexLocker::MutexLocker(Mutex* mutex, char const* file, int line)
|
||||
: _mutex(mutex), _isLocked(true), _file(file), _line(line), _time(0.0) {
|
||||
double t = TRI_microtime();
|
||||
_mutex->lock();
|
||||
_time = TRI_microtime() - t;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
MutexLocker::MutexLocker(Mutex* mutex) : _mutex(mutex), _isLocked(true) {
|
||||
_mutex->lock();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// @brief releases the lock
|
||||
MutexLocker::~MutexLocker() {
|
||||
if (_isLocked) {
|
||||
_mutex->unlock();
|
||||
}
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
if (_time > TRI_SHOW_LOCK_THRESHOLD) {
|
||||
LOG_TOPIC(WARN, arangodb::Logger::FIXME) << "MutexLocker " << _file << ":" << _line << " took " << _time << " s";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void MutexLocker::lock() {
|
||||
TRI_ASSERT(!_isLocked);
|
||||
_mutex->lock();
|
||||
_isLocked = true;
|
||||
}
|
||||
|
||||
void MutexLocker::unlock() {
|
||||
if (_isLocked) {
|
||||
_mutex->unlock();
|
||||
_isLocked = false;
|
||||
}
|
||||
}
|
||||
|
||||
TryMutexLocker::TryMutexLocker(Mutex* mutex) : _mutex(mutex), _isLocked(true) {
|
||||
_isLocked = _mutex->tryLock();
|
||||
}
|
||||
|
||||
/// @brief releases the lock
|
||||
TryMutexLocker::~TryMutexLocker() {
|
||||
if (_isLocked) {
|
||||
_mutex->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void TryMutexLocker::unlock() {
|
||||
if (_isLocked) {
|
||||
_mutex->unlock();
|
||||
_isLocked = false;
|
||||
}
|
||||
}
|
|
@ -26,109 +26,148 @@
|
|||
#define ARANGODB_BASICS_MUTEX_LOCKER_H 1
|
||||
|
||||
#include "Basics/Common.h"
|
||||
#include "Basics/Mutex.h"
|
||||
|
||||
/// @brief construct locker with file and line information
|
||||
///
|
||||
/// Ones needs to use macros twice to get a unique variable based on the line
|
||||
/// number.
|
||||
#define MUTEX_LOCKER_VAR_B(a) MUTEX_LOCKER_VAR_A(a)
|
||||
#include "Basics/Locking.h"
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
|
||||
#define MUTEX_LOCKER(obj, lock) \
|
||||
arangodb::basics::MutexLocker obj(&lock, __FILE__, __LINE__)
|
||||
|
||||
#else
|
||||
|
||||
#define MUTEX_LOCKER(obj, lock) arangodb::basics::MutexLocker obj(&lock)
|
||||
|
||||
#include "Logger/Logger.h"
|
||||
#endif
|
||||
|
||||
#define TRY_MUTEX_LOCKER(obj, lock) arangodb::basics::TryMutexLocker obj(&lock)
|
||||
#include <thread>
|
||||
|
||||
#define MUTEX_LOCKER(obj, lock) \
|
||||
arangodb::basics::MutexLocker<typename std::decay<decltype (lock)>::type> obj(&(lock), arangodb::basics::LockerType::BLOCKING, true, __FILE__, __LINE__)
|
||||
|
||||
#define MUTEX_LOCKER_EVENTUAL(obj, lock, t) \
|
||||
arangodb::basics::MutexLocker<typename std::decay<decltype (lock)>::type> obj(&(lock), arangodb::basics::LockerType::EVENTUAL, true, __FILE__, __LINE__)
|
||||
|
||||
#define TRY_MUTEX_LOCKER(obj, lock) \
|
||||
arangodb::basics::MutexLocker<typename std::decay<decltype (lock)>::type> obj(&(lock), arangodb::basics::LockerType::TRY, true, __FILE__, __LINE__)
|
||||
|
||||
#define CONDITIONAL_MUTEX_LOCKER(obj, lock, condition) \
|
||||
arangodb::basics::MutexLocker<typename std::decay<decltype (lock)>::type> obj(&(lock), arangodb::basics::LockerType::BLOCKING, (condition), __FILE__, __LINE__)
|
||||
|
||||
namespace arangodb {
|
||||
namespace basics {
|
||||
|
||||
/// @brief mutex locker
|
||||
///
|
||||
/// A MutexLocker locks a mutex during its lifetime und unlocks the mutex
|
||||
/// when it is destroyed.
|
||||
template<class LockType>
|
||||
class MutexLocker {
|
||||
MutexLocker(MutexLocker const&) = delete;
|
||||
MutexLocker& operator=(MutexLocker const&) = delete;
|
||||
|
||||
public:
|
||||
/// @brief aquires a lock
|
||||
///
|
||||
/// The constructor aquires a lock, the destructor releases the lock.
|
||||
/// @brief acquires a mutex
|
||||
/// The constructor acquires a read lock, the destructor unlocks the mutex.
|
||||
MutexLocker(LockType* mutex, LockerType type, bool condition, char const* file, int line)
|
||||
: _mutex(mutex), _file(file), _line(line),
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
|
||||
MutexLocker(Mutex* mutex, char const* file, int line);
|
||||
|
||||
_isLocked(false), _time(0.0) {
|
||||
#else
|
||||
|
||||
explicit MutexLocker(Mutex* mutex);
|
||||
|
||||
_isLocked(false) {
|
||||
#endif
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
// fetch current time
|
||||
double t = TRI_microtime();
|
||||
#endif
|
||||
|
||||
~MutexLocker();
|
||||
if (condition) {
|
||||
if (type == LockerType::BLOCKING) {
|
||||
lock();
|
||||
TRI_ASSERT(_isLocked);
|
||||
} else if (type == LockerType::EVENTUAL) {
|
||||
lockEventual();
|
||||
TRI_ASSERT(_isLocked);
|
||||
} else if (type == LockerType::TRY) {
|
||||
_isLocked = tryLock();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
// add elapsed time to time tracker
|
||||
_time = TRI_microtime() - t;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// @brief releases the read-lock
|
||||
~MutexLocker() {
|
||||
if (_isLocked) {
|
||||
_mutex->unlock();
|
||||
}
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
if (_time > TRI_SHOW_LOCK_THRESHOLD) {
|
||||
LOG_TOPIC(WARN, arangodb::Logger::PERFORMANCE) << "MutexLocker " << _file << ":" << _line << " took " << _time << " s";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool isLocked() const { return _isLocked; }
|
||||
|
||||
/// @brief locks the mutex
|
||||
void lock();
|
||||
/// @brief eventually acquire the read lock
|
||||
void lockEventual() {
|
||||
while (!tryLock()) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
TRI_ASSERT(_isLocked);
|
||||
}
|
||||
|
||||
/// @brief releases the mutex
|
||||
void unlock();
|
||||
bool tryLock() {
|
||||
TRI_ASSERT(!_isLocked);
|
||||
if (_mutex->tryLock()) {
|
||||
_isLocked = true;
|
||||
}
|
||||
return _isLocked;
|
||||
}
|
||||
|
||||
/// @brief acquire the mutex, blocking
|
||||
void lock() {
|
||||
TRI_ASSERT(!_isLocked);
|
||||
_mutex->lock();
|
||||
_isLocked = true;
|
||||
}
|
||||
|
||||
/// @brief unlocks the mutex if we own it
|
||||
bool unlock() {
|
||||
if (_isLocked) {
|
||||
_mutex->unlock();
|
||||
_isLocked = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// @brief steals the lock, but does not unlock it
|
||||
bool steal() {
|
||||
if (_isLocked) {
|
||||
_isLocked = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
/// @brief the mutex
|
||||
Mutex* _mutex;
|
||||
LockType* _mutex;
|
||||
|
||||
/// @brief whether or not the mutex is locked
|
||||
bool _isLocked;
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
|
||||
/// @brief file
|
||||
char const* _file;
|
||||
|
||||
/// @brief line number
|
||||
int _line;
|
||||
|
||||
/// @brief whether or not the mutex is locked
|
||||
bool _isLocked;
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
/// @brief lock time
|
||||
double _time;
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
class TryMutexLocker {
|
||||
TryMutexLocker(MutexLocker const&) = delete;
|
||||
TryMutexLocker& operator=(MutexLocker const&) = delete;
|
||||
|
||||
public:
|
||||
/// @brief tries to aquire a lock
|
||||
///
|
||||
/// The constructor aquires a lock, the destructor releases the lock.
|
||||
explicit TryMutexLocker(Mutex* mutex);
|
||||
|
||||
~TryMutexLocker();
|
||||
|
||||
bool isLocked() const { return _isLocked; }
|
||||
|
||||
/// @brief releases the lock
|
||||
void unlock();
|
||||
|
||||
private:
|
||||
/// @brief the mutex
|
||||
Mutex* _mutex;
|
||||
|
||||
/// @brief whether or not the mutex is locked
|
||||
bool _isLocked;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,6 @@
|
|||
|
||||
#include "Basics/Common.h"
|
||||
#include "Basics/Locking.h"
|
||||
#include "Basics/ReadWriteLock.h"
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
#include "Logger/Logger.h"
|
||||
|
@ -38,16 +37,16 @@
|
|||
|
||||
/// @brief construct locker with file and line information
|
||||
#define READ_LOCKER(obj, lock) \
|
||||
arangodb::basics::ReadLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, true, __FILE__, __LINE__)
|
||||
arangodb::basics::ReadLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, true, __FILE__, __LINE__)
|
||||
|
||||
#define READ_LOCKER_EVENTUAL(obj, lock, t) \
|
||||
arangodb::basics::ReadLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::EVENTUAL, true, __FILE__, __LINE__)
|
||||
arangodb::basics::ReadLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::EVENTUAL, true, __FILE__, __LINE__)
|
||||
|
||||
#define TRY_READ_LOCKER(obj, lock) \
|
||||
arangodb::basics::ReadLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::TRY, true, __FILE__, __LINE__)
|
||||
arangodb::basics::ReadLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::TRY, true, __FILE__, __LINE__)
|
||||
|
||||
#define CONDITIONAL_READ_LOCKER(obj, lock, condition) \
|
||||
arangodb::basics::ReadLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, (condition), __FILE__, __LINE__)
|
||||
arangodb::basics::ReadLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, (condition), __FILE__, __LINE__)
|
||||
|
||||
namespace arangodb {
|
||||
namespace basics {
|
||||
|
@ -61,8 +60,8 @@ class ReadLocker {
|
|||
ReadLocker& operator=(ReadLocker const&) = delete;
|
||||
|
||||
public:
|
||||
/// @brief aquires a read-lock
|
||||
/// The constructors acquire a read lock, the destructor unlocks the lock.
|
||||
/// @brief acquires a read-lock
|
||||
/// The constructor acquires a read lock, the destructor unlocks the lock.
|
||||
ReadLocker(LockType* readWriteLock, LockerType type, bool condition, char const* file, int line)
|
||||
: _readWriteLock(readWriteLock), _file(file), _line(line),
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
|
|
|
@ -28,7 +28,6 @@
|
|||
|
||||
#include "Basics/Common.h"
|
||||
#include "Basics/Locking.h"
|
||||
#include "Basics/ReadWriteLock.h"
|
||||
|
||||
#ifdef TRI_SHOW_LOCK_TIME
|
||||
#include "Logger/Logger.h"
|
||||
|
@ -38,16 +37,16 @@
|
|||
|
||||
/// @brief construct locker with file and line information
|
||||
#define WRITE_LOCKER(obj, lock) \
|
||||
arangodb::basics::WriteLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, true, __FILE__, __LINE__)
|
||||
arangodb::basics::WriteLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, true, __FILE__, __LINE__)
|
||||
|
||||
#define WRITE_LOCKER_EVENTUAL(obj, lock) \
|
||||
arangodb::basics::WriteLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::EVENTUAL, true, __FILE__, __LINE__)
|
||||
arangodb::basics::WriteLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::EVENTUAL, true, __FILE__, __LINE__)
|
||||
|
||||
#define TRY_WRITE_LOCKER(obj, lock) \
|
||||
arangodb::basics::WriteLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::TRY, true, __FILE__, __LINE__)
|
||||
arangodb::basics::WriteLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::TRY, true, __FILE__, __LINE__)
|
||||
|
||||
#define CONDITIONAL_WRITE_LOCKER(obj, lock, condition) \
|
||||
arangodb::basics::WriteLocker<std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, (condition), __FILE__, __LINE__)
|
||||
arangodb::basics::WriteLocker<typename std::decay<decltype (lock)>::type> obj(&lock, arangodb::basics::LockerType::BLOCKING, (condition), __FILE__, __LINE__)
|
||||
|
||||
namespace arangodb {
|
||||
namespace basics {
|
||||
|
@ -62,7 +61,7 @@ class WriteLocker {
|
|||
|
||||
public:
|
||||
|
||||
/// @brief aquires a write-lock
|
||||
/// @brief acquires a write-lock
|
||||
/// The constructors acquire a write lock, the destructor unlocks the lock.
|
||||
WriteLocker(LockType* readWriteLock, LockerType type, bool condition, char const* file, int line)
|
||||
: _readWriteLock(readWriteLock), _file(file), _line(line),
|
||||
|
|
|
@ -132,7 +132,6 @@ add_library(${LIB_ARANGO} STATIC
|
|||
Basics/LdapUrlParser.cpp
|
||||
Basics/LocalTaskQueue.cpp
|
||||
Basics/Mutex.cpp
|
||||
Basics/MutexLocker.cpp
|
||||
Basics/Nonce.cpp
|
||||
Basics/OpenFilesTracker.cpp
|
||||
Basics/ReadWriteLockCPP11.cpp
|
||||
|
|
Loading…
Reference in New Issue