|
| 1 | +#include "state_types.h" |
| 2 | + |
| 3 | +void NewState::start(ThreadContext *context) |
| 4 | +{ |
| 5 | + std::cout << "call NewState::start()" << std::endl; |
| 6 | + if (stateName == ::NEW) |
| 7 | + { |
| 8 | + if (context) |
| 9 | + context->changeState(new ReadyState()); |
| 10 | + } |
| 11 | + else |
| 12 | + { |
| 13 | + std::cout << "Cannot call NewState::start() since current state is not [NEW]" << std::endl; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +void ReadyState::acquire(ThreadContext *context) |
| 18 | +{ |
| 19 | + std::cout << "call ReadyState::acquire()" << std::endl; |
| 20 | + if (stateName == ::READY) |
| 21 | + { |
| 22 | + if (context) |
| 23 | + context->changeState(new RunningState()); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + std::cout << "Cannot call ReadyState::acquire() since current state is not [READY]" << std::endl; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void RunningState::suspend(ThreadContext *context) |
| 32 | +{ |
| 33 | + std::cout << "call RunningState::suspend()" << std::endl; |
| 34 | + if (stateName == ::RUNNING) |
| 35 | + { |
| 36 | + if (context) |
| 37 | + context->changeState(new BlockedState()); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + std::cout << "Cannot call RunningState::suspend() since current state is not [RUNNING]" << std::endl; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void RunningState::stop(ThreadContext *context) |
| 46 | +{ |
| 47 | + std::cout << "call RunningState::stop()" << std::endl; |
| 48 | + if (stateName == ::RUNNING) |
| 49 | + { |
| 50 | + if (context) |
| 51 | + context->changeState(new DeadState()); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + std::cout << "Cannot call RunningState::stop() since current state is not [RUNNING]" << std::endl; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void BlockedState::resume(ThreadContext *context) |
| 60 | +{ |
| 61 | + std::cout << "call BlockedState::resume()" << std::endl; |
| 62 | + if (stateName == ::BLOCKED) |
| 63 | + { |
| 64 | + if (context) |
| 65 | + context->changeState(new ReadyState()); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + std::cout << "Cannot call BlockedState::resume() since current state is not [BLOCKED]" << std::endl; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void ThreadContext::changeState(ThreadState *state) |
| 74 | +{ |
| 75 | + if (this->state) |
| 76 | + { |
| 77 | + delete this->state; |
| 78 | + } |
| 79 | + this->state = state; |
| 80 | +} |
| 81 | + |
| 82 | +ThreadState *ThreadContext::getState() |
| 83 | +{ |
| 84 | + return state; |
| 85 | +} |
| 86 | + |
| 87 | +void ThreadContext::start() |
| 88 | +{ |
| 89 | + if (state && state->getStateName() == ::NEW) |
| 90 | + { |
| 91 | + static_cast<NewState *>(state)->start(this); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void ThreadContext::acquireCPU() |
| 96 | +{ |
| 97 | + if (state && state->getStateName() == ::READY) |
| 98 | + { |
| 99 | + static_cast<ReadyState *>(state)->acquire(this); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void ThreadContext::suspend() |
| 104 | +{ |
| 105 | + if (state && state->getStateName() == ::RUNNING) |
| 106 | + { |
| 107 | + static_cast<RunningState *>(state)->suspend(this); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +void ThreadContext::stop() |
| 112 | +{ |
| 113 | + if (state && state->getStateName() == ::RUNNING) |
| 114 | + { |
| 115 | + static_cast<RunningState *>(state)->stop(this); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void ThreadContext::resume() |
| 120 | +{ |
| 121 | + if (state && state->getStateName() == ::BLOCKED) |
| 122 | + { |
| 123 | + static_cast<BlockedState *>(state)->resume(this); |
| 124 | + } |
| 125 | +} |
0 commit comments