feat: 全量同步 254 个常用的 Arduino 扩展库文件

This commit is contained in:
yczpf2019
2026-01-24 16:05:38 +08:00
parent c665ba662b
commit 397b9a23a3
6878 changed files with 2732224 additions and 1 deletions

View File

@@ -0,0 +1,61 @@
/*
BasicCoopTask.cpp - Implementation of cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "BasicCoopTask.h"
#if defined(ARDUINO) && !defined(ESP32_FREERTOS)
#include <alloca.h>
#endif
#if !defined(_MSC_VER) && !defined(ESP32_FREERTOS)
char* CoopTaskStackAllocator::allocateStack(size_t stackSize)
{
char* stackTop = nullptr;
if (stackSize <= CoopTaskBase::MAXSTACKSPACE - (CoopTaskBase::FULLFEATURES ? 2 : 1) * sizeof(CoopTaskBase::STACKCOOKIE))
{
#if defined(ESP8266)
stackTop = new (std::nothrow) char[stackSize + (CoopTaskBase::FULLFEATURES ? 2 : 1) * sizeof(CoopTaskBase::STACKCOOKIE)];
#else
stackTop = new char[stackSize + (CoopTaskBase::FULLFEATURES ? 2 : 1) * sizeof(CoopTaskBase::STACKCOOKIE)];
#endif
}
return stackTop;
}
#endif // !defined(_MSC_VER) && !defined(ESP32_FREERTOS)
#if (defined(ARDUINO) && !defined(ESP32_FREERTOS)) || defined(__GNUC__)
char* CoopTaskStackAllocatorFromLoopBase::allocateStack(size_t loopReserve, size_t stackSize)
{
char* bp = static_cast<char*>(alloca(
(sizeof(unsigned) >= 4) ? ((loopReserve + sizeof(unsigned) - 1) / sizeof(unsigned)) * sizeof(unsigned) : loopReserve
));
std::atomic_thread_fence(std::memory_order_release);
char* stackTop = nullptr;
if (stackSize <= CoopTaskBase::MAXSTACKSPACE - (CoopTaskBase::FULLFEATURES ? 2 : 1) * sizeof(CoopTaskBase::STACKCOOKIE))
{
stackTop = reinterpret_cast<char*>(
reinterpret_cast<long unsigned>(bp) - stackSize + (CoopTaskBase::FULLFEATURES ? 2 : 1) * sizeof(CoopTaskBase::STACKCOOKIE));
}
return stackTop;
}
#endif // (defined(ARDUINO) && !defined(ESP32_FREERTOS)) || defined(__GNUC__)

View File

@@ -0,0 +1,113 @@
/*
BasicCoopTask.h - Implementation of cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __BasicCoopTask_h
#define __BasicCoopTask_h
#include "CoopTaskBase.h"
class CoopTaskStackAllocator
{
public:
static constexpr size_t DEFAULTTASKSTACKSIZE = CoopTaskBase::DEFAULTTASKSTACKSIZE;
#if !defined(_MSC_VER) && !defined(ESP32_FREERTOS)
static char* allocateStack(size_t stackSize);
static void disposeStack(char* stackTop) { delete[] stackTop; }
#endif
};
template<size_t StackSize = CoopTaskBase::DEFAULTTASKSTACKSIZE>
class CoopTaskStackAllocatorAsMember
{
public:
static constexpr size_t DEFAULTTASKSTACKSIZE =
(sizeof(unsigned) >= 4) ? ((StackSize + sizeof(unsigned) - 1) / sizeof(unsigned)) * sizeof(unsigned) : StackSize;
#if !defined(_MSC_VER) && !defined(ESP32_FREERTOS)
protected:
char _stackTop[DEFAULTTASKSTACKSIZE + (CoopTaskBase::FULLFEATURES ? 2 : 1) * sizeof(CoopTaskBase::STACKCOOKIE)];
public:
char* allocateStack(size_t stackSize)
{
return (DEFAULTTASKSTACKSIZE >= stackSize) ?
_stackTop : nullptr;
}
static void disposeStack(char* stackTop) { }
#endif
};
class CoopTaskStackAllocatorFromLoopBase
{
public:
static constexpr size_t DEFAULTTASKSTACKSIZE = CoopTaskBase::DEFAULTTASKSTACKSIZE;
#if (defined(ARDUINO) && !defined(ESP32_FREERTOS)) || defined(__GNUC__)
protected:
static char* allocateStack(size_t loopReserve, size_t stackSize);
#endif
public:
static void disposeStack(char* stackTop) { }
};
template<size_t LoopReserve = (CoopTaskBase::DEFAULTTASKSTACKSIZE / 2)>
class CoopTaskStackAllocatorFromLoop : public CoopTaskStackAllocatorFromLoopBase
{
public:
static constexpr size_t DEFAULTTASKSTACKSIZE = CoopTaskBase::DEFAULTTASKSTACKSIZE;
#if (defined(ARDUINO) && !defined(ESP32_FREERTOS)) || defined(__GNUC__)
static char* allocateStack(size_t stackSize)
{
return CoopTaskStackAllocatorFromLoopBase::allocateStack(LoopReserve, stackSize);
}
#endif
};
template<class StackAllocator = CoopTaskStackAllocator> class BasicCoopTask : public CoopTaskBase
{
public:
#ifdef ARDUINO
BasicCoopTask(const String& name, taskfunction_t _func, size_t stackSize = StackAllocator::DEFAULTTASKSTACKSIZE) :
#else
BasicCoopTask(const std::string& name, taskfunction_t _func, size_t stackSize = StackAllocator::DEFAULTTASKSTACKSIZE) :
#endif
CoopTaskBase(name, _func, stackSize)
{
#if !defined(_MSC_VER) && !defined(ESP32_FREERTOS)
taskStackTop = stackAllocator.allocateStack(taskStackSize);
#endif
}
BasicCoopTask(const BasicCoopTask&) = delete;
BasicCoopTask& operator=(const BasicCoopTask&) = delete;
~BasicCoopTask()
{
#if !defined(_MSC_VER) && !defined(ESP32_FREERTOS)
stackAllocator.disposeStack(taskStackTop);
#endif
}
/// Every task is entered into this list by scheduleTask(). It is removed when it exits
/// or gets deleted.
static const std::array< std::atomic<BasicCoopTask* >, MAXNUMBERCOOPTASKS + 1>& getRunnableTasks()
{
// this is safe to do because CoopTaskBase ctor is protected.
return reinterpret_cast<const std::array< std::atomic<BasicCoopTask* >, MAXNUMBERCOOPTASKS + 1>&>(CoopTaskBase::getRunnableTasks());
}
protected:
StackAllocator stackAllocator;
};
#endif // __BasicCoopTask_h

View File

@@ -0,0 +1,93 @@
/*
CoopMutex.h - Implementation of a mutex and an RAII lock for cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __CoopMutex_h
#define __CoopMutex_h
#include "CoopSemaphore.h"
/// A mutex that is safe to use from CoopTasks.
class CoopMutex : private CoopSemaphore
{
protected:
std::atomic<CoopTaskBase*> owner;
public:
CoopMutex(size_t maxPending = 10) : CoopSemaphore(1, maxPending), owner(nullptr) {}
CoopMutex(const CoopMutex&) = delete;
CoopMutex& operator=(const CoopMutex&) = delete;
/// @returns: true, or false, if the current task does not own the mutex.
bool unlock()
{
if (CoopTaskBase::running() && CoopTaskBase::self() == owner.load() && post())
{
owner.store(nullptr);
return true;
}
return false;
}
/// @returns: true if the mutex becomes locked. false if it is already locked by the same task, or the maximum number of pending tasks is exceeded.
bool lock()
{
if (CoopTaskBase::running() && CoopTaskBase::self() != owner.load() && wait())
{
owner.store(CoopTaskBase::self());
return true;
}
return false;
}
/// @returns: true if the mutex becomes freshly locked without waiting, otherwise false.
bool try_lock()
{
if (CoopTaskBase::running() && CoopTaskBase::self() != owner.load() && try_wait())
{
owner.store(CoopTaskBase::self());
return true;
}
return false;
}
};
/// A RAII CoopMutex lock class.
class CoopMutexLock {
protected:
CoopMutex& mutex;
bool locked;
public:
/// The constructor returns if the mutex was locked, or locking failed.
explicit CoopMutexLock(CoopMutex& _mutex) : mutex(_mutex) {
locked = mutex.lock();
}
CoopMutexLock() = delete;
CoopMutexLock(const CoopMutexLock&) = delete;
CoopMutexLock& operator=(const CoopMutexLock&) = delete;
/// @returns: true if the mutex became locked, potentially after blocking, otherwise false.
operator bool() const {
return locked;
}
/// The destructor unlocks the mutex.
~CoopMutexLock() {
if (locked) mutex.unlock();
}
};
#endif // __CoopMutex_h

View File

@@ -0,0 +1,257 @@
/*
CoopSemaphore.cpp - Implementation of a semaphore for cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CoopSemaphore.h"
#if defined(ESP8266)
#include <interrupts.h>
using esp8266::InterruptLock;
#elif defined(ESP32) || !defined(ARDUINO)
using std::min;
#else
class InterruptLock {
public:
InterruptLock() {
noInterrupts();
}
~InterruptLock() {
interrupts();
}
};
#endif
#ifndef ARDUINO
#include <chrono>
namespace
{
uint32_t millis()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
}
#endif
bool CoopSemaphore::_wait(const bool withDeadline, const uint32_t ms)
{
const uint32_t start = withDeadline ? millis() : 0;
uint32_t expired = 0;
bool selfFirst = false;
for (;;)
{
auto self = CoopTaskBase::self();
unsigned val;
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
val = value.load();
if (val)
{
value.store(val - 1);
}
}
#else
val = 1;
while (val && !value.compare_exchange_weak(val, val - 1)) {}
#endif
const unsigned valOnEntry = val;
if (withDeadline) expired = millis() - start;
if (!(selfFirst && valOnEntry))
{
if (pendingTasks.push(self))
{
if (!withDeadline) self->sleep(true);
}
else
{
selfFirst = true;
}
}
bool fwd = !selfFirst && val;
bool stop = false;
CoopTaskBase* pendingTask = nullptr;
bool selfSuccess = false;
for (;;)
{
if (pendingTasks.available())
{
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
pendingTask = pendingTask0.load();
if (fwd || !pendingTask) pendingTask0.store(pendingTasks.pop());
}
#else
pendingTask = nullptr;
bool exchd = false;
while ((fwd || !pendingTask) && !(exchd = pendingTask0.compare_exchange_weak(pendingTask, pendingTasks.peek()))) {}
if (exchd) pendingTasks.pop();
#endif
}
else
{
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
pendingTask = pendingTask0.load();
if (fwd && pendingTask) pendingTask0.store(nullptr);
}
#else
pendingTask = nullptr;
if (fwd) pendingTask = pendingTask0.exchange(nullptr);
#endif
stop = true;
}
if (!val)
{
break;
}
if (!(pendingTask || stop))
{
continue;
}
if (selfFirst)
{
selfFirst = false;
if (!withDeadline) self->sleep(false);
selfSuccess = true;
}
else if (pendingTask == self)
{
if (!selfSuccess)
{
if (!withDeadline) self->sleep(false);
return true;
}
if (!stop) continue;
}
else if (pendingTask)
{
pendingTask->scheduleTask(true);
}
if (stop)
{
break;
}
val -= 1;
fwd = val;
}
if (selfSuccess)
{
return true;
}
if (valOnEntry)
{
#if !defined(ESP32) && defined(ARDUINO)
InterruptLock lock;
val = value.load();
value.store(val + 1);
#else
while (!value.compare_exchange_weak(val, val + 1)) {}
#endif
}
if (withDeadline)
{
if (expired >= ms)
{
pendingTasks.for_each_rev_requeue(notIsSelfTask);
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
pendingTask = pendingTask0.load();
if (pendingTask == self) pendingTask0.store(pendingTasks.available() ? pendingTasks.pop() : nullptr);
}
#else
bool exchd = false;
pendingTask = self;
while ((pendingTask == self) && !(exchd = pendingTask0.compare_exchange_weak(pendingTask, pendingTasks.available() ? pendingTasks.peek() : nullptr))) {}
if (exchd && pendingTasks.available()) pendingTasks.pop();
#endif
return false;
}
CoopTaskBase::delay(ms - expired);
}
else
{
CoopTaskBase::yield();
}
selfFirst = true;
}
}
bool IRAM_ATTR CoopSemaphore::post()
{
CoopTaskBase* pendingTask;
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
unsigned val = value.load();
value.store(val + 1);
pendingTask = pendingTask0.load();
if (pendingTask) pendingTask0.store(nullptr);
}
#else
unsigned val = 0;
while (!value.compare_exchange_weak(val, val + 1)) {}
pendingTask = pendingTask0.exchange(nullptr);
#endif
if (!pendingTask || !pendingTask->suspended()) return true;
return pendingTask->scheduleTask(true);
}
bool CoopSemaphore::setval(unsigned newVal)
{
CoopTaskBase* pendingTask = nullptr;
unsigned val;
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
val = value.load();
value.store(newVal);
if (newVal > val)
{
pendingTask = pendingTask0.load();
pendingTask0.store(nullptr);
}
}
#else
val = value.exchange(newVal);
if (newVal > val) pendingTask = pendingTask0.exchange(nullptr);
#endif
if (!pendingTask || !pendingTask->suspended()) return true;
return pendingTask->scheduleTask(true);
}
bool CoopSemaphore::try_wait()
{
unsigned val;
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
val = value.load();
if (val)
{
value.store(val - 1);
}
}
#else
val = 1;
while (val && !value.compare_exchange_weak(val, val - 1)) {}
#endif
return val;
}

View File

@@ -0,0 +1,93 @@
/*
CoopSemaphore.h - Implementation of a semaphore for cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __CoopSemaphore_h
#define __CoopSemaphore_h
#include "CoopTaskBase.h"
#include "circular_queue/circular_queue.h"
/// A semaphore that is safe to use from CoopTasks.
/// Only post() is safe to use from interrupt service routines,
/// or concurrent OS threads that must synchronized with the singled thread running CoopTasks.
class CoopSemaphore
{
protected:
std::atomic<unsigned> value;
std::atomic<CoopTaskBase*> pendingTask0;
circular_queue<CoopTaskBase*> pendingTasks;
// capture-less functions for iterators.
static void awakeAndSchedule(CoopTaskBase*&& task)
{
task->scheduleTask(true);
}
static bool notIsSelfTask(CoopTaskBase*& task)
{
return CoopTaskBase::self() != task;
}
/// @param withDeadline true: the ms parameter specifies the relative timeout for a successful
/// aquisition of the semaphore.
/// false: there is no deadline, the ms parameter is disregarded.
/// @param ms the relative timeout measured in milliseconds.
/// @returns: true if it sucessfully acquired the semaphore, either immediately or after sleeping.
/// false if the deadline expired, or the maximum number of pending tasks is exceeded.
bool _wait(const bool withDeadline = false, const uint32_t ms = 0);
public:
/// @param val the initial value of the semaphore.
/// @param maxPending the maximum supported number of concurrently waiting tasks.
CoopSemaphore(unsigned val, size_t maxPending = 10) : value(val), pendingTask0(nullptr), pendingTasks(maxPending) {}
CoopSemaphore(const CoopSemaphore&) = delete;
CoopSemaphore& operator=(const CoopSemaphore&) = delete;
~CoopSemaphore()
{
// wake up all queued tasks
pendingTasks.for_each(awakeAndSchedule);
}
/// post() is the only operation that is allowed from an interrupt service routine,
/// or a concurrent OS thread that is synchronized with the singled thread running CoopTasks.
bool IRAM_ATTR post();
/// @param newVal: the semaphore is immediately set to the specified value. if newVal is greater
/// than the current semaphore value, the behavior is identical to as many post operations.
bool setval(unsigned newVal);
/// @returns: true if it sucessfully acquired the semaphore, either immediately or after sleeping.
/// false if the maximum number of pending tasks is exceeded.
bool wait()
{
return _wait();
}
/// @param ms the relative timeout, measured in milliseconds, for a successful aquisition of the semaphore.
/// @returns: true if it sucessfully acquired the semaphore, either immediately or after sleeping.
/// false if the deadline expired, or the maximum number of pending tasks is exceeded.
bool wait(uint32_t ms)
{
return _wait(true, ms);
}
/// @returns: true if the semaphore was acquired immediately, otherwise false.
bool try_wait();
};
#endif // __CoopSemaphore_h

View File

@@ -0,0 +1,129 @@
/*
CoopTask.h - Implementation of cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __CoopTask_h
#define __CoopTask_h
#include "BasicCoopTask.h"
template<typename Result = int, class StackAllocator = CoopTaskStackAllocator> class CoopTask : public BasicCoopTask<StackAllocator>
{
public:
using taskfunction_t = Delegate< Result() >;
#if defined(ARDUINO)
CoopTask(const String& name, CoopTask::taskfunction_t _func, size_t stackSize = BasicCoopTask<StackAllocator>::DEFAULTTASKSTACKSIZE) :
#else
CoopTask(const std::string& name, CoopTask::taskfunction_t _func, size_t stackSize = BasicCoopTask<StackAllocator>::DEFAULTTASKSTACKSIZE) :
#endif
// Wrap _func into _exit() to capture return value as exit code
BasicCoopTask<StackAllocator>(name, captureFuncReturn, stackSize), func(_func)
{
}
protected:
Result _exitCode = {};
static void captureFuncReturn() noexcept
{
#if !defined(ARDUINO)
try {
#endif
self()->_exitCode = self()->func();
#if !defined(ARDUINO)
}
catch (const Result code)
{
self()->_exitCode = code;
}
catch (...)
{
}
#endif
}
void _exit(Result&& code = Result{}) noexcept
{
_exitCode = std::move(code);
BasicCoopTask<StackAllocator>::_exit();
}
void _exit(const Result& code) noexcept
{
_exitCode = code;
BasicCoopTask<StackAllocator>::_exit();
}
private:
taskfunction_t func;
public:
/// @returns: The exit code is either the return value of of the task function, or set by using the exit() function.
Result exitCode() const noexcept { return _exitCode; }
/// @returns: a pointer to the CoopTask instance that is running. nullptr if not called from a CoopTask function (running() == false).
static CoopTask* self() noexcept { return static_cast<CoopTask*>(BasicCoopTask<StackAllocator>::self()); }
/// Use only in running CoopTask function. As stack unwinding is corrupted
/// by exit(), which among other issues breaks the RAII idiom,
/// using regular return or exceptions is to be preferred in most cases.
/// @param code default exit code is default value of CoopTask<>'s template argument, use exit() to set a different value.
static void exit(Result&& code = Result{}) noexcept { self()->_exit(std::move(code)); }
/// Use only in running CoopTask function. As stack unwinding is corrupted
/// by exit(), which among other issues breaks the RAII idiom,
/// using regular return or exceptions is to be preferred in most cases.
/// @param code default exit code is default value of CoopTask<>'s template argument, use exit() to set a different value.
static void exit(const Result& code) noexcept { self()->_exit(code); }
};
template<class StackAllocator> class CoopTask<void, StackAllocator> : public BasicCoopTask<StackAllocator>
{
public:
using CoopTaskBase::taskfunction_t;
#if defined(ARDUINO)
CoopTask(const String& name, CoopTaskBase::taskfunction_t func, size_t stackSize = BasicCoopTask<StackAllocator>::DEFAULTTASKSTACKSIZE) :
#else
CoopTask(const std::string& name, CoopTaskBase::taskfunction_t func, size_t stackSize = BasicCoopTask<StackAllocator>::DEFAULTTASKSTACKSIZE) :
#endif
BasicCoopTask<StackAllocator>(name, func, stackSize)
{
}
/// @returns: a pointer to the CoopTask instance that is running. nullptr if not called from a CoopTask function (running() == false).
static CoopTask* self() noexcept { return static_cast<CoopTask*>(BasicCoopTask<StackAllocator>::self()); }
};
/// A convenience function that creates a new CoopTask instance for the supplied task function, with the
/// given name and stack size, and schedules it.
/// @returns: the pointer to the new CoopTask instance, or nullptr if the creation or preparing for scheduling failed.
template<typename Result = int, class StackAllocator = CoopTaskStackAllocator>
CoopTask<Result, StackAllocator>* createCoopTask(
#if defined(ARDUINO)
const String& name, typename CoopTask<Result, StackAllocator>::taskfunction_t func, size_t stackSize = CoopTaskBase::DEFAULTTASKSTACKSIZE)
#else
const std::string& name, typename CoopTask<Result, StackAllocator>::taskfunction_t func, size_t stackSize = CoopTaskBase::DEFAULTTASKSTACKSIZE)
#endif
{
auto task = new CoopTask<Result, StackAllocator>(name, func, stackSize);
if (task && task->scheduleTask()) return task;
delete task;
return nullptr;
}
#endif // __CoopTask_h

View File

@@ -0,0 +1,965 @@
/*
CoopTaskBase.cpp - Implementation of cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CoopTaskBase.h"
#ifdef ARDUINO
#include <alloca.h>
#else
#include <chrono>
#endif
#if defined(ESP8266)
#include <Schedule.h>
#include <interrupts.h>
using esp8266::InterruptLock;
#elif !defined(ESP32) && defined(ARDUINO)
class InterruptLock {
public:
InterruptLock() {
noInterrupts();
}
~InterruptLock() {
interrupts();
}
};
#endif
extern "C" {
// Integration into global yield() and delay()
#if defined(ESP8266) || defined(ESP32)
void __yield();
void yield()
{
auto self = CoopTaskBase::self();
if (self) CoopTaskBase::yield(self);
else __yield();
}
#elif defined(ARDUINO)
void yield()
{
auto self = CoopTaskBase::self();
if (self) CoopTaskBase::yield(self);
}
#endif
#if defined(ESP8266)
void __delay(unsigned long ms);
void delay(unsigned long ms)
{
auto self = CoopTaskBase::self();
if (self) CoopTaskBase::delay(self, ms);
else __delay(ms);
}
#if defined(HAVE_ESP_SUSPEND)
void __esp_suspend();
// disable CONT suspend, resume by esp_schedule pattern
void esp_suspend()
{
auto self = CoopTaskBase::self();
if (self) CoopTaskBase::yield(self);
else __esp_suspend();
}
#else
void __esp_yield();
// disable CONT suspend, resume by esp_schedule pattern
void esp_yield()
{
auto self = CoopTaskBase::self();
if (self) CoopTaskBase::yield(self);
else __esp_yield();
}
#endif
void __esp_delay(unsigned long ms);
// disable CONT suspend, resume by esp_schedule pattern
void esp_delay(unsigned long ms)
{
auto self = CoopTaskBase::self();
if (self) CoopTaskBase::yield(self);
else __esp_delay(ms);
}
#elif defined(ESP32) && !defined(ESP32_FREERTOS)
void __delay(uint32_t ms);
void delay(uint32_t ms)
{
auto self = CoopTaskBase::self();
if (self) CoopTaskBase::delay(self, ms);
else __delay(ms);
}
#endif // ESP32_FREERTOS
}
std::array< std::atomic<CoopTaskBase* >, CoopTaskBase::MAXNUMBERCOOPTASKS + 1> CoopTaskBase::runnableTasks {};
std::atomic<size_t> CoopTaskBase::runnableTasksCount(0);
CoopTaskBase* CoopTaskBase::current = nullptr;
#ifndef ARDUINO
namespace
{
uint32_t millis()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
uint32_t micros()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
void delayMicroseconds(uint32_t us)
{
const uint32_t start = micros();
while (micros() - start < us) {}
}
}
#elif defined(ESP8266) || defined(ESP32)
namespace
{
static const uint32_t CYCLES_PER_MS = ESP.getCpuFreqMHz() * 1000;
}
#endif
#if defined(ESP8266)
bool CoopTaskBase::usingBuiltinScheduler = false;
bool CoopTaskBase::rescheduleTask(uint32_t repeat_us)
{
auto stat = run();
if (sleeping()) return false;
switch (stat)
{
case -1: // exited.
return false;
break;
case 0: // runnable.
// rather keep scheduling at wrong delayed interval than drop altogether
if (repeat_us)
{
return !schedule_recurrent_function_us([this]() { return rescheduleTask(0); }, 0);
}
break;
default: // delayed for stat milliseconds or microseconds, check delayIsMs().
uint32_t next_repeat_us = delayIsMs() ? stat * 1000 : stat;
if (next_repeat_us > 26000000) next_repeat_us = 26000000;
if (next_repeat_us == repeat_us) break;
// rather keep scheduling at wrong interval than drop altogether
return !schedule_recurrent_function_us([this, next_repeat_us]() { return rescheduleTask(next_repeat_us); }, next_repeat_us, [this]() { return !delayed(); });
break;
}
return true;
}
#endif
bool IRAM_ATTR CoopTaskBase::enrollRunnable()
{
bool enrolled = false;
bool inserted = false;
for (size_t i = 0; i < runnableTasks.size(); ++i)
{
#if !defined(ESP32) && defined(ARDUINO)
InterruptLock lock;
auto task = runnableTasks[i].load();
if (!enrolled && nullptr == task)
{
runnableTasks[i].store(this);
enrolled = true;
inserted = true;
}
else if (this == task)
{
if (enrolled)
{
runnableTasks[i].store(nullptr);
inserted = false;
}
else
{
enrolled = true;
}
break;
}
}
if (inserted) runnableTasksCount.store(runnableTasksCount.load() + 1);
#else
CoopTaskBase* cmpTo = nullptr;
if (!enrolled && runnableTasks[i].compare_exchange_strong(cmpTo, this))
{
enrolled = true;
inserted = true;
}
else if (enrolled)
{
cmpTo = this;
if (runnableTasks[i].compare_exchange_strong(cmpTo, nullptr))
{
inserted = false;
break;
}
}
else if (this == runnableTasks[i].load())
{
enrolled = true;
break;
}
}
if (inserted) ++runnableTasksCount;
#endif
return enrolled;
}
void CoopTaskBase::delistRunnable()
{
#if !defined(ESP32) && defined(ARDUINO)
InterruptLock lock;
for (size_t i = 0; i < runnableTasks.size(); ++i)
{
if (runnableTasks[i].load() == this)
{
runnableTasks[i].store(nullptr);
runnableTasksCount.store(runnableTasksCount.load() - 1);
break;
}
}
#else
for (size_t i = 0; i < runnableTasks.size(); ++i)
{
CoopTaskBase* self = this;
if (runnableTasks[i].compare_exchange_strong(self, nullptr))
{
--runnableTasksCount;
break;
}
}
#endif
}
bool IRAM_ATTR CoopTaskBase::scheduleTask(bool wakeup)
{
if (!*this || !enrollRunnable()) return false;
#if defined(ESP8266)
bool reschedule = usingBuiltinScheduler && sleeping();
#endif
if (wakeup)
{
sleep(false);
}
#if defined(ESP8266)
return !reschedule || schedule_function([this]() { rescheduleTask(1); });
#else
return true;
#endif
}
#if defined(_MSC_VER)
CoopTaskBase::~CoopTaskBase()
{
if (taskFiber) DeleteFiber(taskFiber);
delistRunnable();
}
LPVOID CoopTaskBase::primaryFiber = nullptr;
void __stdcall CoopTaskBase::taskFiberFunc(void* self)
{
static_cast<CoopTaskBase*>(self)->func();
static_cast<CoopTaskBase*>(self)->_exit();
}
int32_t CoopTaskBase::initialize()
{
if (!cont || init) return -1;
init = true;
if (*this)
{
if (!primaryFiber) primaryFiber = ConvertThreadToFiber(nullptr);
if (primaryFiber)
{
taskFiber = CreateFiber(taskStackSize, taskFiberFunc, this);
if (taskFiber) return 0;
}
}
cont = false;
delistRunnable();
return -1;
}
int32_t CoopTaskBase::run()
{
if (!cont) return -1;
if (sleeps.load()) return 0;
if (delays.load())
{
if (delay_ms)
{
auto expired = millis() - delay_start;
if (expired < delay_duration)
{
auto delay_rem = delay_duration - expired;
return static_cast<int32_t>(delay_rem) < 0 ? DELAY_MAXINT : delay_rem;
}
}
else
{
auto expired = micros() - delay_start;
if (expired < delay_duration)
{
auto delay_rem = delay_duration - expired;
if (delay_rem >= DELAYMICROS_THRESHOLD)
{
return static_cast<int32_t>(delay_rem) < 0 ? DELAY_MAXINT : delay_rem;
}
::delayMicroseconds(delay_rem);
}
}
delays.store(false);
delay_duration = 0;
}
current = this;
if (!init && initialize() < 0) return -1;
SwitchToFiber(taskFiber);
current = nullptr;
// val = 0: init; -1: exit() task; 1: yield task; 2: sleep task; 3: delay task for delay_duration
cont = cont && (val > 0);
sleeps.store(sleeps.load() || (val == 2));
delays.store(delays.load() || (val > 2));
if (!cont) {
DeleteFiber(taskFiber);
taskFiber = NULL;
delistRunnable();
return -1;
}
switch (val)
{
case 1:
case 2:
return 0;
break;
case 3:
default:
return static_cast<int32_t>(delay_duration) < 0 ? DELAY_MAXINT : delay_duration;
break;
}
}
size_t CoopTaskBase::getFreeStack() const
{
return taskFiber ? taskStackSize : 0;
}
void CoopTaskBase::doYield(unsigned val) noexcept
{
self()->val = val;
SwitchToFiber(primaryFiber);
}
void CoopTaskBase::_delay(uint32_t ms) noexcept
{
delay_ms = true;
delay_start = millis();
delay_duration = ms;
// CoopTask::run() defers task for delay_duration milliseconds.
doYield(3);
}
void CoopTaskBase::_delayMicroseconds(uint32_t us) noexcept
{
if (us < DELAYMICROS_THRESHOLD) {
::delayMicroseconds(us);
return;
}
delay_ms = false;
delay_start = micros();
delay_duration = us;
// CoopTask::run() defers task for delay_duration microseconds.
doYield(3);
}
void CoopTaskBase::_exit() noexcept
{
self()->val = -1;
SwitchToFiber(primaryFiber);
}
void CoopTaskBase::_yield() noexcept
{
doYield(1);
}
void CoopTaskBase::_sleep() noexcept
{
doYield(2);
}
void IRAM_ATTR CoopTaskBase::sleep(const bool state) noexcept
{
sleeps.store(state);
if (!state)
{
delays.store(false);
delay_duration = 0;
}
}
#elif defined(ESP32_FREERTOS)
CoopTaskBase::~CoopTaskBase()
{
if (taskHandle) vTaskDelete(taskHandle);
taskHandle = nullptr;
delistRunnable();
}
void CoopTaskBase::taskFunc(void* _self)
{
static_cast<CoopTaskBase*>(_self)->func();
static_cast<CoopTaskBase*>(_self)->_exit();
}
int32_t CoopTaskBase::initialize()
{
if (!cont || init) return -1;
init = true;
if (*this)
{
xTaskCreateUniversal(taskFunc, name().c_str(), taskStackSize, this, tskIDLE_PRIORITY, &taskHandle, CONFIG_ARDUINO_RUNNING_CORE);
if (taskHandle)
{
vTaskSuspend(taskHandle);
return 0;
}
}
cont = false;
delistRunnable();
return -1;
}
int32_t CoopTaskBase::run()
{
if (!cont) return -1;
if (sleeps.load()) return 0;
if (delays.load())
{
if (delay_ms)
{
if (0 == delay_duration && eSuspended != eTaskGetState(taskHandle))
{
// fall through, blocked during FreeRTOS delay or asynchronously ready after is specifically handled below
}
else
{
auto expired = (ESP.getCycleCount() - delay_start) / CYCLES_PER_MS;
while (expired && delay_duration)
{
delay_start += CYCLES_PER_MS;
--delay_duration;
--expired;
}
if (expired < delay_duration)
{
auto delay_rem = delay_duration - expired;
return static_cast<int32_t>(delay_rem) < 0 ? DELAY_MAXINT : delay_rem;
}
delays.store(false);
delay_duration = 0;
}
}
else
{
auto expired = micros() - delay_start;
if (expired < delay_duration)
{
auto delay_rem = delay_duration - expired;
if (delay_rem >= DELAYMICROS_THRESHOLD)
{
return static_cast<int32_t>(delay_rem) < 0 ? DELAY_MAXINT : delay_rem;
}
::delayMicroseconds(delay_rem);
}
delays.store(false);
delay_duration = 0;
}
}
current = this;
if (!init)
{
if (initialize() < 0)
{
current = nullptr;
return -1;
}
}
bool resume = true;
for (;;)
{
auto taskState = eTaskGetState(taskHandle);
if (eSuspended == taskState)
{
if (!resume)
{
vTaskPrioritySet(taskHandle, tskIDLE_PRIORITY);
break;
}
resume = false;
vTaskPrioritySet(taskHandle, 1);
vTaskResume(taskHandle);
continue;
}
else if (eReady == taskState)
{
if (resume)
{
resume = false;
sleep(false);
delay_duration = 0;
vTaskPrioritySet(taskHandle, 1);
continue;
}
vPortYield();
continue;
}
else if (eBlocked == taskState)
{
if (resume)
{
if (!delays.load())
{
vTaskSuspend(taskHandle);
continue;
}
break;
}
vTaskPrioritySet(taskHandle, tskIDLE_PRIORITY);
if (!delays.exchange(true))
{
delay_ms = true;
delay_start = ESP.getCycleCount();
delay_duration = 0;
}
break;
}
else if (eDeleted == taskState)
{
cont = false;
break;
}
}
current = nullptr;
if (!cont) {
vTaskDelete(taskHandle);
taskHandle = nullptr;
delistRunnable();
return -1;
}
return static_cast<int32_t>(delay_duration) < 0 ? DELAY_MAXINT : delay_duration;
}
size_t CoopTaskBase::getFreeStack() const
{
return taskHandle ? uxTaskGetStackHighWaterMark(taskHandle) : 0;
}
void CoopTaskBase::_delay(uint32_t ms) noexcept
{
delays.store(true);
delay_ms = true;
delay_start = ESP.getCycleCount();
delay_duration = ms;
vTaskSuspend(taskHandle);
}
void CoopTaskBase::_delayMicroseconds(uint32_t us) noexcept
{
if (us < DELAYMICROS_THRESHOLD) {
::delayMicroseconds(us);
return;
}
delays.store(true);
delay_ms = false;
delay_start = micros();
delay_duration = us;
vTaskSuspend(taskHandle);
}
void CoopTaskBase::_sleep() noexcept
{
sleeps.store(true);
vTaskSuspend(taskHandle);
}
void CoopTaskBase::_yield() noexcept
{
delay_duration = 0;
delays.store(false);
vTaskSuspend(taskHandle);
}
void CoopTaskBase::_exit() noexcept
{
cont = false;
vTaskSuspend(taskHandle);
}
void IRAM_ATTR CoopTaskBase::sleep(const bool state) noexcept
{
sleeps.store(state);
if (!state)
{
delay_duration = 0;
delays.store(false);
}
}
CoopTaskBase* CoopTaskBase::self() noexcept
{
const auto currentTaskHandle = xTaskGetCurrentTaskHandle();
auto cur = current;
if (cur && currentTaskHandle == cur->taskHandle) return cur;
for (size_t i = 0; i < runnableTasks.size(); ++i)
{
cur = runnableTasks[i].load();
if (cur && currentTaskHandle == cur->taskHandle) return cur;
}
return nullptr;
}
#else
jmp_buf CoopTaskBase::env;
CoopTaskBase::~CoopTaskBase()
{
delistRunnable();
}
int32_t CoopTaskBase::initialize()
{
if (!cont || init) return -1;
init = true;
// fill stack with magic values to check overflow, corruption, and high water mark
for (size_t pos = 0; pos <= (taskStackSize + (FULLFEATURES ? sizeof(STACKCOOKIE) : 0)) / sizeof(STACKCOOKIE); ++pos)
{
reinterpret_cast<unsigned*>(taskStackTop)[pos] = STACKCOOKIE;
}
#if defined(__GNUC__) && (defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64))
asm volatile (
"movq %0, %%rsp"
:
: "r" (((reinterpret_cast<long unsigned>(taskStackTop) + taskStackSize + (FULLFEATURES ? sizeof(STACKCOOKIE) : 0)) >> 4) << 4)
);
#elif defined(ARDUINO) || defined(__GNUC__)
char* bp = static_cast<char*>(alloca(
reinterpret_cast<long unsigned>(&bp) - reinterpret_cast<long unsigned>(taskStackTop) - (taskStackSize + (FULLFEATURES ? sizeof(STACKCOOKIE) : 0))
));
std::atomic_thread_fence(std::memory_order_release);
#else
#error Setting stack pointer is not implemented on this target
#endif
func();
self()->_exit();
cont = false;
delistRunnable();
return -1;
}
int32_t CoopTaskBase::run()
{
if (!cont) return -1;
if (sleeps.load()) return 0;
if (delays.load())
{
if (delay_ms)
{
#if defined(ESP8266) || defined(ESP32)
uint32_t expired;
#ifdef ESP8266
if (usingBuiltinScheduler)
{
expired = millis() - delay_start;
}
else
#endif
{
expired = (ESP.getCycleCount() - delay_start) / CYCLES_PER_MS;
while (expired && delay_duration)
{
delay_start += CYCLES_PER_MS;
--delay_duration;
--expired;
}
}
#else
auto expired = millis() - delay_start;
#endif
if (expired < delay_duration)
{
auto delay_rem = delay_duration - expired;
return static_cast<int32_t>(delay_rem) < 0 ? DELAY_MAXINT : delay_rem;
}
}
else
{
auto expired = micros() - delay_start;
if (expired < delay_duration)
{
auto delay_rem = delay_duration - expired;
if (delay_rem >= DELAYMICROS_THRESHOLD)
{
return static_cast<int32_t>(delay_rem) < 0 ? DELAY_MAXINT : delay_rem;
}
::delayMicroseconds(delay_rem);
}
}
delays.store(false);
delay_duration = 0;
}
auto val = setjmp(env);
// val = 0: init; -1: exit() task; 1: yield task; 2: sleep task; 3: delay task for delay_duration
if (!val) {
current = this;
if (!init) return initialize();
if (FULLFEATURES && *reinterpret_cast<unsigned*>(taskStackTop + taskStackSize + sizeof(STACKCOOKIE)) != STACKCOOKIE)
{
#ifndef ARDUINO_attiny
::printf(PSTR("FATAL ERROR: CoopTask %s stack corrupted\n"), name().c_str());
#endif
::abort();
}
longjmp(env_yield, 1);
}
else
{
current = nullptr;
if (*reinterpret_cast<unsigned*>(taskStackTop) != STACKCOOKIE)
{
#ifndef ARDUINO_attiny
::printf(PSTR("FATAL ERROR: CoopTask %s stack overflow\n"), name().c_str());
#endif
::abort();
}
cont = cont && (val > 0);
sleeps.store(sleeps.load() || (val == 2));
delays.store(delays.load() || (val > 2));
}
if (!cont) {
delistRunnable();
return -1;
}
switch (val)
{
case 1:
case 2:
return 0;
break;
case 3:
default:
return static_cast<int32_t>(delay_duration) < 0 ? DELAY_MAXINT : delay_duration;
break;
}
}
void CoopTaskBase::dumpStack() const
{
if (!taskStackTop) return;
size_t pos;
for (pos = 1; pos < (taskStackSize + (FULLFEATURES ? sizeof(STACKCOOKIE) : 0)) / sizeof(STACKCOOKIE); ++pos)
{
if (STACKCOOKIE != reinterpret_cast<unsigned*>(taskStackTop)[pos])
break;
}
#ifndef ARDUINO_attiny
::printf(PSTR(">>>stack>>>\n"));
#endif
while (pos < (taskStackSize + (FULLFEATURES ? sizeof(STACKCOOKIE) : 0)) / sizeof(STACKCOOKIE))
{
#ifndef ARDUINO_attiny
auto* sp = &reinterpret_cast<unsigned*>(taskStackTop)[pos];
// rough indicator: stack frames usually have SP saved as the second word
bool looksLikeStackFrame = (sp[2] == reinterpret_cast<size_t>(&sp[4]));
::printf(PSTR("%08x: %08x %08x %08x %08x %c\n"),
reinterpret_cast<size_t>(sp), sp[0], sp[1], sp[2], sp[3], looksLikeStackFrame ? '<' : ' ');
#endif
pos += 4;
}
#ifndef ARDUINO_attiny
::printf(PSTR("<<<stack<<<\n"));
#endif
}
size_t CoopTaskBase::getFreeStack() const
{
if (!taskStackTop) return 0;
size_t pos;
for (pos = 1; pos < (taskStackSize + (FULLFEATURES ? sizeof(STACKCOOKIE) : 0)) / sizeof(STACKCOOKIE); ++pos)
{
if (STACKCOOKIE != reinterpret_cast<unsigned*>(taskStackTop)[pos])
break;
}
return (pos - 1) * sizeof(unsigned);
}
void CoopTaskBase::doYield(unsigned val) noexcept
{
if (!setjmp(env_yield))
{
longjmp(env, val);
}
}
void CoopTaskBase::_delay(uint32_t ms) noexcept
{
delay_ms = true;
#ifdef ESP8266
delay_start = usingBuiltinScheduler ? millis() : ESP.getCycleCount();
#elif ESP32
delay_start = ESP.getCycleCount();
#else
delay_start = millis();
#endif
delay_duration = ms;
// CoopTask::run() defers task for delay_duration milliseconds.
doYield(3);
}
void CoopTaskBase::_delayMicroseconds(uint32_t us) noexcept
{
if (us < DELAYMICROS_THRESHOLD) {
::delayMicroseconds(us);
return;
}
delay_ms = false;
delay_start = micros();
delay_duration = us;
// CoopTask::run() defers task for delay_duration microseconds.
doYield(3);
}
void CoopTaskBase::_exit() noexcept
{
longjmp(env, -1);
}
void CoopTaskBase::_yield() noexcept
{
doYield(1);
}
void CoopTaskBase::_sleep() noexcept
{
doYield(2);
}
void IRAM_ATTR CoopTaskBase::sleep(const bool state) noexcept
{
sleeps.store(state);
if (!state)
{
delays.store(false);
delay_duration = 0;
}
}
#endif // _MSC_VER
void runCoopTasks(const Delegate<void(const CoopTaskBase* const task)>& reaper,
const Delegate<bool(uint32_t ms)>& onDelay, const Delegate<bool()>& onSleep)
{
#ifdef ESP32_FREERTOS
static TaskHandle_t yieldGuardHandle = nullptr;
if (!yieldGuardHandle)
{
xTaskCreateUniversal([](void*)
{
for (;;)
{
vPortYield();
}
}, "YieldGuard", 0x200, nullptr, 1, &yieldGuardHandle, CONFIG_ARDUINO_RUNNING_CORE);
}
#endif
auto taskCount = CoopTaskBase::getRunnableTasksCount();
bool allSleeping = true;
uint32_t minDelay_ms = ~(decltype(minDelay_ms))0U;
for (size_t i = 0; taskCount && i < CoopTaskBase::getRunnableTasks().size(); ++i)
{
#if defined(ESP8266) || defined(ESP32)
optimistic_yield(10000);
#endif
auto task = CoopTaskBase::getRunnableTasks()[i].load();
if (task)
{
--taskCount;
auto runResult = task->run();
if (runResult < 0 && reaper)
reaper(task);
else if (minDelay_ms)
{
if (task->delayed())
{
allSleeping = false;
uint32_t delay_ms = task->delayIsMs() ? static_cast<uint32_t>(runResult) : static_cast<uint32_t>(runResult) / 1000UL;
if (delay_ms < minDelay_ms)
minDelay_ms = delay_ms;
}
else if (!task->sleeping())
{
allSleeping = false;
minDelay_ms = 0;
}
}
}
}
bool cleanup = true;
if (allSleeping && onSleep)
{
cleanup = onSleep();
}
else if (minDelay_ms && onDelay)
{
cleanup = onDelay(minDelay_ms);
}
if (cleanup)
{
#ifdef ESP32_FREERTOS
vTaskSuspend(yieldGuardHandle);
vTaskDelay(1);
vTaskResume(yieldGuardHandle);
#endif
}
}

View File

@@ -0,0 +1,270 @@
/*
CoopTaskBase.h - Implementation of cooperative scheduling tasks
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __CoopTaskBase_h
#define __CoopTaskBase_h
#ifdef ESP32
#define ESP32_FREERTOS
#endif
#include "circular_queue/Delegate.h"
#if defined(ESP8266) || defined(ESP32)
#include <array>
#include <memory>
#include <csetjmp>
#include <Arduino.h>
#elif defined(ARDUINO)
#include <setjmp.h>
#include <Arduino.h>
#elif defined(_MSC_VER)
#include <array>
#include <Windows.h>
#include <string>
#else
#include <array>
#include <csetjmp>
#include <string>
#endif
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
#include <atomic>
#else
#include "circular_queue/ghostl.h"
#endif
#if !defined(ESP32) && !defined(ESP8266)
#define IRAM_ATTR
#endif
#ifdef _MSC_VER
#define __attribute__(_)
#endif
class CoopTaskBase
{
public:
static constexpr bool FULLFEATURES = sizeof(unsigned) >= 4;
protected:
using taskfunction_t = Delegate< void() >;
#ifdef ARDUINO
CoopTaskBase(const String& name, taskfunction_t _func, size_t stackSize = DEFAULTTASKSTACKSIZE) :
#else
CoopTaskBase(const std::string& name, taskfunction_t _func, size_t stackSize = DEFAULTTASKSTACKSIZE) :
#endif
taskName(name), sleeps(true), delays(false), func(_func)
{
taskStackSize = (sizeof(unsigned) >= 4) ? ((stackSize + sizeof(unsigned) - 1) / sizeof(unsigned)) * sizeof(unsigned) : stackSize;
}
CoopTaskBase(const CoopTaskBase&) = delete;
CoopTaskBase& operator=(const CoopTaskBase&) = delete;
static constexpr int32_t DELAYMICROS_THRESHOLD = 50;
static constexpr uint32_t DELAY_MAXINT = (~(uint32_t)0) >> 1;
#ifdef ARDUINO
const String taskName;
#else
const std::string taskName;
#endif
size_t taskStackSize;
#if defined(_MSC_VER)
static LPVOID primaryFiber;
LPVOID taskFiber = nullptr;
int val = 0;
static void __stdcall taskFiberFunc(void* self);
#elif defined(ESP32_FREERTOS)
TaskHandle_t taskHandle = nullptr;
static void taskFunc(void* _self);
#else
char* taskStackTop = nullptr;
static jmp_buf env;
jmp_buf env_yield;
#endif
static constexpr size_t MAXNUMBERCOOPTASKS = FULLFEATURES ? 32 : 8;
// for lock-free insertion, must be one element larger than max task count
static std::array< std::atomic<CoopTaskBase* >, MAXNUMBERCOOPTASKS + 1> runnableTasks;
static std::atomic<size_t> runnableTasksCount;
static CoopTaskBase* current;
bool init = false;
bool cont = true;
std::atomic<bool> sleeps;
// ESP32 FreeRTOS (#define ESP32_FREERTOS) handles delays, on this platfrom delays is always false
std::atomic<bool> delays;
int32_t initialize();
void doYield(unsigned val) noexcept;
#if defined(ESP8266)
static bool usingBuiltinScheduler;
bool rescheduleTask(uint32_t repeat_us);
#endif
bool IRAM_ATTR enrollRunnable();
void delistRunnable();
void _exit() noexcept;
void _yield() noexcept;
void _sleep() noexcept;
void _delay(uint32_t ms) noexcept;
void _delayMicroseconds(uint32_t us) noexcept;
private:
// true: delay_start/delay_duration are in milliseconds; false: delay_start/delay_duration are in microseconds.
bool delay_ms = false;
uint32_t delay_start = 0;
uint32_t delay_duration = 0;
taskfunction_t func;
public:
virtual ~CoopTaskBase();
#if defined(ESP32)
static constexpr size_t MAXSTACKSPACE = 0x2000;
#elif defined(ESP8266)
static constexpr size_t MAXSTACKSPACE = 0x1000;
#elif defined(ARDUINO)
static constexpr size_t MAXSTACKSPACE = FULLFEATURES ? 0x180 : 0xc0;
#else
static constexpr size_t MAXSTACKSPACE = 0x10000;
#endif
static constexpr unsigned STACKCOOKIE = FULLFEATURES ? 0xdeadbeefUL : 0xdeadU;
static constexpr size_t DEFAULTTASKSTACKSIZE = MAXSTACKSPACE - (FULLFEATURES ? 2 : 1) * sizeof(STACKCOOKIE);
#ifdef ARDUINO
const String& name() const noexcept { return taskName; }
#else
const std::string& name() const noexcept { return taskName; }
#endif
/// @returns: true if the CoopTask object is ready to run, including stack allocation.
/// false if either initialization has failed, or the task has exited().
#if !defined(_MSC_VER) && !defined(ESP32_FREERTOS)
operator bool() const noexcept { return cont && taskStackTop; }
/// Prints the task stack, decodable by the ESP exception decoder
void dumpStack() const;
#else
operator bool() const noexcept { return cont; }
#endif
/// Ready the task for scheduling, by default waking up the task from both sleep and delay.
/// @returns: true on success.
bool IRAM_ATTR scheduleTask(bool wakeup = true);
inline bool IRAM_ATTR wakeup() __attribute__((always_inline)) { return scheduleTask(true); }
#ifdef ESP8266
/// For full access to all features, cyclic task scheduling, state evaluation
/// and running are performed explicitly from user code. For convenience, the function
/// runCoopTasks() implements the pattern as best practice. See the CoopTask examples for this.
/// If only a pre-determined number of loop tasks need to run indefinitely
/// without exit codes or explicit deep sleep system states, on platforms where a
/// scheduler exists that is suffiently capable to iteratively run each of these CoopTasks,
/// calling this function switches all task creation and scheduling to using that, obviating
/// the need to call a scheduler explicitly from user code.
/// The scheduler selection should be done before the first CoopTask is created, and not
/// changed thereafter during runtime.
/// By default, builtin schedulers are not used, for well-defined behavior and portability.
/// @param state true: The parameter default value. All subsequent scheduling of tasks is
/// handed to the builtin scheduler.
static void useBuiltinScheduler(bool state = true)
{
usingBuiltinScheduler = state;
}
#endif
/// Every task is entered into this list by scheduleTask(). It is removed when it exits
/// or gets deleted.
static const decltype(runnableTasks)& getRunnableTasks()
{
return runnableTasks;
}
/// @returns: the count of runnable, non-nullptr, tasks in the return of getRunnableTasks().
static size_t getRunnableTasksCount()
{
return runnableTasksCount.load();
}
/// @returns: -1: exited. 0: runnable or sleeping. >0: delayed for milliseconds or microseconds, check delayIsMs().
int32_t run();
/// @returns: size of unused stack space. 0 if stack is not allocated yet or was deleted after task exited.
size_t getFreeStack() const;
bool delayIsMs() const noexcept { return delay_ms; }
/// Modifies the sleep flag. if called from a running task, it is not immediately suspended.
/// @param state true: a suspended task becomes sleeping, if call from the running task,
/// the next call to yield() or delay() puts it into sleeping state.
/// false: clears the sleeping and delay state of the task.
void IRAM_ATTR sleep(const bool state) noexcept;
#ifdef ESP32_FREERTOS
/// @returns: a pointer to the CoopTask instance that is running. nullptr if not called from a CoopTask function (running() == false).
static CoopTaskBase* self() noexcept;
#else
/// @returns: a pointer to the CoopTask instance that is running. nullptr if not called from a CoopTask function (running() == false).
static CoopTaskBase* self() noexcept { return current; }
#endif
/// @returns: true if called from the task function of a CoopTask, false otherwise.
static bool running() noexcept { return self(); }
/// @returns: true if the task's is set to sleep.
/// For a non-running task, this implies it is also currently not scheduled.
inline bool IRAM_ATTR sleeping() const noexcept __attribute__((always_inline)) { return sleeps.load(); }
inline bool IRAM_ATTR delayed() const noexcept __attribute__((always_inline)) { return delays.load(); }
inline bool IRAM_ATTR suspended() const noexcept __attribute__((always_inline)) { return sleeps.load() || delays.load(); }
/// use only in running CoopTask function. As stack unwinding is corrupted
/// by exit(), which among other issues breaks the RAII idiom,
/// using regular return or exceptions is to be preferred in most cases.
static void exit() noexcept { self()->_exit(); }
/// use only in running CoopTask function.
static void yield() noexcept { self()->_yield(); }
static void yield(CoopTaskBase* self) noexcept { self->_yield(); }
/// use only in running CoopTask function.
static void sleep() noexcept { self()->_sleep(); }
/// use only in running CoopTask function.
static void delay(uint32_t ms) noexcept { self()->_delay(ms); }
static void delay(CoopTaskBase* self, uint32_t ms) noexcept { self->_delay(ms); }
/// use only in running CoopTask function.
static void delayMicroseconds(uint32_t us) noexcept { self()->_delayMicroseconds(us); }
};
#ifndef ARDUINO
inline void yield() { CoopTaskBase::yield(); }
inline void delay(uint32_t ms) { CoopTaskBase::delay(ms); }
#endif
/// An optional convenience funtion that does all the work to cyclically perform CoopTask execution.
/// @param reaper An optional function that is called once when a task exits.
/// @param onDelay An optional function to handle a global delay greater or equal 1 millisecond, resulting
/// from the minimum time interval for which at this time all CoopTasks are delayed.
/// This can be used for power saving, if wake up by asynchronous events is properly considered.
/// If onSleep is not set, onDelay() is called instead with the maximum value for the ms delay parameter.
/// onDelay() must return a bool value, if true, runCoopTasks performs the default housekeeping actions,
/// otherwise it skips those.
/// @param onSleep An optional function indicating that all CoopTasks are sleeping, that is, are infinitely delayed.
/// This can be used for power saving modes.
/// onSleep(), like onDelay(), must return a bool value, if true, runCoopTasks performs the
/// default housekeeping actions, otherwise it skips those.
void runCoopTasks(const Delegate<void(const CoopTaskBase* const task)>& reaper = nullptr,
const Delegate<bool(uint32_t ms)>& onDelay = nullptr, const Delegate<bool()>& onSleep = nullptr);
#endif // __CoopTaskBase_h

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,567 @@
/*
MultiDelegate.h - A queue or event multiplexer based on the efficient Delegate
class
Copyright (c) 2019-2020 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __MULTIDELEGATE_H
#define __MULTIDELEGATE_H
#include <iterator>
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
#include <atomic>
#else
#include "circular_queue/ghostl.h"
#endif
#if defined(ESP8266)
#include <interrupts.h>
using esp8266::InterruptLock;
#elif defined(ARDUINO)
class InterruptLock {
public:
InterruptLock() {
noInterrupts();
}
~InterruptLock() {
interrupts();
}
};
#else
#include <mutex>
#endif
namespace
{
template< typename Delegate, typename R, bool ISQUEUE = false, typename... P>
struct CallP
{
static R execute(Delegate& del, P... args)
{
return del(std::forward<P...>(args...));
}
};
template< typename Delegate, bool ISQUEUE, typename... P>
struct CallP<Delegate, void, ISQUEUE, P...>
{
static bool execute(Delegate& del, P... args)
{
del(std::forward<P...>(args...));
return true;
}
};
template< typename Delegate, typename R, bool ISQUEUE = false>
struct Call
{
static R execute(Delegate& del)
{
return del();
}
};
template< typename Delegate, bool ISQUEUE>
struct Call<Delegate, void, ISQUEUE>
{
static bool execute(Delegate& del)
{
del();
return true;
}
};
}
namespace delegate
{
namespace detail
{
template< typename Delegate, typename R, bool ISQUEUE = false, size_t QUEUE_CAPACITY = 32, typename... P>
class MultiDelegatePImpl
{
public:
MultiDelegatePImpl() = default;
~MultiDelegatePImpl()
{
*this = nullptr;
}
MultiDelegatePImpl(const MultiDelegatePImpl&) = delete;
MultiDelegatePImpl& operator=(const MultiDelegatePImpl&) = delete;
MultiDelegatePImpl(MultiDelegatePImpl&& md)
{
first = md.first;
last = md.last;
unused = md.unused;
nodeCount = md.nodeCount;
md.first = nullptr;
md.last = nullptr;
md.unused = nullptr;
md.nodeCount = 0;
}
MultiDelegatePImpl(const Delegate& del)
{
add(del);
}
MultiDelegatePImpl(Delegate&& del)
{
add(std::move(del));
}
MultiDelegatePImpl& operator=(MultiDelegatePImpl&& md)
{
first = md.first;
last = md.last;
unused = md.unused;
nodeCount = md.nodeCount;
md.first = nullptr;
md.last = nullptr;
md.unused = nullptr;
md.nodeCount = 0;
return *this;
}
MultiDelegatePImpl& operator=(std::nullptr_t)
{
if (last)
last->mNext = unused;
if (first)
unused = first;
while (unused)
{
auto to_delete = unused;
unused = unused->mNext;
delete(to_delete);
}
return *this;
}
MultiDelegatePImpl& operator+=(const Delegate& del)
{
add(del);
return *this;
}
MultiDelegatePImpl& operator+=(Delegate&& del)
{
add(std::move(del));
return *this;
}
protected:
struct Node_t
{
~Node_t()
{
mDelegate = nullptr; // special overload in Delegate
}
Node_t* mNext = nullptr;
Delegate mDelegate;
};
Node_t* first = nullptr;
Node_t* last = nullptr;
Node_t* unused = nullptr;
size_t nodeCount = 0;
// Returns a pointer to an unused Node_t,
// or if none are available allocates a new one,
// or nullptr if limit is reached
Node_t* IRAM_ATTR get_node_unsafe()
{
Node_t* result = nullptr;
// try to get an item from unused items list
if (unused)
{
result = unused;
unused = unused->mNext;
}
// if no unused items, and count not too high, allocate a new one
else if (nodeCount < QUEUE_CAPACITY)
{
#if defined(ESP8266) || defined(ESP32)
result = new (std::nothrow) Node_t;
#else
result = new Node_t;
#endif
if (result)
++nodeCount;
}
return result;
}
void recycle_node_unsafe(Node_t* node)
{
node->mDelegate = nullptr; // special overload in Delegate
node->mNext = unused;
unused = node;
}
#ifndef ARDUINO
std::mutex mutex_unused;
#endif
public:
class iterator : public std::iterator<std::forward_iterator_tag, Delegate>
{
public:
Node_t* current = nullptr;
Node_t* prev = nullptr;
const Node_t* stop = nullptr;
iterator(MultiDelegatePImpl& md) : current(md.first), stop(md.last) {}
iterator() = default;
iterator(const iterator&) = default;
iterator& operator=(const iterator&) = default;
iterator& operator=(iterator&&) = default;
operator bool() const
{
return current && stop;
}
bool operator==(const iterator& rhs) const
{
return current == rhs.current;
}
bool operator!=(const iterator& rhs) const
{
return !operator==(rhs);
}
Delegate& operator*() const
{
return current->mDelegate;
}
Delegate* operator->() const
{
return &current->mDelegate;
}
iterator& operator++() // prefix
{
if (current && stop != current)
{
prev = current;
current = current->mNext;
}
else
current = nullptr; // end
return *this;
}
iterator& operator++(int) // postfix
{
iterator tmp(*this);
operator++();
return tmp;
}
};
iterator begin()
{
return iterator(*this);
}
iterator end() const
{
return iterator();
}
const Delegate* IRAM_ATTR add(const Delegate& del)
{
return add(Delegate(del));
}
const Delegate* IRAM_ATTR add(Delegate&& del)
{
if (!del)
return nullptr;
#ifdef ARDUINO
InterruptLock lockAllInterruptsInThisScope;
#else
std::lock_guard<std::mutex> lock(mutex_unused);
#endif
Node_t* item = ISQUEUE ? get_node_unsafe() :
#if defined(ESP8266) || defined(ESP32)
new (std::nothrow) Node_t;
#else
new Node_t;
#endif
if (!item)
return nullptr;
item->mDelegate = std::move(del);
item->mNext = nullptr;
if (last)
last->mNext = item;
else
first = item;
last = item;
return &item->mDelegate;
}
iterator erase(iterator it)
{
if (!it)
return end();
#ifdef ARDUINO
InterruptLock lockAllInterruptsInThisScope;
#else
std::lock_guard<std::mutex> lock(mutex_unused);
#endif
auto to_recycle = it.current;
if (last == it.current)
last = it.prev;
it.current = it.current->mNext;
if (it.prev)
{
it.prev->mNext = it.current;
}
else
{
first = it.current;
}
if (ISQUEUE)
recycle_node_unsafe(to_recycle);
else
delete to_recycle;
return it;
}
bool erase(const Delegate* const del)
{
auto it = begin();
while (it)
{
if (del == &(*it))
{
erase(it);
return true;
}
++it;
}
return false;
}
operator bool() const
{
return first;
}
R operator()(P... args)
{
auto it = begin();
if (!it)
return {};
static std::atomic<bool> fence(false);
// prevent recursive calls
#if defined(ARDUINO) && !defined(ESP32)
if (fence.load()) return {};
fence.store(true);
#else
if (fence.exchange(true)) return {};
#endif
R result;
do
{
result = CallP<Delegate, R, ISQUEUE, P...>::execute(*it, args...);
if (result && ISQUEUE)
it = erase(it);
else
++it;
#if defined(ESP8266) || defined(ESP32)
// running callbacks might last too long for watchdog etc.
optimistic_yield(10000);
#endif
} while (it);
fence.store(false);
return result;
}
};
template< typename Delegate, typename R = void, bool ISQUEUE = false, size_t QUEUE_CAPACITY = 32>
class MultiDelegateImpl : public MultiDelegatePImpl<Delegate, R, ISQUEUE, QUEUE_CAPACITY>
{
public:
using MultiDelegatePImpl<Delegate, R, ISQUEUE, QUEUE_CAPACITY>::MultiDelegatePImpl;
R operator()()
{
auto it = this->begin();
if (!it)
return {};
static std::atomic<bool> fence(false);
// prevent recursive calls
#if defined(ARDUINO) && !defined(ESP32)
if (fence.load()) return {};
fence.store(true);
#else
if (fence.exchange(true)) return {};
#endif
R result;
do
{
result = Call<Delegate, R, ISQUEUE>::execute(*it);
if (result && ISQUEUE)
it = this->erase(it);
else
++it;
#if defined(ESP8266) || defined(ESP32)
// running callbacks might last too long for watchdog etc.
optimistic_yield(10000);
#endif
} while (it);
fence.store(false);
return result;
}
};
template< typename Delegate, typename R, bool ISQUEUE, size_t QUEUE_CAPACITY, typename... P> class MultiDelegate;
template< typename Delegate, typename R, bool ISQUEUE, size_t QUEUE_CAPACITY, typename... P>
class MultiDelegate<Delegate, R(P...), ISQUEUE, QUEUE_CAPACITY> : public MultiDelegatePImpl<Delegate, R, ISQUEUE, QUEUE_CAPACITY, P...>
{
public:
using MultiDelegatePImpl<Delegate, R, ISQUEUE, QUEUE_CAPACITY, P...>::MultiDelegatePImpl;
};
template< typename Delegate, typename R, bool ISQUEUE, size_t QUEUE_CAPACITY>
class MultiDelegate<Delegate, R(), ISQUEUE, QUEUE_CAPACITY> : public MultiDelegateImpl<Delegate, R, ISQUEUE, QUEUE_CAPACITY>
{
public:
using MultiDelegateImpl<Delegate, R, ISQUEUE, QUEUE_CAPACITY>::MultiDelegateImpl;
};
template< typename Delegate, bool ISQUEUE, size_t QUEUE_CAPACITY, typename... P>
class MultiDelegate<Delegate, void(P...), ISQUEUE, QUEUE_CAPACITY> : public MultiDelegatePImpl<Delegate, void, ISQUEUE, QUEUE_CAPACITY, P...>
{
public:
using MultiDelegatePImpl<Delegate, void, ISQUEUE, QUEUE_CAPACITY, P...>::MultiDelegatePImpl;
void operator()(P... args)
{
auto it = this->begin();
if (!it)
return;
static std::atomic<bool> fence(false);
// prevent recursive calls
#if defined(ARDUINO) && !defined(ESP32)
if (fence.load()) return;
fence.store(true);
#else
if (fence.exchange(true)) return;
#endif
do
{
CallP<Delegate, void, ISQUEUE, P...>::execute(*it, args...);
if (ISQUEUE)
it = this->erase(it);
else
++it;
#if defined(ESP8266) || defined(ESP32)
// running callbacks might last too long for watchdog etc.
optimistic_yield(10000);
#endif
} while (it);
fence.store(false);
}
};
template< typename Delegate, bool ISQUEUE, size_t QUEUE_CAPACITY>
class MultiDelegate<Delegate, void(), ISQUEUE, QUEUE_CAPACITY> : public MultiDelegateImpl<Delegate, void, ISQUEUE, QUEUE_CAPACITY>
{
public:
using MultiDelegateImpl<Delegate, void, ISQUEUE, QUEUE_CAPACITY>::MultiDelegateImpl;
void operator()()
{
auto it = this->begin();
if (!it)
return;
static std::atomic<bool> fence(false);
// prevent recursive calls
#if defined(ARDUINO) && !defined(ESP32)
if (fence.load()) return;
fence.store(true);
#else
if (fence.exchange(true)) return;
#endif
do
{
Call<Delegate, void, ISQUEUE>::execute(*it);
if (ISQUEUE)
it = this->erase(it);
else
++it;
#if defined(ESP8266) || defined(ESP32)
// running callbacks might last too long for watchdog etc.
optimistic_yield(10000);
#endif
} while (it);
fence.store(false);
}
};
}
}
/**
The MultiDelegate class template can be specialized to either a queue or an event multiplexer.
It is designed to be used with Delegate, the efficient runtime wrapper for C function ptr and C++ std::function.
@tparam Delegate specifies the concrete type that MultiDelegate bases the queue or event multiplexer on.
@tparam ISQUEUE modifies the generated MultiDelegate class in subtle ways. In queue mode (ISQUEUE == true),
the value of QUEUE_CAPACITY enforces the maximum number of simultaneous items the queue can contain.
This is exploited to minimize the use of new and delete by reusing already allocated items, thus
reducing heap fragmentation. In event multiplexer mode (ISQUEUE = false), new and delete are
used for allocation of the event handler items.
If the result type of the function call operator of Delegate is void, calling a MultiDelegate queue
removes each item after calling it; a Multidelegate event multiplexer keeps event handlers until
explicitly removed.
If the result type of the function call operator of Delegate is non-void, in a MultiDelegate queue
the type-conversion to bool of that result determines if the item is immediately removed or kept
after each call: if true is returned, the item is removed. A Multidelegate event multiplexer keeps event
handlers until they are explicitly removed.
@tparam QUEUE_CAPACITY is only used if ISQUEUE == true. Then, it sets the maximum capacity that the queue dynamically
allocates from the heap. Unused items are not returned to the heap, but are managed by the MultiDelegate
instance during its own lifetime for efficiency.
*/
template< typename Delegate, bool ISQUEUE = false, size_t QUEUE_CAPACITY = 32>
class MultiDelegate : public delegate::detail::MultiDelegate<Delegate, typename Delegate::target_type, ISQUEUE, QUEUE_CAPACITY>
{
public:
using delegate::detail::MultiDelegate<Delegate, typename Delegate::target_type, ISQUEUE, QUEUE_CAPACITY>::MultiDelegate;
};
#endif // __MULTIDELEGATE_H

