2025-09-18 23:03:39 -04:00
|
|
|
#ifndef LOCK_SET_H
|
|
|
|
|
#define LOCK_SET_H
|
2025-09-17 16:29:58 -04:00
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <stdexcept>
|
2025-09-20 18:16:46 -04:00
|
|
|
#include <utility>
|
|
|
|
|
#include <memory>
|
2025-09-30 02:59:14 -04:00
|
|
|
#include <qutex.h>
|
2025-09-20 18:16:46 -04:00
|
|
|
#include <lockerAndInvokerBase.h>
|
2025-09-17 16:29:58 -04:00
|
|
|
|
|
|
|
|
namespace smo {
|
|
|
|
|
|
2025-09-20 18:16:46 -04:00
|
|
|
// Forward declarations
|
|
|
|
|
template <class OriginalCbFnT>
|
|
|
|
|
class SerializedAsynchronousContinuation;
|
|
|
|
|
class Qutex;
|
|
|
|
|
|
2025-09-17 16:29:58 -04:00
|
|
|
/**
|
2025-09-18 22:21:06 -04:00
|
|
|
* @brief LockSet - Manages a collection of locks for acquisition/release
|
2025-09-17 16:29:58 -04:00
|
|
|
*/
|
2025-09-20 18:16:46 -04:00
|
|
|
template <class OriginalCbFnT>
|
2025-09-18 22:21:06 -04:00
|
|
|
class LockSet
|
2025-09-17 16:29:58 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2025-09-20 18:16:46 -04:00
|
|
|
/** EXPLANATION:
|
|
|
|
|
* Tracks both the Qutex that must be acquired, as well as the parent
|
|
|
|
|
* LockerAndInvoker that this LockSet has registered into that Qutex's
|
|
|
|
|
* queue.
|
2025-09-17 16:29:58 -04:00
|
|
|
*/
|
2025-09-20 18:16:46 -04:00
|
|
|
typedef std::pair<
|
|
|
|
|
std::reference_wrapper<Qutex>,
|
|
|
|
|
typename LockerAndInvokerBase::List::iterator> LockUsageDesc;
|
2025-09-17 16:29:58 -04:00
|
|
|
|
2025-09-20 18:16:46 -04:00
|
|
|
public:
|
2025-09-17 16:29:58 -04:00
|
|
|
/**
|
2025-09-20 18:16:46 -04:00
|
|
|
* @brief Constructor
|
|
|
|
|
* @param parentContinuation Reference to the parent
|
|
|
|
|
* SerializedAsynchronousContinuation
|
|
|
|
|
* @param qutexes Vector of Qutex references that must be acquired
|
2025-09-17 16:29:58 -04:00
|
|
|
*/
|
2025-09-20 18:16:46 -04:00
|
|
|
LockSet(
|
|
|
|
|
SerializedAsynchronousContinuation<OriginalCbFnT> &parentContinuation,
|
|
|
|
|
std::vector<std::reference_wrapper<Qutex>> qutexes = {})
|
|
|
|
|
: parentContinuation(parentContinuation), allLocksAcquired(false),
|
|
|
|
|
registeredInQutexQueues(false)
|
2025-09-17 16:29:58 -04:00
|
|
|
{
|
2025-09-20 18:16:46 -04:00
|
|
|
/* Convert Qutex references to LockUsageDesc (iterators will be filled
|
|
|
|
|
* in during registration)
|
|
|
|
|
*/
|
|
|
|
|
locks.reserve(qutexes.size());
|
|
|
|
|
for (auto& qutexRef : qutexes)
|
2025-09-17 16:29:58 -04:00
|
|
|
{
|
2025-09-20 18:16:46 -04:00
|
|
|
locks.emplace_back(
|
|
|
|
|
qutexRef,
|
|
|
|
|
typename LockerAndInvokerBase::List::iterator{});
|
2025-09-17 16:29:58 -04:00
|
|
|
}
|
2025-09-20 18:16:46 -04:00
|
|
|
}
|
2025-09-17 16:29:58 -04:00
|
|
|
|
2025-09-20 18:16:46 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Register the LockSet with all its Qutex locks
|
|
|
|
|
* @param lockvoker The LockerAndInvoker to register with each Qutex
|
|
|
|
|
*
|
|
|
|
|
* EXPLANATION:
|
|
|
|
|
* I'm not sure an unregisterFromQutexQueues() method is needed.
|
|
|
|
|
* Why? Because if an async sequence can't acquire all locks, it will
|
|
|
|
|
* simply never leave the qutexQ until it eventually does. The only other
|
|
|
|
|
* time it will leave the qutexQ is when the program terminates.
|
|
|
|
|
*
|
|
|
|
|
* I'm not sure we'll actually cancal all in-flight async sequences --
|
|
|
|
|
* and especially not all those that aren't even in any io_service queues.
|
|
|
|
|
* To whatever extent these objects get cleaned up, they'll probably be
|
|
|
|
|
* cleaned up in the qutexQ's std::list destructor -- and that won't
|
|
|
|
|
* execute any fancy cleanup logic. It'll just clear() out the list.
|
|
|
|
|
*/
|
|
|
|
|
void registerInQutexQueues(
|
2025-09-30 02:55:25 -04:00
|
|
|
const std::shared_ptr<LockerAndInvokerBase> &lockvoker);
|
2025-09-17 16:29:58 -04:00
|
|
|
|
2025-09-20 18:16:46 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Try to acquire all locks in order; back off if acquisition fails
|
|
|
|
|
* @param lockvoker The LockerAndInvoker attempting to acquire the locks
|
2025-09-21 15:11:28 -04:00
|
|
|
* @param firstFailedQutex Output parameter to receive the first Qutex that
|
|
|
|
|
* failed acquisition (can be nullptr)
|
2025-09-20 18:16:46 -04:00
|
|
|
* @return true if all locks were acquired, false otherwise
|
|
|
|
|
*/
|
2025-09-21 15:11:28 -04:00
|
|
|
bool tryAcquireOrBackOff(
|
2025-09-30 02:53:54 -04:00
|
|
|
LockerAndInvokerBase &lockvoker, Qutex **firstFailedQutex = nullptr);
|
2025-09-20 18:16:46 -04:00
|
|
|
void unregisterFromQutexQueues();
|
2025-09-17 16:29:58 -04:00
|
|
|
|
2025-09-30 02:17:46 -04:00
|
|
|
// @brief Release all locks
|
2025-09-30 02:59:14 -04:00
|
|
|
void release()
|
|
|
|
|
{
|
|
|
|
|
if (!registeredInQutexQueues)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
std::string(__func__) +
|
|
|
|
|
": LockSet::release() called but not registered in Qutex "
|
|
|
|
|
"queues");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!allLocksAcquired)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
std::string(__func__) +
|
|
|
|
|
": LockSet::release() called but allLocksAcquired is false");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& lockUsageDesc : locks) {
|
|
|
|
|
lockUsageDesc.first.get().release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
allLocksAcquired = false;
|
|
|
|
|
}
|
2025-09-20 18:16:46 -04:00
|
|
|
|
|
|
|
|
const LockUsageDesc &getLockUsageDesc(const Qutex &criterionLock) const
|
2025-09-17 16:29:58 -04:00
|
|
|
{
|
2025-09-20 18:16:46 -04:00
|
|
|
for (auto& lockUsageDesc : locks)
|
2025-09-17 16:29:58 -04:00
|
|
|
{
|
2025-09-20 18:16:46 -04:00
|
|
|
if (&lockUsageDesc.first.get() == &criterionLock) {
|
|
|
|
|
return lockUsageDesc;
|
|
|
|
|
}
|
2025-09-17 16:29:58 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 18:16:46 -04:00
|
|
|
// Should never happen if the LockSet is properly constructed
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
std::string(__func__) +
|
|
|
|
|
": Qutex not found in this LockSet");
|
2025-09-17 16:29:58 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 03:51:36 -04:00
|
|
|
public:
|
|
|
|
|
std::vector<LockUsageDesc> locks;
|
|
|
|
|
|
2025-09-17 16:29:58 -04:00
|
|
|
private:
|
2025-09-20 18:16:46 -04:00
|
|
|
SerializedAsynchronousContinuation<OriginalCbFnT> &parentContinuation;
|
2025-09-30 03:51:36 -04:00
|
|
|
bool allLocksAcquired, registeredInQutexQueues;
|
2025-09-17 16:29:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace smo
|
|
|
|
|
|
2025-09-18 23:03:39 -04:00
|
|
|
#endif // LOCK_SET_H
|