Cleanup and improvements (#194)
- Lots of changes for migrating C code to C++ - Improved `Lockable` in several ways like adding `withLock()` (+ tests) - Improved `Semaphore` a bit for improved readability, and also added some tests - Upgrade Linux machine in GitHub Actions so that we can compile with a newer GCC - Simplification of WiFi connection - Updated funding options - (and more)
This commit is contained in:
committed by
GitHub
parent
1bb1260ea0
commit
6c67845645
@@ -12,18 +12,41 @@ class ScopedLockableUsage;
|
||||
|
||||
/** Represents a lock/mutex */
|
||||
class Lockable {
|
||||
|
||||
public:
|
||||
|
||||
virtual ~Lockable() = default;
|
||||
|
||||
virtual bool lock(TickType_t timeoutTicks) const = 0;
|
||||
virtual bool lock(TickType_t timeout) const = 0;
|
||||
|
||||
virtual bool lock() const { return lock(portMAX_DELAY); }
|
||||
bool lock() const { return lock(portMAX_DELAY); }
|
||||
|
||||
virtual bool unlock() const = 0;
|
||||
|
||||
void withLock(TickType_t timeout, const std::function<void()>& onLockAcquired) const {
|
||||
if (lock(timeout)) {
|
||||
onLockAcquired();
|
||||
unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void withLock(TickType_t timeout, const std::function<void()>& onLockAcquired, const std::function<void()>& onLockFailure) const {
|
||||
if (lock(timeout)) {
|
||||
onLockAcquired();
|
||||
unlock();
|
||||
} else {
|
||||
onLockFailure();
|
||||
}
|
||||
}
|
||||
|
||||
void withLock(const std::function<void()>& onLockAcquired) const { withLock(portMAX_DELAY, onLockAcquired); }
|
||||
|
||||
void withLock(const std::function<void()>& onLockAcquired, const std::function<void()>& onLockFailed) const { withLock(portMAX_DELAY, onLockAcquired, onLockFailed); }
|
||||
|
||||
std::unique_ptr<ScopedLockableUsage> scoped() const;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Represents a lockable instance that is scoped to a specific lifecycle.
|
||||
* Once the ScopedLockableUsage is destroyed, unlock() is called automatically.
|
||||
@@ -37,6 +60,8 @@ class ScopedLockableUsage final : public Lockable {
|
||||
|
||||
public:
|
||||
|
||||
using Lockable::lock;
|
||||
|
||||
explicit ScopedLockableUsage(const Lockable& lockable) : lockable(lockable) {}
|
||||
|
||||
~ScopedLockableUsage() final {
|
||||
@@ -47,8 +72,6 @@ public:
|
||||
return lockable.lock(timeout);
|
||||
}
|
||||
|
||||
bool lock() const override { return lock(portMAX_DELAY); }
|
||||
|
||||
bool unlock() const override {
|
||||
return lockable.unlock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user