Skip to content

Commit

Permalink
Switch NodeLock to use our Spinlock
Browse files Browse the repository at this point in the history
This is mostly just to make it easier to understand whether we are waiting on
Charm++ or our own locking when profiling.
  • Loading branch information
nilsdeppe committed Apr 15, 2024
1 parent 11175df commit 622cabe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
32 changes: 12 additions & 20 deletions src/Parallel/NodeLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@

#include <converse.h>

#include "Parallel/Spinlock.hpp"
#include "Utilities/ErrorHandling/Error.hpp"
#include "Utilities/Gsl.hpp"

namespace Parallel {

NodeLock::NodeLock() : lock_(std::make_unique<CmiNodeLock>(CmiCreateLock())) {}
NodeLock::NodeLock()
: lock_(std::make_unique<typename decltype(lock_)::element_type>())

NodeLock::NodeLock(NodeLock&& moved_lock) : lock_(std::move(moved_lock.lock_)) {
{}

NodeLock::NodeLock(NodeLock&& moved_lock) noexcept
: lock_(std::move(moved_lock.lock_)) {
moved_lock.lock_ = nullptr;
}

NodeLock& NodeLock::operator=(NodeLock&& moved_lock) {
NodeLock& NodeLock::operator=(NodeLock&& moved_lock) noexcept {
lock_ = std::move(moved_lock.lock_);
moved_lock.lock_ = nullptr;
return *this;
Expand All @@ -28,40 +33,27 @@ void NodeLock::lock() {
if (UNLIKELY(nullptr == lock_)) {
ERROR("Trying to lock a destroyed lock");
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
CmiLock(*lock_);
#pragma GCC diagnostic pop
lock_->lock();
}

bool NodeLock::try_lock() {
if (UNLIKELY(nullptr == lock_)) {
ERROR("Trying to try_lock a destroyed lock");
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
return CmiTryLock(*lock_) == 0;
#pragma GCC diagnostic pop
return lock_->try_lock();
}

void NodeLock::unlock() {
if (UNLIKELY(nullptr == lock_)) {
ERROR("Trying to unlock a destroyed lock");
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
CmiUnlock(*lock_);
#pragma GCC diagnostic pop
lock_->unlock();
}

void NodeLock::destroy() {
if (nullptr == lock_) {
return;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
CmiDestroyLock(*lock_);
#pragma GCC diagnostic pop
lock_ = nullptr;
}

Expand All @@ -72,7 +64,7 @@ void NodeLock::pup(PUP::er& p) { // NOLINT
lock_ = nullptr;
} else {
if (p.isUnpacking()) {
lock_ = std::make_unique<CmiNodeLock>(CmiCreateLock());
lock_ = std::make_unique<typename decltype(lock_)::element_type>();
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/Parallel/NodeLock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <memory>
#include <pup.h>

#include "Parallel/Spinlock.hpp"

namespace Parallel {

/*!
Expand All @@ -30,8 +32,8 @@ class NodeLock {

NodeLock(const NodeLock&) = delete;
NodeLock& operator=(const NodeLock&) = delete;
NodeLock(NodeLock&& moved_lock);
NodeLock& operator=(NodeLock&& moved_lock);
NodeLock(NodeLock&& moved_lock) noexcept;
NodeLock& operator=(NodeLock&& moved_lock) noexcept;
~NodeLock();

void lock();
Expand All @@ -48,6 +50,6 @@ class NodeLock {
void pup(PUP::er& p);

private:
std::unique_ptr<CmiNodeLock> lock_;
std::unique_ptr<Spinlock> lock_;
};
} // namespace Parallel
3 changes: 2 additions & 1 deletion tools/FileTestDefs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ standard_checks+=(enable_if)
# Check for noexcept
noexcept() {
whitelist "$1" \
'src/Parallel/StaticSpscQueue.hpp' && \
'src/Parallel/StaticSpscQueue.hpp' \
"src/Parallel/NodeLock..pp$" && \
is_c++ "$1" && \
staged_grep -q 'noexcept ' "$1"
}
Expand Down

0 comments on commit 622cabe

Please sign in to comment.