Files
MuditaOS/module-services/service-gui/SynchronizationMechanism.cpp
SP2FET 103af76459 [EGD-4836] Add firmware update test
Add test utility to update firmware via USB (service-desktop API)
Add timeout to harness port discovery

[EGD-5240] Add common timeout to python test harness
General purpose timeout based on signals library

[EGD-5239] Preserve 1Phone testing
In the long run test harness requires two phones,
but allow for one phone to still pass some test cases
2021-01-15 08:57:31 +01:00

60 lines
1.9 KiB
C++

// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "SynchronizationMechanism.hpp"
#include <stdexcept>
namespace service::gui
{
class FreeRtosSynchronization : public SynchronizationMechanism
{
public:
FreeRtosSynchronization();
FreeRtosSynchronization(const FreeRtosSynchronization &) = delete;
FreeRtosSynchronization(FreeRtosSynchronization &&) noexcept = default;
FreeRtosSynchronization &operator=(const FreeRtosSynchronization &) = delete;
FreeRtosSynchronization &operator=(FreeRtosSynchronization &&) noexcept = default;
~FreeRtosSynchronization() noexcept override;
void wait(cpp_freertos::Mutex &lock, std::chrono::milliseconds timeout, Predicate predicate) override;
void notify() override;
private:
SemaphoreHandle_t semaphore;
};
FreeRtosSynchronization::FreeRtosSynchronization() : semaphore{xSemaphoreCreateBinary()}
{}
FreeRtosSynchronization::~FreeRtosSynchronization() noexcept
{
vSemaphoreDelete(semaphore);
}
void FreeRtosSynchronization::wait(cpp_freertos::Mutex &lock,
std::chrono::milliseconds timeout,
Predicate predicate)
{
if (!predicate) {
// Predicate needed to cover spurious wake-ups.
throw std::invalid_argument{"Null predicate passed."};
}
while (!predicate()) {
lock.Unlock();
xSemaphoreTake(semaphore, timeout.count());
lock.Lock();
}
}
void FreeRtosSynchronization::notify()
{
xSemaphoreGive(semaphore);
}
std::unique_ptr<SynchronizationMechanism> getFreeRtosSynchronizationMechanism()
{
return std::make_unique<FreeRtosSynchronization>();
}
} // namespace service::gui