1
0
Fork 0

add SpinLock and SpinLocker, unified other Lockers

This commit is contained in:
Jan Steemann 2015-07-15 21:22:00 +02:00
parent 1a3a18ea3e
commit 6d4dbcbb47
12 changed files with 425 additions and 21 deletions

View File

@ -30,9 +30,6 @@
#include "MutexLocker.h"
#include "Basics/Exceptions.h"
#include "Basics/StringUtils.h"
using namespace triagens::basics;
// -----------------------------------------------------------------------------
@ -42,18 +39,17 @@ using namespace triagens::basics;
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructors aquires a lock, the destructors releases the lock.
/// The constructor aquires a lock, the destructors releases the lock.
////////////////////////////////////////////////////////////////////////////////
MutexLocker::MutexLocker (Mutex* mutex)
: _mutex(mutex), _file(0), _line(0) {
_mutex->lock();
: MutexLocker(mutex, nullptr, 0) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructors aquires a lock, the destructors releases the lock.
/// The constructor aquires a lock, the destructors releases the lock.
////////////////////////////////////////////////////////////////////////////////
MutexLocker::MutexLocker (Mutex* mutex, char const* file, int line)

View File

@ -32,7 +32,6 @@
#define ARANGODB_BASICS_MUTEX_LOCKER_H 1
#include "Basics/Common.h"
#include "Basics/Mutex.h"
// -----------------------------------------------------------------------------
@ -53,7 +52,7 @@
triagens::basics::MutexLocker MUTEX_LOCKER_VAR_B(__LINE__)(&b, __FILE__, __LINE__)
// -----------------------------------------------------------------------------
// --SECTION-- class ReadLocker
// --SECTION-- class MutexLocker
// -----------------------------------------------------------------------------
namespace triagens {
@ -79,7 +78,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructors aquires a lock, the destructors releases the lock.
/// The constructor aquires a lock, the destructor releases the lock.
////////////////////////////////////////////////////////////////////////////////
explicit
@ -88,7 +87,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructors aquires a lock, the destructors releases the lock.
/// The constructor aquires a lock, the destructor releases the lock.
////////////////////////////////////////////////////////////////////////////////
MutexLocker (Mutex* mutex, char const* file, int line);
@ -122,6 +121,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
int _line;
};
}
}

View File

@ -43,8 +43,7 @@ using namespace triagens::basics;
////////////////////////////////////////////////////////////////////////////////
ReadLocker::ReadLocker (ReadWriteLock* readWriteLock)
: _readWriteLock(readWriteLock), _file(0), _line(0) {
_readWriteLock->readLock();
: ReadLocker(readWriteLock, nullptr, 0) {
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -32,7 +32,6 @@
#define ARANGODB_BASICS_READ_LOCKER_H 1
#include "Basics/Common.h"
#include "Basics/ReadWriteLock.h"
// -----------------------------------------------------------------------------
@ -83,7 +82,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
explicit
ReadLocker (ReadWriteLock* readWriteLock);
ReadLocker (ReadWriteLock*);
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a read-lock
@ -122,6 +121,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
int _line;
};
}
}

83
lib/Basics/SpinLock.cpp Normal file
View File

