Browse Source

Use enum class, and put classes in CodeDweller namespace.


git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@103 d34b734f-a00e-4b39-a726-e4eeb87269ab
adeniz_1
adeniz 9 years ago
parent
commit
9acdc5bd8b
2 changed files with 19 additions and 11 deletions
  1. 13
    8
      multitasking.cpp
  2. 6
    3
      multitasking.hpp

+ 13
- 8
multitasking.cpp View File



#include "multitasking.hpp" #include "multitasking.hpp"


namespace CodeDweller {

void MultiTasker::processBatch(size_t n, MultiTaskQueue& q, bool StopOnWait) { void MultiTasker::processBatch(size_t n, MultiTaskQueue& q, bool StopOnWait) {
size_t batchSize = n; size_t batchSize = n;
while(batchSize--) { while(batchSize--) {
MultiTask& t = q.get(); MultiTask& t = q.get();
switch(t.task()) { switch(t.task()) {
case Running: { working.put(t); break; }
case Waiting: { waiting.put(t); if(StopOnWait) return; break; }
case Finished: { finished.put(t); break; }
case State::Running: { working.put(t); break; }
case State::Waiting: { waiting.put(t); if(StopOnWait) return; break; }
case State::Finished: { finished.put(t); break; }
} }
} }
} }


MultiTaskState MultiTasker::task() {
MultiTask::State MultiTasker::task() {
const bool StopOnFirstWaitingTask = true;
size_t workingBatchSize = working.size(); size_t workingBatchSize = working.size();
size_t waitingBatchSize = waiting.size(); size_t waitingBatchSize = waiting.size();
processBatch(workingBatchSize, working); processBatch(workingBatchSize, working);
processBatch(waitingBatchSize, waiting, true);
processBatch(waitingBatchSize, waiting, StopOnFirstWaitingTask);

if(false == working.empty()) { return State::Running; }
else if (false == waiting.empty()) { return State::Waiting; }
else { return State::Finished; }
}


if(false == working.empty()) { return Running; }
else if (false == waiting.empty()) { return Waiting; }
else { return Finished; }
} }

+ 6
- 3
multitasking.hpp View File

#include <cstddef> #include <cstddef>
#include <queue> #include <queue>


enum MultiTaskState {Running, Waiting, Finished};
namespace CodeDweller {


class MultiTask { class MultiTask {
public: public:
virtual MultiTaskState task() = 0;
enum class State{Running, Waiting, Finished};
virtual State task() = 0;
}; };


class MultiTaskQueue : public std::queue<MultiTask*> { class MultiTaskQueue : public std::queue<MultiTask*> {
MultiTaskQueue working; MultiTaskQueue working;
MultiTaskQueue waiting; MultiTaskQueue waiting;
MultiTaskQueue finished; MultiTaskQueue finished;
MultiTaskState task();
State task();


bool isReady() { return (false == working.empty()); } bool isReady() { return (false == working.empty()); }
bool isWaiting() { return (false == waiting.empty() && true == working.empty()); } bool isWaiting() { return (false == waiting.empty() && true == working.empty()); }
bool canGet() { return (false == finished.empty()); } bool canGet() { return (false == finished.empty()); }
}; };


}

#endif // M_MultiTasking #endif // M_MultiTasking

Loading…
Cancel
Save