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

Dependencies:   LinkedList

Dependents:   JobSchedulerDemo Borsch

scheduler.cpp

Committer:
sgnezdov
Date:
2017-07-11
Revision:
1:ec6a1d054065
Parent:
0:806403f3d0d1
Child:
2:9bf5366ad5a2

File content as of revision 1:ec6a1d054065:

#include "scheduler.h"

void update(void *target) {
};

namespace JobScheduler {

    struct JobAddReq: Action {
        JobTypeID jobTID;
        Response<JobID> response;
        JobAddReq(JobTypeID typeID) : Action(JobAddAT), jobTID(typeID), response(NoError, 0) {}
    };
    
    Scheduler::Scheduler(JobService *jobService) 
    : _jobService(jobService)  {   }

    void Scheduler::updateAdapter(void *thisPointer) {
        Scheduler *self = static_cast<Scheduler*>(thisPointer);
        self->updateHandler();
    }
    
    void Scheduler::Start() {
        _updater.start(callback(Scheduler::updateAdapter, this));
    }
    
    void Scheduler::Stop() {
        // it is not thread-safe, but impact is non-existent.
        _quit = true;
    }
    
    void Scheduler::WaitToStop() {
        _updater.join();
    }
    
    Response<JobID> Scheduler::JobAdd(JobTypeID jobTID) {
        JobAddReq req(jobTID);
        _actions.put(&req);
        // default is wait forever
        osEvent evt = req.resQueue.get();
        if (evt.status == osEventMessage) {
            printf("[Scheduler::JobAdd] completed ok\n");
            //return (JobRes*)evt.value.p;
        }
        return req.response;
    }
    
    void Scheduler::JobRemove(int jobID) {
    }
    
    void Scheduler::updateHandler() {
        while (!_quit) {
            printf("[Scheduler::updateHandler] waiting for action\n");
            // wait forever ...
            osEvent evt = _actions.get();
            if (evt.status == osEventMessage) {
                printf("[Scheduler::updateHandler] process action\n");
                this->process((Action*)evt.value.p);
            } else {
                printf("[Scheduler::updateHandler] NOT osEventMessage\n");
            }
            wait(2);
        }
    }

    void Scheduler::process(Action *action)
    {
        switch(action->type) {
            case JobAddAT: {
                JobAddReq *req = static_cast<JobAddReq*>(action);
                action->resQueue.put(&req->response);
                break;
            }
            default:
                printf("[Scheduler::process] unknown action type\n");
                action->resQueue.put(NULL);
        }
    }
        
}