View File

@@ -0,0 +1,393 @@
/*
circular_queue.h - Implementation of a lock-free circular queue for EspSoftwareSerial.
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __circular_queue_h
#define __circular_queue_h
#ifdef ARDUINO
#include <Arduino.h>
#endif
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
#include <atomic>
#include <memory>
#include <algorithm>
#include "Delegate.h"
using std::min;
#else
#include "ghostl.h"
#endif
#if !defined(ESP32) && !defined(ESP8266)
#define IRAM_ATTR
#endif
/*!
@brief Instance class for a single-producer, single-consumer circular queue / ring buffer (FIFO).
This implementation is lock-free between producer and consumer for the available(), peek(),
pop(), and push() type functions.
*/
template< typename T, typename ForEachArg = void >
class circular_queue
{
public:
/*!
@brief Constructs a valid, but zero-capacity dummy queue.
*/
circular_queue() : m_bufSize(1)
{
m_inPos.store(0);
m_outPos.store(0);
}
/*!
@brief Constructs a queue of the given maximum capacity.
*/
circular_queue(const size_t capacity) : m_bufSize(capacity + 1), m_buffer(new T[m_bufSize])
{
m_inPos.store(0);
m_outPos.store(0);
}
circular_queue(circular_queue&& cq) :
m_bufSize(cq.m_bufSize), m_buffer(cq.m_buffer), m_inPos(cq.m_inPos.load()), m_outPos(cq.m_outPos.load())
{}
~circular_queue()
{
m_buffer.reset();
}
circular_queue(const circular_queue&) = delete;
circular_queue& operator=(circular_queue&& cq)
{
m_bufSize = cq.m_bufSize;
m_buffer = cq.m_buffer;
m_inPos.store(cq.m_inPos.load());
m_outPos.store(cq.m_outPos.load());
}
circular_queue& operator=(const circular_queue&) = delete;
/*!
@brief Get the numer of elements the queue can hold at most.
*/
size_t capacity() const
{
return m_bufSize - 1;
}
/*!
@brief Resize the queue. The available elements in the queue are preserved.
This is not lock-free and concurrent producer or consumer access
will lead to corruption.
@return True if the new capacity could accommodate the present elements in
the queue, otherwise nothing is done and false is returned.
*/
bool capacity(const size_t cap);
/*!
@brief Discard all data in the queue.
*/
void flush()
{
m_outPos.store(m_inPos.load());
}
/*!
@brief Get a snapshot number of elements that can be retrieved by pop.
*/
size_t available() const
{
int avail = static_cast<int>(m_inPos.load() - m_outPos.load());
if (avail < 0) avail += m_bufSize;
return avail;
}
/*!
@brief Get the remaining free elementes for pushing.
*/
size_t available_for_push() const
{
int avail = static_cast<int>(m_outPos.load() - m_inPos.load()) - 1;
if (avail < 0) avail += m_bufSize;
return avail;
}
/*!
@brief Peek at the next element pop will return without removing it from the queue.
@return An rvalue copy of the next element that can be popped. If the queue is empty,
return an rvalue copy of the element that is pending the next push.
*/
T peek() const
{
const auto outPos = m_outPos.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
return m_buffer[outPos];
}
/*!
@brief Peek at the next pending input value.
@return A reference to the next element that can be pushed.
*/
inline T& IRAM_ATTR pushpeek() __attribute__((always_inline))
{
const auto inPos = m_inPos.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
return m_buffer[inPos];
}
/*!
@brief Release the next pending input value, accessible by pushpeek(), into the queue.
@return true if the queue accepted the value, false if the queue
was full.
*/
inline bool IRAM_ATTR push() __attribute__((always_inline))
{
const auto inPos = m_inPos.load(std::memory_order_acquire);
const size_t next = (inPos + 1) % m_bufSize;
if (next == m_outPos.load(std::memory_order_relaxed)) {
return false;
}
std::atomic_thread_fence(std::memory_order_acquire);
m_inPos.store(next, std::memory_order_release);
return true;
}
/*!
@brief Move the rvalue parameter into the queue.
@return true if the queue accepted the value, false if the queue
was full.
*/
inline bool IRAM_ATTR push(T&& val) __attribute__((always_inline))
{
const auto inPos = m_inPos.load(std::memory_order_acquire);
const size_t next = (inPos + 1) % m_bufSize;
if (next == m_outPos.load(std::memory_order_relaxed)) {
return false;
}
std::atomic_thread_fence(std::memory_order_acquire);
m_buffer[inPos] = std::move(val);
std::atomic_thread_fence(std::memory_order_release);
m_inPos.store(next, std::memory_order_release);
return true;
}
/*!
@brief Push a copy of the parameter into the queue.
@return true if the queue accepted the value, false if the queue
was full.
*/
inline bool IRAM_ATTR push(const T& val) __attribute__((always_inline))
{
T v(val);
return push(std::move(v));
}
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
/*!
@brief Push copies of multiple elements from a buffer into the queue,
in order, beginning at buffer's head.
@return The number of elements actually copied into the queue, counted
from the buffer head.
*/
size_t push_n(const T* buffer, size_t size);
#endif
/*!
@brief Pop the next available element from the queue.
@return An rvalue copy of the popped element, or a default
value of type T if the queue is empty.
*/
T pop();
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
/*!
@brief Pop multiple elements in ordered sequence from the queue to a buffer.
If buffer is nullptr, simply discards up to size elements from the queue.
@return The number of elements actually popped from the queue to
buffer.
*/
size_t pop_n(T* buffer, size_t size);
#endif
/*!
@brief Iterate over and remove each available element from queue,
calling back fun with an rvalue reference of every single element.
*/
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
void for_each(const Delegate<void(T&&), ForEachArg>& fun);
#else
void for_each(Delegate<void(T&&), ForEachArg> fun);
#endif
/*!
@brief In reverse order, iterate over, pop and optionally requeue each available element from the queue,
calling back fun with a reference of every single element.
Requeuing is dependent on the return boolean of the callback function. If it
returns true, the requeue occurs.
*/
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
bool for_each_rev_requeue(const Delegate<bool(T&), ForEachArg>& fun);
#else
bool for_each_rev_requeue(Delegate<bool(T&), ForEachArg> fun);
#endif
protected:
const T defaultValue = {};
size_t m_bufSize;
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
std::unique_ptr<T[]> m_buffer;
#else
std::unique_ptr<T> m_buffer;
#endif
std::atomic<size_t> m_inPos;
std::atomic<size_t> m_outPos;
};
template< typename T, typename ForEachArg >
bool circular_queue<T, ForEachArg>::capacity(const size_t cap)
{
if (cap + 1 == m_bufSize) return true;
else if (available() > cap) return false;
std::unique_ptr<T[] > buffer(new T[cap + 1]);
const auto available = pop_n(buffer, cap);
m_buffer.reset(buffer);
m_bufSize = cap + 1;
std::atomic_thread_fence(std::memory_order_release);
m_inPos.store(available, std::memory_order_relaxed);
m_outPos.store(0, std::memory_order_release);
return true;
}
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
template< typename T, typename ForEachArg >
size_t circular_queue<T, ForEachArg>::push_n(const T* buffer, size_t size)
{
const auto inPos = m_inPos.load(std::memory_order_acquire);
const auto outPos = m_outPos.load(std::memory_order_relaxed);
size_t blockSize = (outPos > inPos) ? outPos - 1 - inPos : (outPos == 0) ? m_bufSize - 1 - inPos : m_bufSize - inPos;
blockSize = min(size, blockSize);
if (!blockSize) return 0;
int next = (inPos + blockSize) % m_bufSize;
std::atomic_thread_fence(std::memory_order_acquire);
auto dest = m_buffer.get() + inPos;
std::copy_n(std::make_move_iterator(buffer), blockSize, dest);
size = min(size - blockSize, outPos > 1 ? static_cast<size_t>(outPos - next - 1) : 0);
next += size;
dest = m_buffer.get();
std::copy_n(std::make_move_iterator(buffer + blockSize), size, dest);
std::atomic_thread_fence(std::memory_order_release);
m_inPos.store(next, std::memory_order_release);
return blockSize + size;
}
#endif
template< typename T, typename ForEachArg >
T circular_queue<T, ForEachArg>::pop()
{
const auto outPos = m_outPos.load(std::memory_order_acquire);
if (m_inPos.load(std::memory_order_relaxed) == outPos) return defaultValue;
std::atomic_thread_fence(std::memory_order_acquire);
auto val = std::move(m_buffer[outPos]);
std::atomic_thread_fence(std::memory_order_release);
m_outPos.store((outPos + 1) % m_bufSize, std::memory_order_release);
return val;
}
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
template< typename T, typename ForEachArg >
size_t circular_queue<T, ForEachArg>::pop_n(T* buffer, size_t size) {
size_t avail = size = min(size, available());
if (!avail) return 0;
const auto outPos = m_outPos.load(std::memory_order_acquire);
size_t n = min(avail, static_cast<size_t>(m_bufSize - outPos));
std::atomic_thread_fence(std::memory_order_acquire);
if (buffer) {
buffer = std::copy_n(std::make_move_iterator(m_buffer.get() + outPos), n, buffer);
avail -= n;
std::copy_n(std::make_move_iterator(m_buffer.get()), avail, buffer);
}
std::atomic_thread_fence(std::memory_order_release);
m_outPos.store((outPos + size) % m_bufSize, std::memory_order_release);
return size;
}
#endif
template< typename T, typename ForEachArg >
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
void circular_queue<T, ForEachArg>::for_each(const Delegate<void(T&&), ForEachArg>& fun)
#else
void circular_queue<T, ForEachArg>::for_each(Delegate<void(T&&), ForEachArg> fun)
#endif
{
auto outPos = m_outPos.load(std::memory_order_acquire);
const auto inPos = m_inPos.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
while (outPos != inPos)
{
fun(std::move(m_buffer[outPos]));
std::atomic_thread_fence(std::memory_order_release);
outPos = (outPos + 1) % m_bufSize;
m_outPos.store(outPos, std::memory_order_release);
}
}
template< typename T, typename ForEachArg >
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
bool circular_queue<T, ForEachArg>::for_each_rev_requeue(const Delegate<bool(T&), ForEachArg>& fun)
#else
bool circular_queue<T, ForEachArg>::for_each_rev_requeue(Delegate<bool(T&), ForEachArg> fun)
#endif
{
auto inPos0 = circular_queue<T, ForEachArg>::m_inPos.load(std::memory_order_acquire);
auto outPos = circular_queue<T, ForEachArg>::m_outPos.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (outPos == inPos0) return false;
auto pos = inPos0;
auto outPos1 = inPos0;
const auto posDecr = circular_queue<T, ForEachArg>::m_bufSize - 1;
do {
pos = (pos + posDecr) % circular_queue<T, ForEachArg>::m_bufSize;
T&& val = std::move(circular_queue<T, ForEachArg>::m_buffer[pos]);
if (fun(val))
{
outPos1 = (outPos1 + posDecr) % circular_queue<T, ForEachArg>::m_bufSize;
if (outPos1 != pos) circular_queue<T, ForEachArg>::m_buffer[outPos1] = std::move(val);
}
} while (pos != outPos);
circular_queue<T, ForEachArg>::m_outPos.store(outPos1, std::memory_order_release);
return true;
}
#endif // __circular_queue_h

