diff --git a/lib/Basics/MutexLocker.cpp b/lib/Basics/MutexLocker.cpp index fb723b845f..859d341b66 100644 --- a/lib/Basics/MutexLocker.cpp +++ b/lib/Basics/MutexLocker.cpp @@ -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) diff --git a/lib/Basics/MutexLocker.h b/lib/Basics/MutexLocker.h index 4eaa402de9..db913e242f 100644 --- a/lib/Basics/MutexLocker.h +++ b/lib/Basics/MutexLocker.h @@ -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; + }; } } diff --git a/lib/Basics/ReadLocker.cpp b/lib/Basics/ReadLocker.cpp index 2a137c0e59..49f924b833 100644 --- a/lib/Basics/ReadLocker.cpp +++ b/lib/Basics/ReadLocker.cpp @@ -43,8 +43,7 @@ using namespace triagens::basics; //////////////////////////////////////////////////////////////////////////////// ReadLocker::ReadLocker (ReadWriteLock* readWriteLock) - : _readWriteLock(readWriteLock), _file(0), _line(0) { - _readWriteLock->readLock(); + : ReadLocker(readWriteLock, nullptr, 0) { } //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/Basics/ReadLocker.h b/lib/Basics/ReadLocker.h index eecb18e108..0435ff0596 100644 --- a/lib/Basics/ReadLocker.h +++ b/lib/Basics/ReadLocker.h @@ -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; + }; } } diff --git a/lib/Basics/SpinLock.cpp b/lib/Basics/SpinLock.cpp new file mode 100644 index 0000000000..bfdf94040b --- /dev/null +++ b/lib/Basics/SpinLock.cpp @@ -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: diff --git a/lib/Basics/SpinLock.h b/lib/Basics/SpinLock.h new file mode 100644 index 0000000000..1f0f5d28a3 --- /dev/null +++ b/lib/Basics/SpinLock.h @@ -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: diff --git a/lib/Basics/SpinLocker.cpp b/lib/Basics/SpinLocker.cpp new file mode 100644 index 0000000000..6b6949b121 --- /dev/null +++ b/lib/Basics/SpinLocker.cpp @@ -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: diff --git a/lib/Basics/SpinLocker.h b/lib/Basics/SpinLocker.h new file mode 100644 index 0000000000..ea6c18cb0c --- /dev/null +++ b/lib/Basics/SpinLocker.h @@ -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: diff --git a/lib/Basics/WriteLocker.cpp b/lib/Basics/WriteLocker.cpp index d90345ae49..8943f0e121 100644 --- a/lib/Basics/WriteLocker.cpp +++ b/lib/Basics/WriteLocker.cpp @@ -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) { } //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/Basics/WriteLocker.h b/lib/Basics/WriteLocker.h index ab61e95592..1f1ceb3546 100644 --- a/lib/Basics/WriteLocker.h +++ b/lib/Basics/WriteLocker.h @@ -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; + }; } } diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 28b302544a..0cfab0fc9a 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -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 diff --git a/lib/Makefile.files b/lib/Makefile.files index 5527c71161..d1d4539fa6 100644 --- a/lib/Makefile.files +++ b/lib/Makefile.files @@ -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 \