MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / TaskFsm

Class TaskFsm

src/task/include/task_fsm.hpp:154–220  ·  view source on GitHub ↗

* @brief 任务有限状态机 */

Source from the content-addressed store, hash-verified

152 * @brief 任务有限状态机
153 */
154class TaskFsm {
155 public:
156 /**
157 * @brief 启动 FSM(在 TCB 完全构造后调用)
158 */
159 auto Start() -> void {
160 fsm_.start();
161 cached_state_.store(fsm_.get_state_id(), std::memory_order_release);
162 }
163
164 /**
165 * @brief 向 FSM 发送消息
166 * @param msg 要发送的消息
167 * @pre 调用者持有保护此 TCB 的锁(如 cpu_sched.lock)
168 * @post cached_state_ 以 release 语义更新,跨核可见
169 */
170 auto Receive(const etl::imessage& msg) -> void {
171 fsm_.receive(msg);
172 cached_state_.store(fsm_.get_state_id(), std::memory_order_release);
173 }
174
175 /**
176 * @brief 获取当前状态 ID(线程安全)
177 * @return etl::fsm_state_id_t 当前状态 ID
178 * @note 使用 acquire 语义从 atomic 缓存读取,
179 * 无需持有与 Receive() 相同的锁即可安全调用
180 */
181 [[nodiscard]] auto GetStateId() const -> etl::fsm_state_id_t {
182 return cached_state_.load(std::memory_order_acquire);
183 }
184
185 /// @name 构造/析构函数
186 /// @{
187 TaskFsm() : fsm_(router_id::kTaskFsm) {
188 state_list_[0] = &state_uninit_;
189 state_list_[1] = &state_ready_;
190 state_list_[2] = &state_running_;
191 state_list_[3] = &state_sleeping_;
192 state_list_[4] = &state_blocked_;
193 state_list_[5] = &state_exited_;
194 state_list_[6] = &state_zombie_;
195 fsm_.set_states(state_list_, 7);
196 }
197
198 TaskFsm(const TaskFsm&) = delete;
199 TaskFsm(TaskFsm&&) = delete;
200 auto operator=(const TaskFsm&) -> TaskFsm& = delete;
201 auto operator=(TaskFsm&&) -> TaskFsm& = delete;
202 ~TaskFsm() = default;
203 /// @}
204
205 private:
206 StateUnInit state_uninit_;
207 StateReady state_ready_;
208 StateRunning state_running_;
209 StateSleeping state_sleeping_;
210 StateBlocked state_blocked_;
211 StateExited state_exited_;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected