job scheduler works with run once and run periodic schedules. Stop logic is not fully thought through.

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

Committer:
sgnezdov
Date:
Tue Jul 11 00:16:08 2017 +0000
Revision:
0:806403f3d0d1
Child:
1:ec6a1d054065
basic JobScheduler implementation with core producer - consumer thread safe design; functional, but really bare bones

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgnezdov 0:806403f3d0d1 1 #include "scheduler.h"
sgnezdov 0:806403f3d0d1 2 //#include "core-util/atomic_ops.h"
sgnezdov 0:806403f3d0d1 3
sgnezdov 0:806403f3d0d1 4 void update(void *target) {
sgnezdov 0:806403f3d0d1 5 };
sgnezdov 0:806403f3d0d1 6
sgnezdov 0:806403f3d0d1 7 namespace JobScheduler {
sgnezdov 0:806403f3d0d1 8
sgnezdov 0:806403f3d0d1 9 Scheduler::Scheduler(JobService *jobService)
sgnezdov 0:806403f3d0d1 10 : _jobService(jobService) { }
sgnezdov 0:806403f3d0d1 11
sgnezdov 0:806403f3d0d1 12 void Scheduler::updateAdapter(void *thisPointer) {
sgnezdov 0:806403f3d0d1 13 Scheduler *self = static_cast<Scheduler*>(thisPointer);
sgnezdov 0:806403f3d0d1 14 self->updateHandler();
sgnezdov 0:806403f3d0d1 15 }
sgnezdov 0:806403f3d0d1 16
sgnezdov 0:806403f3d0d1 17 void Scheduler::Start() {
sgnezdov 0:806403f3d0d1 18 _updater.start(callback(Scheduler::updateAdapter, this));
sgnezdov 0:806403f3d0d1 19 }
sgnezdov 0:806403f3d0d1 20
sgnezdov 0:806403f3d0d1 21 void Scheduler::Stop() {
sgnezdov 0:806403f3d0d1 22 // it is not thread-safe, but impact is non-existent.
sgnezdov 0:806403f3d0d1 23 _quit = true;
sgnezdov 0:806403f3d0d1 24 }
sgnezdov 0:806403f3d0d1 25
sgnezdov 0:806403f3d0d1 26 void Scheduler::WaitToStop() {
sgnezdov 0:806403f3d0d1 27 _updater.join();
sgnezdov 0:806403f3d0d1 28 }
sgnezdov 0:806403f3d0d1 29
sgnezdov 0:806403f3d0d1 30 JobRes* Scheduler::JobAdd(int typeID) {
sgnezdov 0:806403f3d0d1 31 Action action(ActionJobAdd, (void*)typeID);
sgnezdov 0:806403f3d0d1 32 _actions.put(&action);
sgnezdov 0:806403f3d0d1 33 // default is wait forever
sgnezdov 0:806403f3d0d1 34 osEvent evt = action.response.get();
sgnezdov 0:806403f3d0d1 35 if (evt.status == osEventMessage) {
sgnezdov 0:806403f3d0d1 36 printf("[Scheduler::JobAdd] completed ok\n");
sgnezdov 0:806403f3d0d1 37 return (JobRes*)evt.value.p;
sgnezdov 0:806403f3d0d1 38 }
sgnezdov 0:806403f3d0d1 39 printf("[Scheduler::JobAdd] completed UNEXPECTEDLY\n");
sgnezdov 0:806403f3d0d1 40 return new JobRes(NoError, 0);
sgnezdov 0:806403f3d0d1 41 }
sgnezdov 0:806403f3d0d1 42
sgnezdov 0:806403f3d0d1 43 void Scheduler::JobRemove(int jobID) {
sgnezdov 0:806403f3d0d1 44 }
sgnezdov 0:806403f3d0d1 45
sgnezdov 0:806403f3d0d1 46 void Scheduler::updateHandler() {
sgnezdov 0:806403f3d0d1 47 while (!_quit) {
sgnezdov 0:806403f3d0d1 48 printf("[Scheduler::updateHandler] waiting for action\n");
sgnezdov 0:806403f3d0d1 49 // wait forever ...
sgnezdov 0:806403f3d0d1 50 osEvent evt = _actions.get();
sgnezdov 0:806403f3d0d1 51 if (evt.status == osEventMessage) {
sgnezdov 0:806403f3d0d1 52 printf("[Scheduler::updateHandler] process action\n");
sgnezdov 0:806403f3d0d1 53 this->process((Action*)evt.value.p);
sgnezdov 0:806403f3d0d1 54 } else {
sgnezdov 0:806403f3d0d1 55 printf("[Scheduler::updateHandler] NOT osEventMessage\n");
sgnezdov 0:806403f3d0d1 56 }
sgnezdov 0:806403f3d0d1 57 wait(2);
sgnezdov 0:806403f3d0d1 58 }
sgnezdov 0:806403f3d0d1 59 }
sgnezdov 0:806403f3d0d1 60
sgnezdov 0:806403f3d0d1 61 void Scheduler::process(Action *action)
sgnezdov 0:806403f3d0d1 62 {
sgnezdov 0:806403f3d0d1 63 JobRes *jr = new JobRes(NoError, 0);
sgnezdov 0:806403f3d0d1 64 action->response.put(jr);
sgnezdov 0:806403f3d0d1 65 }
sgnezdov 0:806403f3d0d1 66
sgnezdov 0:806403f3d0d1 67 }