36#ifndef _EXECUTOR_EXECUTOR_HXX_
37#define _EXECUTOR_EXECUTOR_HXX_
89 void sync_run(std::function<
void()> fn);
91#if OPENMRN_FEATURE_RTOS_FROM_ISR
98 unsigned priority = UINT_MAX) = 0;
157 OSThread& thread() {
return *
this; }
174 void *
entry()
override;
209 LOG(
FATAL,
"Unexpected select type %d", type);
264template <
unsigned NUM_PRIO>
274 Executor(
const char *name,
int priority,
size_t stack_size)
309 msg, priority >= NUM_PRIO ? NUM_PRIO - 1 : priority);
312 wakeup_executor(
this);
318#if OPENMRN_FEATURE_RTOS_FROM_ISR
324 void add_from_isr(
Executable *msg,
unsigned priority = UINT_MAX)
override
332 msg, priority >= NUM_PRIO ? NUM_PRIO - 1 : priority);
335 msg, priority >= NUM_PRIO ? NUM_PRIO - 1 : priority);
365 *priority = result.index;
387 : ::
Timer(e->active_timers())
409template <
unsigned NUM_PRIO>
Class that manages the list of active timers.
An object that can be scheduled on an executor to run.
This class implements an execution of tasks pulled off an input queue.
Executable *volatile current_
Currently executing closure.
void sync_run(std::function< void()> fn)
Synchronously runs a closure on this executor.
void shutdown()
Terminates the executor thread.
Executable * current()
Helper function for debugging and tracing.
~ExecutorBase()
Destructor.
long long loop_some() ICACHE_FLASH_ATTR
Performs a few loops of the executor on the calling thread.
void * entry() override
Thread entry point.
unsigned selectPrescaler_
How many executables we schedule blindly before calling a select() in order to find more data to read...
fd_set selectRead_
fd to select for read.
fd_set selectExcept_
fd to select for except.
virtual Executable * next(unsigned *priority)=0
Retrieve an item from the front of the queue.
void select(Selectable *job)
Adds a file descriptor to be watched to the select loop.
bool is_selected(Selectable *job)
virtual uint32_t sequence()=0
volatile unsigned sequence_
Sequence number.
const char * name_
name of this Executor
ActiveTimers * active_timers()
void run() override
Entry point.
ExecutorBase()
Constructor.
static ExecutorBase * by_name(const char *name, bool wait)
Lookup an executor by its name.
virtual void add(Executable *action, unsigned priority=UINT_MAX)=0
Send a message to this Executor's queue.
void wait_with_select(long long next_timer_nsec)
Executes a select call, and schedules any necessary executables based on the return.
OSSelectWakeup selectHelper_
Helper object for interruptible select calls.
fd_set selectWrite_
fd to select for write.
bool loop_once()
Performs one loop of the execution on the calling thread.
void assert_current()
Die if we are not on the current executor.
os_thread_t thread_handle()
std::atomic_uint_least8_t started_
1 if the executor is already running
void unselect(Selectable *job)
Removes a job from the select loop.
TypedQueue< Selectable > selectables_
Head of the linked list for the select calls.
ActiveTimers activeTimers_
List of active timers.
fd_set * get_select_set(Selectable::SelectType type)
Helper function.
std::atomic_uint_least8_t done_
Set to 1 when the executor thread has exited and it is safe to delete *this.
int selectNFds_
maximum fd to select for + 1
This class can be given an executor, and will notify itself when that executor is out of work.
ExecutorBase * executor_
Parent.
ExecutorGuard(ExecutorBase *e)
Constructor.
long long timeout() override
Implementation of the guard functionality. Called on the executor.
Implementation the ExecutorBase with a specific number of priority bands.
Executor()
Default Constructor.
Executable * next(unsigned *priority) OVERRIDE
Retrieve an item from the front of the queue.
void add(Executable *msg, unsigned priority=UINT_MAX) OVERRIDE
Send a message to this Executor's queue.
Executor(const NO_THREAD &unused)
Constructor that does not create a thread for running the executor.
QListProtected< NUM_PRIO > queue_
Internal queue of executables waiting to be scheduled.
void start_thread(const char *name, int priority, size_t stack_size)
Creates a new thread for running this executor.
uint32_t sequence() OVERRIDE
void thread_body()
If the executor was created with NO_THREAD, then this function needs to be called to run the executor...
Executor(const char *name, int priority, size_t stack_size)
Constructor.
Using this class as a base class will cause the given class to have all its instances linked up in a ...
Helper class that allows a select to be asynchronously woken up.
void wakeup()
Wakes up the select in the locked thread.
This class provides a threading API.
void inherit()
Inherits the current thread.
void start(const char *name, int priority, size_t stack_size)
Starts the thread.
QMember * next
pointer to the next member in the queue
Handler structure that ExecutorBase knows about each entry to the select call.
SelectType
Which operation this file should be selected upon.
Collection of related state machines that pend on incoming messages.
A Notifiable for synchronously waiting for a notification.
void notify() override
Implementation of notification receive.
A timer that can schedule itself to run on an executor at specified times in the future.
@ RESTART
Restart the timer with existing period.
@ NONE
Do not restart the timer.
void start(long long period=-1)
Starts a timer.
A simple, fast, type-safe single-linked queue class with non-virtual methods.
#define LOG(level, message...)
Conditionally write a message to the logging output.
static const int FATAL
Loglevel that kills the current process.
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
#define ICACHE_FLASH_ATTR
Declares (on the ESP8266) that the current function is not executed too often and should be placed in...
OS_INLINE os_thread_t os_thread_self(void)
Return a handle to the calling thread.