View File

@@ -0,0 +1,200 @@
/*
circular_queue_mp.h - Implementation of a lock-free circular queue for EspSoftwareSerial.
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __circular_queue_mp_h
#define __circular_queue_mp_h
#include "circular_queue.h"
#ifdef ESP8266
#include "interrupts.h"
#else
#include <mutex>
#endif
/*!
@brief Instance class for a multi-producer, single-consumer circular queue / ring buffer (FIFO).
This implementation is lock-free between producers and consumer for the available(), peek(),
pop(), and push() type functions, but is guarded to safely allow only a single producer
at any instant.
*/
template< typename T, typename ForEachArg = void >
class circular_queue_mp : protected circular_queue<T, ForEachArg>
{
public:
circular_queue_mp() = default;
circular_queue_mp(const size_t capacity) : circular_queue<T, ForEachArg>(capacity)
{}
circular_queue_mp(circular_queue<T, ForEachArg>&& cq) : circular_queue<T, ForEachArg>(std::move(cq))
{}
using circular_queue<T, ForEachArg>::operator=;
using circular_queue<T, ForEachArg>::capacity;
using circular_queue<T, ForEachArg>::flush;
using circular_queue<T, ForEachArg>::available;
using circular_queue<T, ForEachArg>::available_for_push;
using circular_queue<T, ForEachArg>::peek;
using circular_queue<T, ForEachArg>::pop;
using circular_queue<T, ForEachArg>::pop_n;
using circular_queue<T, ForEachArg>::for_each;
using circular_queue<T, ForEachArg>::for_each_rev_requeue;
/*!
@brief Resize the queue. The available elements in the queue are preserved.
This is not lock-free, but safe, concurrent producer or consumer access
is guarded.
@return True if the new capacity could accommodate the present elements in
the queue, otherwise nothing is done and false is returned.
*/
bool capacity(const size_t cap)
{
#ifdef ESP8266
esp8266::InterruptLock lock;
#else
std::lock_guard<std::mutex> lock(m_pushMtx);
#endif
return circular_queue<T, ForEachArg>::capacity(cap);
}
bool IRAM_ATTR push() = delete;
/*!
@brief Move the rvalue parameter into the queue, guarded
for multiple concurrent producers.
@return true if the queue accepted the value, false if the queue
was full.
*/
bool IRAM_ATTR push(T&& val)
{
#ifdef ESP8266
esp8266::InterruptLock lock;
#else
std::lock_guard<std::mutex> lock(m_pushMtx);
#endif
return circular_queue<T, ForEachArg>::push(std::move(val));
}
/*!
@brief Push a copy of the parameter into the queue, guarded
for multiple concurrent producers.
@return true if the queue accepted the value, false if the queue
was full.
*/
bool IRAM_ATTR push(const T& val)
{
#ifdef ESP8266
esp8266::InterruptLock lock;
#else
std::lock_guard<std::mutex> lock(m_pushMtx);
#endif
return circular_queue<T, ForEachArg>::push(val);
}
/*!
@brief Push copies of multiple elements from a buffer into the queue,
in order, beginning at buffer's head. This is guarded for
multiple producers, push_n() is atomic.
@return The number of elements actually copied into the queue, counted
from the buffer head.
*/
size_t push_n(const T* buffer, size_t size)
{
#ifdef ESP8266
esp8266::InterruptLock lock;
#else
std::lock_guard<std::mutex> lock(m_pushMtx);
#endif
return circular_queue<T, ForEachArg>::push_n(buffer, size);
}
/*!
@brief Pops the next available element from the queue, requeues
it immediately.
@return A reference to the just requeued element, or the default
value of type T if the queue is empty.
*/
T& pop_requeue();
/*!
@brief Iterate over, pop and optionally requeue each available element from the queue,
calling back fun with a reference of every single element.
Requeuing is dependent on the return boolean of the callback function. If it
returns true, the requeue occurs.
*/
bool for_each_requeue(const Delegate<bool(T&), ForEachArg>& fun);
#ifndef ESP8266
protected:
std::mutex m_pushMtx;
#endif
};
template< typename T, typename ForEachArg >
T& circular_queue_mp<T, ForEachArg>::pop_requeue()
{
#ifdef ESP8266
esp8266::InterruptLock lock;
#else
std::lock_guard<std::mutex> lock(m_pushMtx);
#endif
const auto outPos = circular_queue<T, ForEachArg>::m_outPos.load(std::memory_order_acquire);
const auto inPos = circular_queue<T, ForEachArg>::m_inPos.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (inPos == outPos) return circular_queue<T, ForEachArg>::defaultValue;
T& val = circular_queue<T, ForEachArg>::m_buffer[inPos] = std::move(circular_queue<T, ForEachArg>::m_buffer[outPos]);
const auto bufSize = circular_queue<T, ForEachArg>::m_bufSize;
std::atomic_thread_fence(std::memory_order_release);
circular_queue<T, ForEachArg>::m_outPos.store((outPos + 1) % bufSize, std::memory_order_relaxed);
circular_queue<T, ForEachArg>::m_inPos.store((inPos + 1) % bufSize, std::memory_order_release);
return val;
}
template< typename T, typename ForEachArg >
bool circular_queue_mp<T, ForEachArg>::for_each_requeue(const Delegate<bool(T&), ForEachArg>& fun)
{
auto inPos0 = circular_queue<T, ForEachArg>::m_inPos.load(std::memory_order_acquire);
auto outPos = circular_queue<T, ForEachArg>::m_outPos.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (outPos == inPos0) return false;
do {
T&& val = std::move(circular_queue<T, ForEachArg>::m_buffer[outPos]);
if (fun(val))
{
#ifdef ESP8266
esp8266::InterruptLock lock;
#else
std::lock_guard<std::mutex> lock(m_pushMtx);
#endif
std::atomic_thread_fence(std::memory_order_release);
auto inPos = circular_queue<T, ForEachArg>::m_inPos.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
circular_queue<T, ForEachArg>::m_buffer[inPos] = std::move(val);
std::atomic_thread_fence(std::memory_order_release);
circular_queue<T, ForEachArg>::m_inPos.store((inPos + 1) % circular_queue<T, ForEachArg>::m_bufSize, std::memory_order_release);
}
else
{
std::atomic_thread_fence(std::memory_order_release);
}
outPos = (outPos + 1) % circular_queue<T, ForEachArg>::m_bufSize;
circular_queue<T, ForEachArg>::m_outPos.store(outPos, std::memory_order_release);
} while (outPos != inPos0);
return true;
}
#endif // __circular_queue_mp_h

