COSA
An Object-Oriented Platform for Arduino Programming
|
#include <ProtoThread.hh>
Public Types | |
enum | { INITIATED = 0, READY, WAITING, TIMEOUT, RUNNING, SLEEPING, TERMINATED = 0xff } |
Public Member Functions | |
ProtoThread (Job::Scheduler *scheduler) | |
bool | begin () |
void | end () |
uint8_t | state () const |
void | set_timer (uint16_t ms) |
void | cancel_timer () |
bool | timer_expired () const |
virtual void | on_run (uint8_t type, uint16_t value)=0 |
void | expire_at (uint32_t time) |
uint32_t | expire_at () const |
void | expire_after (uint32_t time) |
int32_t | expire_after () const |
uint32_t | time () const |
bool | is_started () const |
bool | start () |
bool | stop () |
virtual void | on_expired () |
virtual void | run () |
void | detach () |
Linkage * | succ () const |
Linkage * | pred () const |
void | attach (Linkage *pred) |
Static Public Member Functions | |
static uint16_t | dispatch (bool flag=true) |
static void | schedule (ProtoThread *thread) |
Protected Member Functions | |
virtual void | on_event (uint8_t type, uint16_t value) |
Protected Attributes | |
uint8_t | m_state |
void * | m_ip |
uint32_t | m_expires |
Scheduler * | m_scheduler |
Linkage * | m_succ |
Linkage * | m_pred |
Static Protected Attributes | |
static Head | runq |
Cosa implementation of protothreads; A protothread is a low-overhead mechanism for concurrent programming. Protothreads function as stackless, lightweight threads providing a blocking context using minimal memory per protothread. Cosa/Thread supports event to thread mapping and timers.
The thread macro set should only be used within the ProtoThread::on_run() function. The macros cannot be used in functions called from on_run().
Inspired by research and prototype by Adam Dunkels, Oliver Schmidt, Thiermo Voigt, Muneeb Ali, and the protothread library by Larry Ruane.
[1] Adam Dunkels et al, Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems, SenSys'06, November 1-3, 2006, Boulder, Colorado, USA.
[2] Larry Ruane, protothread: An extremely lightweight thread library for GCC, http://code.google.com/p/protothread/
[3] http://en.wikipedia.org/wiki/Protothreads
Definition at line 52 of file ProtoThread.hh.
anonymous enum |
Thread states.
Definition at line 57 of file ProtoThread.hh.
|
inline |
Construct thread, initiate state and continuation. Does not schedule the thread. This is done with begin().
[in] | scheduler | wiht milli-seconds time unit. |
Definition at line 72 of file ProtoThread.hh.
|
inlineinherited |
Attach given linkage as predecessor. Will check and detach if already attached.
[in] | pred | linkage to attach. |
Definition at line 71 of file Linkage.hh.
|
inline |
Start the thread. Must be in INITIATED state to be allowed to be scheduled. Returns false if not in correct state.
Definition at line 83 of file ProtoThread.hh.
|
inline |
Cancel timer and dequeue thread from timer queue.
Definition at line 126 of file ProtoThread.hh.
|
inlineinherited |
Detach this link. Unlink from any list.
Definition at line 125 of file Linkage.hh.
|
static |
Run threads in the run queue. If given flag is true events will be processes. Returns number of dispatched threads and events. The run queue is only iterated once per call to dispatch to allow user defined outer loop, i.e., arduino loop() function.
[in] | flag | process events if non zero. |
Definition at line 41 of file ProtoThread.cpp.
|
inline |
End the thread. Mark as terminated and remove from any queue. Use macro THREAD_END() in thread body (run function).
Definition at line 94 of file ProtoThread.hh.
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
protectedvirtual |
The first level event handler. Filters timeout events and run the thread action function.
[in] | type | the type of event. |
[in] | value | the event value. |
Reimplemented from Job.
Definition at line 27 of file ProtoThread.cpp.
|
inlinevirtualinherited |
Job member function that is called Scheduler::dispatch() when the job time has expired. This function is normally called from an interrupt service routine. The default implementation will push a timeout event with the job as target. The default event handler will call the job run() virtual member function. Override this function if the job should be executed during the interrupt service routine.
|
pure virtual |
Thread activity. Must be overridden. Use the thread macro set in the following format: { PROTO_THREAD_BEGIN(); while (1) { ... PROTO_THREAD_AWAIT(condition); ... } PROTO_THREAD_END(); } Additional macros are PROTO_THREAD_YIELD(), PROTO_THREAD_SLEEP(), PROTO_THREAD_WAKE(), and PROTO_THREAD_DELAY().
[in] | type | the type of event. |
[in] | value | the event value. |
|
inlineinherited |
Return predecessor in sequence.
Definition at line 60 of file Linkage.hh.
|
inlinevirtualinherited |
|
static |
Add the given thread to the run queue (last). A terminated thread may be restarted.
[in] | thread | to enqueue. |
Definition at line 78 of file ProtoThread.cpp.
|
inline |
Set timer and enqueue thread to receive timeout event. If the timer expires the thread is put in TIMEOUT state.
[in] | ms | timeout period. |
Definition at line 114 of file ProtoThread.hh.
|
inlineinherited |
|
inline |
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inline |
Check if the timer expired; i.e., the thread is in TIMEOUT state.
Definition at line 136 of file ProtoThread.hh.
|
protectedinherited |
|
protected |
Definition at line 182 of file ProtoThread.hh.
|
protectedinherited |
Definition at line 94 of file Linkage.hh.
|
protectedinherited |
|
protected |
Definition at line 181 of file ProtoThread.hh.
|
protectedinherited |
Double linked list pointers.
Definition at line 93 of file Linkage.hh.
|
staticprotected |
Definition at line 180 of file ProtoThread.hh.