From 1aa96dc0a112ffdec4fb62ac94625e56562b3cbe Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Thu, 26 Oct 2017 22:33:11 +0200 Subject: Add a RAII SpinLock (using boost's spinlock) --- libs/pbd/pbd/spinlock.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ libs/pbd/spinlock.cc | 37 +++++++++++++++++++++++++++++++++++++ libs/pbd/wscript | 1 + 3 files changed, 86 insertions(+) create mode 100644 libs/pbd/pbd/spinlock.h create mode 100644 libs/pbd/spinlock.cc (limited to 'libs') diff --git a/libs/pbd/pbd/spinlock.h b/libs/pbd/pbd/spinlock.h new file mode 100644 index 0000000000..eb784ac6ce --- /dev/null +++ b/libs/pbd/pbd/spinlock.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2017 Robin Gareus + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _pbd_spinlock_h_ +#define _pbd_spinlock_h_ + +#include +#include "pbd/libpbd_visibility.h" + +namespace PBD { + +/* struct boost::detail::spinlock { + * void lock(); + * bool try_lock(); + * void unlock(); + * }; + */ +typedef boost::detail::spinlock spinlock_t; + +/* RAII wrapper */ +class LIBPBD_API SpinLock { + +public: + SpinLock (spinlock_t&); + ~SpinLock (); + +private: + spinlock_t& _lock; +}; + +} /* namespace */ + +#endif /* _pbd_spinlock_h__ */ diff --git a/libs/pbd/spinlock.cc b/libs/pbd/spinlock.cc new file mode 100644 index 0000000000..5892c2dd9c --- /dev/null +++ b/libs/pbd/spinlock.cc @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2017 Robin Gareus + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifdef PLATFORM_WINDOWS +#include +#include +#endif + +#include "pbd/spinlock.h" + +using namespace PBD; + +SpinLock::SpinLock (spinlock_t& lock) + : _lock (lock) +{ + _lock.lock (); +} + +SpinLock::~SpinLock () +{ + _lock.unlock (); +} diff --git a/libs/pbd/wscript b/libs/pbd/wscript index 29d82edcb4..cf4beba9fb 100644 --- a/libs/pbd/wscript +++ b/libs/pbd/wscript @@ -70,6 +70,7 @@ libpbd_sources = [ 'semutils.cc', 'shortpath.cc', 'signals.cc', + 'spinlock.cc', 'stacktrace.cc', 'stateful_diff_command.cc', 'stateful.cc', -- cgit v1.2.3