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:
0:806403f3d0d1
Child:
1:ec6a1d054065

File content as of revision 0:806403f3d0d1:

#include "scheduler.h"
//#include "core-util/atomic_ops.h"

void update(void *target) {
};

namespace JobScheduler {
    
    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();
    }
    
    JobRes* Scheduler::JobAdd(int typeID) {
        Action action(ActionJobAdd, (void*)typeID);
        _actions.put(&action);
        // default is wait forever
        osEvent evt = action.response.get();
        if (evt.status == osEventMessage) {
            printf("[Scheduler::JobAdd] completed ok\n");
            return (JobRes*)evt.value.p;
        }
        printf("[Scheduler::JobAdd] completed UNEXPECTEDLY\n");
        return new JobRes(NoError, 0);
    }
    
    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)
    {
        JobRes *jr = new JobRes(NoError, 0);
        action->response.put(jr);
    }
        
}