View File

@@ -0,0 +1,94 @@
/*
ghostl.h - Implementation of a bare-bones, mostly no-op, C++ STL shell
that allows building some Arduino ESP8266/ESP32
libraries on Aruduino AVR.
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __ghostl_h
#define __ghostl_h
#if defined(ARDUINO_ARCH_SAMD)
#include <atomic>
#endif
using size_t = decltype(sizeof(char));
namespace std
{
#if !defined(ARDUINO_ARCH_SAMD)
typedef enum memory_order {
memory_order_relaxed,
memory_order_acquire,
memory_order_release,
memory_order_seq_cst
} memory_order;
template< typename T > class atomic {
private:
T value;
public:
atomic() {}
atomic(T desired) { value = desired; }
void store(T desired, std::memory_order = std::memory_order_seq_cst) volatile noexcept { value = desired; }
T load(std::memory_order = std::memory_order_seq_cst) const volatile noexcept { return value; }
};
inline void atomic_thread_fence(std::memory_order order) noexcept {}
template< typename T > T&& move(T& t) noexcept { return static_cast<T&&>(t); }
#endif
template< typename T, size_t long N > struct array
{
T _M_elems[N];
decltype(sizeof(0)) size() const { return N; }
T& operator[](decltype(sizeof(0)) i) { return _M_elems[i]; }
const T& operator[](decltype(sizeof(0)) i) const { return _M_elems[i]; }
};
template< typename T > class unique_ptr
{
public:
using pointer = T*;
unique_ptr() noexcept : ptr(nullptr) {}
unique_ptr(pointer p) : ptr(p) {}
pointer operator->() const noexcept { return ptr; }
T& operator[](decltype(sizeof(0)) i) const { return ptr[i]; }
void reset(pointer p = pointer()) noexcept
{
delete ptr;
ptr = p;
}
T& operator*() const { return *ptr; }
private:
pointer ptr;
};
template< typename T > using function = T*;
using nullptr_t = decltype(nullptr);
template<typename T>
struct identity {
typedef T type;
};
template <typename T>
inline T&& forward(typename identity<T>::type& t) noexcept
{
return static_cast<typename identity<T>::type&&>(t);
}
}
#endif // __ghostl_h