1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright (c) 2020, 2021 by Apex.AI Inc. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
#include "iceoryx_posh/popo/trigger_handle.hpp"
#include "iceoryx_posh/internal/popo/building_blocks/condition_notifier.hpp"
namespace iox
{
namespace popo
{
/// explicitly implemented for MSVC and QNX
TriggerHandle::TriggerHandle() noexcept
{
}
TriggerHandle::TriggerHandle(ConditionVariableData& conditionVariableData,
const cxx::MethodCallback<void, uint64_t> resetCallback,
const uint64_t uniqueTriggerId) noexcept
: m_conditionVariableDataPtr(&conditionVariableData)
, m_resetCallback(resetCallback)
, m_uniqueTriggerId(uniqueTriggerId)
{
if (!m_resetCallback)
{
errorHandler(Error::kPOPO__TRIGGER_HANDLE_INVALID_RESET_CALLBACK, nullptr, ErrorLevel::FATAL);
}
}
TriggerHandle::TriggerHandle(TriggerHandle&& rhs) noexcept
{
*this = std::move(rhs);
}
TriggerHandle& TriggerHandle::operator=(TriggerHandle&& rhs) noexcept
{
if (this != &rhs)
{
std::lock(m_mutex, rhs.m_mutex);
std::lock_guard<std::recursive_mutex> lock(m_mutex, std::adopt_lock);
std::lock_guard<std::recursive_mutex> lockRhs(rhs.m_mutex, std::adopt_lock);
reset();
m_conditionVariableDataPtr = rhs.m_conditionVariableDataPtr;
m_resetCallback = std::move(rhs.m_resetCallback);
m_uniqueTriggerId = rhs.m_uniqueTriggerId;
rhs.invalidate();
}
return *this;
}
TriggerHandle::~TriggerHandle() noexcept
{
reset();
}
TriggerHandle::operator bool() const noexcept
{
return isValid();
}
bool TriggerHandle::isValid() const noexcept
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
return m_conditionVariableDataPtr != nullptr;
}
void TriggerHandle::trigger() noexcept
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (isValid())
{
ConditionNotifier(*m_conditionVariableDataPtr, m_uniqueTriggerId).notify();
}
}
bool TriggerHandle::wasTriggered() const noexcept
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (m_conditionVariableDataPtr != nullptr)
{
return m_conditionVariableDataPtr->m_activeNotifications[m_uniqueTriggerId].load(std::memory_order_relaxed);
}
return false;
}
void TriggerHandle::reset() noexcept
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (!isValid())
{
return;
}
// constructor ensured that resetCallback is valid
IOX_DISCARD_RESULT(m_resetCallback(m_uniqueTriggerId));
invalidate();
}
void TriggerHandle::invalidate() noexcept
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
m_conditionVariableDataPtr = nullptr;
m_resetCallback = cxx::MethodCallback<void, uint64_t>();
m_uniqueTriggerId = Trigger::INVALID_TRIGGER_ID;
}
ConditionVariableData* TriggerHandle::getConditionVariableData() noexcept
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
return m_conditionVariableDataPtr;
}
uint64_t TriggerHandle::getUniqueId() const noexcept
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
return m_uniqueTriggerId;
}
} // namespace popo
} // namespace iox
|