COSA
An Object-Oriented Platform for Arduino Programming
Job_Scheduler.cpp
Go to the documentation of this file.
1 
21 #include "Cosa/Job.hh"
22 
23 bool
25 {
26  // Check that the job is not already started
27  if (job->is_started()) return (false);
28 
29  // Insert the job into the scheduler job queue
30  synchronized {
31  Linkage* succ = &m_queue;
32  Linkage* curr;
33  while ((curr = succ->pred()) != &m_queue) {
34  int32_t diff = ((Job*) curr)->m_expires - job->m_expires;
35  if (diff < 0) break;
36  succ = curr;
37  }
38  succ->attach(job);
39  }
40  return (true);
41 }
42 
43 bool
45 {
46  // Check that the job is started
47  if (!job->is_started()) return (false);
48  job->detach();
49  return (true);
50 }
51 
52 void
54 {
55  // Check if the queue is empty
56  if (m_queue.is_empty()) return;
57 
58  // Run all jobs that have expired
59  Job* job = (Job*) m_queue.succ();
60  while ((Linkage*) job != &m_queue) {
61  uint32_t now = time();
62  int32_t diff = now - job->expire_at();
63  if (diff < 0) return;
64  Job* succ = (Job*) job->succ();
65  ((Link*) job)->detach();
66  job->on_expired();
67  job = succ;
68  }
69 }
uint32_t m_expires
Definition: Job.hh:214
Definition: Job.hh:33
bool is_started() const
Definition: Job.hh:149
virtual bool stop(Job *job)
void detach()
Definition: Linkage.hh:100
virtual void on_expired()
Definition: Job.hh:185
virtual uint32_t time()=0
Linkage * succ() const
Definition: Linkage.hh:51
bool is_empty() const
Definition: Linkage.hh:155
Linkage * pred() const
Definition: Linkage.hh:60
Head m_queue
Definition: Job.hh:84
virtual void dispatch()
virtual bool start(Job *job)
void expire_at(uint32_t time)
Definition: Job.hh:102
void attach(Linkage *pred)
Definition: Linkage.hh:71