@ -0,0 +1,83 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief SpinLock
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 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
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2008-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "SpinLock.h"
using namespace triagens::basics;
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief constructs a spinlock
////////////////////////////////////////////////////////////////////////////////
SpinLock::SpinLock ()
: _lock() {
TRI_InitSpin(&_lock);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief deletes the lock
////////////////////////////////////////////////////////////////////////////////
SpinLock::~SpinLock () {
TRI_DestroySpin(&_lock);
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief acquires the lock
////////////////////////////////////////////////////////////////////////////////
void SpinLock::lock () {
TRI_LockSpin(&_lock);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief releases the lock
////////////////////////////////////////////////////////////////////////////////
void SpinLock::unlock () {
TRI_UnlockSpin(&_lock);
}
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

113
lib/Basics/SpinLock.h Normal file
View File

@ -0,0 +1,113 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief SpinLock
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 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
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_BASICS_SPIN_LOCK_H
#define ARANGODB_BASICS_SPIN_LOCK_H 1
#include "Basics/Common.h"
#include "Basics/locks.h"
namespace triagens {
namespace basics {
// -----------------------------------------------------------------------------
// --SECTION-- class SpinLock
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief spinlock
////////////////////////////////////////////////////////////////////////////////
class SpinLock {
private:
SpinLock (SpinLock const&) = delete;
SpinLock& operator= (SpinLock const&) = delete;
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief constructs a spinlock
////////////////////////////////////////////////////////////////////////////////
SpinLock ();
////////////////////////////////////////////////////////////////////////////////
/// @brief deletes the spinlock
////////////////////////////////////////////////////////////////////////////////
~SpinLock ();
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief acquires the lock
////////////////////////////////////////////////////////////////////////////////
void lock ();
////////////////////////////////////////////////////////////////////////////////
/// @brief releases the lock
////////////////////////////////////////////////////////////////////////////////
void unlock ();
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief spinlock variable
////////////////////////////////////////////////////////////////////////////////
TRI_spin_t _lock;
};
}
}
#endif
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

75
lib/Basics/SpinLocker.cpp Normal file
View File

@ -0,0 +1,75 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief SpinLocker
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 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
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2008-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "SpinLocker.h"
using namespace triagens::basics;
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructor aquires a lock, the destructor releases the lock.
////////////////////////////////////////////////////////////////////////////////
SpinLocker::SpinLocker (SpinLock* lock)
: SpinLocker(lock, nullptr, 0) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructor aquires a lock, the destructor releases the lock.
////////////////////////////////////////////////////////////////////////////////
SpinLocker::SpinLocker (SpinLock* lock, char const* file, int line)
: _lock(lock), _file(file), _line(line) {
_lock->lock();
}
////////////////////////////////////////////////////////////////////////////////
/// @brief releases the lock
////////////////////////////////////////////////////////////////////////////////
SpinLocker::~SpinLocker () {
_lock->unlock();
}
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

138
lib/Basics/SpinLocker.h Normal file
View File

@ -0,0 +1,138 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Spin Locker
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 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
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2008-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_BASICS_SPIN_LOCKER_H
#define ARANGODB_BASICS_SPIN_LOCKER_H 1
#include "Basics/Common.h"
#include "Basics/SpinLock.h"
// -----------------------------------------------------------------------------
// --SECTION-- public macros
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @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 SPIN_LOCKER_VAR_A(a) _spin_lock_variable_ ## a
#define SPIN_LOCKER_VAR_B(a) SPIN_LOCKER_VAR_A(a)
#define SPIN_LOCKER(b) \
triagens::basics::SpinLocker SPIN_LOCKER_VAR_B(__LINE__)(&b, __FILE__, __LINE__)
// -----------------------------------------------------------------------------
// --SECTION-- class SpinLocker
// -----------------------------------------------------------------------------
namespace triagens {
namespace basics {
////////////////////////////////////////////////////////////////////////////////
/// @brief spin locker
///
/// A SpinLocker locks a spinlock during its lifetime und unlocks the spinlock
/// when it is destroyed.
////////////////////////////////////////////////////////////////////////////////
class SpinLocker {
SpinLocker (SpinLocker const&);
SpinLocker& operator= (SpinLocker const&);
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructor aquires a lock, the destructors releases the lock.
////////////////////////////////////////////////////////////////////////////////
explicit
SpinLocker (SpinLock*);
////////////////////////////////////////////////////////////////////////////////
/// @brief aquires a lock
///
/// The constructor aquires a lock, the destructors releases the lock.
////////////////////////////////////////////////////////////////////////////////
SpinLocker (SpinLock*, char const* file, int line);
////////////////////////////////////////////////////////////////////////////////
/// @brief releases the lock
////////////////////////////////////////////////////////////////////////////////
~SpinLocker ();
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief the mutex
////////////////////////////////////////////////////////////////////////////////
SpinLock* _lock;
////////////////////////////////////////////////////////////////////////////////
/// @brief file
////////////////////////////////////////////////////////////////////////////////
char const* _file;
////////////////////////////////////////////////////////////////////////////////
/// @brief line number
////////////////////////////////////////////////////////////////////////////////
int _line;
};
}
}
#endif
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

View File

@ -30,9 +30,6 @@
#include "WriteLocker.h"
#include "Basics/Exceptions.h"
#include "Basics/StringUtils.h"
using namespace triagens::basics;
// -----------------------------------------------------------------------------
@ -46,8 +43,7 @@ using namespace triagens::basics;
////////////////////////////////////////////////////////////////////////////////
WriteLocker::WriteLocker (ReadWriteLock* readWriteLock)
: _readWriteLock(readWriteLock), _file(0), _line(0) {
_readWriteLock->writeLock();
: WriteLocker(readWriteLock, nullptr, 0) {
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -32,7 +32,6 @@
#define ARANGODB_BASICS_WRITE_LOCKER_H 1
#include "Basics/Common.h"
#include "Basics/ReadWriteLock.h"
// -----------------------------------------------------------------------------
@ -123,6 +122,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
int _line;
};
}
}

View File

@ -83,6 +83,8 @@ add_library(
Basics/ReadWriteLock.cpp
Basics/skip-list.cpp
Basics/socket-utils.cpp
Basics/SpinLock.cpp
Basics/SpinLocker.cpp
Basics/ssl-helper.cpp
Basics/string-buffer.cpp
Basics/StringUtils.cpp

View File

@ -53,6 +53,8 @@ lib_libarango_a_SOURCES = \
lib/Basics/ReadWriteLock.cpp \
lib/Basics/skip-list.cpp \
lib/Basics/socket-utils.cpp \
lib/Basics/SpinLock.cpp \
lib/Basics/SpinLocker.cpp \
lib/Basics/ssl-helper.cpp \
lib/Basics/string-buffer.cpp \
lib/Basics/StringUtils.cpp \