COSA
An Object-Oriented Platform for Arduino Programming
ProtoThread.cpp
Go to the documentation of this file.
1 
21 #include "ProtoThread.hh"
22 #include "Cosa/Watchdog.hh"
23 
25 
26 void
27 ProtoThread::on_event(uint8_t type, uint16_t value)
28 {
29  if (UNLIKELY(m_state == WAITING)) detach();
31  on_run(type, value);
32  if (m_state == RUNNING) {
33  m_state = READY;
34  }
35  else if (m_state == TIMEOUT) {
36  schedule(this);
37  }
38 }
39 
40 uint16_t
42 {
43  uint16_t count = 0;
44  // Check if events should be processed and the run queue is empty
45  if (flag && runq.is_empty()) {
46  Event event;
47  Event::queue.await(&event);
48  event.dispatch();
49  count += 1;
50  }
51  // Iterate once through the run queue and call all threads run method
52  Linkage* link = runq.succ();
53  while (link != &runq) {
54  Linkage* succ = link->succ();
55  ProtoThread* thread = (ProtoThread*) link;
56  thread->m_state = RUNNING;
57  thread->on_run(Event::RUN_TYPE, 0);
58  if (thread->m_state == RUNNING) {
59  thread->m_state = READY;
60  }
61  link = succ;
62  count += 1;
63  // Check if events should be processed
64  if (flag) {
65  while (Event::queue.available()) {
66  Event event;
67  Event::queue.dequeue(&event);
68  event.dispatch();
69  count += 1;
70  }
71  }
72  }
73  // Return total number of function dispatch
74  return (count);
75 }
76 
77 void
79 {
80  if (UNLIKELY(thread->m_state == TERMINATED)) thread->m_ip = 0;
81  thread->m_state = READY;
82  runq.attach(thread);
83 }
bool dequeue(T *data)
Definition: Queue.hh:145
static void schedule(ProtoThread *thread)
Definition: ProtoThread.cpp:78
static Queue< Event, QUEUE_MAX > queue
Definition: Event.hh:204
Removed from all queues.
Definition: ProtoThread.hh:64
In timer queue.
Definition: ProtoThread.hh:60
In run queue.
Definition: ProtoThread.hh:59
void await(T *data)
Definition: Queue.hh:158
uint8_t m_state
Definition: ProtoThread.hh:181
Linkage * succ() const
Definition: Linkage.hh:51
Dispatched and running.
Definition: ProtoThread.hh:62
static uint16_t dispatch(bool flag=true)
Definition: ProtoThread.cpp:41
Definition: Event.hh:39
bool is_empty() const
Definition: Linkage.hh:155
Timeout received and running.
Definition: ProtoThread.hh:61
virtual void on_event(uint8_t type, uint16_t value)
Definition: ProtoThread.cpp:27
#define UNLIKELY(x)
Definition: Types.h:153
Definition: Linkage.hh:132
static Head runq
Definition: ProtoThread.hh:180
virtual void on_run(uint8_t type, uint16_t value)=0
void attach(Linkage *pred)
Definition: Linkage.hh:71