--- nono/vm/scheduler.h 2026/04/29 17:04:45 1.1.1.6 +++ nono/vm/scheduler.h 2026/04/29 17:05:11 1.1.1.11 @@ -4,48 +4,37 @@ // Licensed under nono-license.txt // +// +// スケジューラ +// + #pragma once -#include "device.h" #include "event.h" -#include "mpu.h" -#include +#include "message.h" +#include "monitor.h" +#include "spscqueue.h" +#include "thread.h" +#include #include -#include #include -#include // スケジューラ -class Scheduler : public Device +class Scheduler : public ThreadDevice { - using inherited = Device; + using inherited = ThreadDevice; + private: // リクエストフラグ - static const uint REQ_SYNC = 0x0001; // スケジューラを同期モードに - static const uint REQ_FAST = 0x0002; // スケジューラを高速モードに - static const uint REQ_RUN = 0x0004; // MPU が通常状態になった - static const uint REQ_STOP = 0x0008; // MPU が STOP 状態になった - static const uint REQ_MODEMASK = 0x000f; // mode マスク - static const uint REQ_POWER_ON = 0x0010; // 電源オン - static const uint REQ_POWER_OFF = 0x0020; // 電源オフ - static const uint REQ_EXIT = 0x0040; // アプリケーション終了 - static const uint REQ_RESET = 0x0080; // ハードウェアリセット - - // 動作モード - // 動作モード変数 (mode) は高速モードか通常モードかと、MPU が通常状態が - // STOP 状態かを同時に保持している。高速走行中であっても MPU が STOP 状態 - // なら通常走行するというロジックを比較1回で済ませるため。 - static const uint32 SCHED_STOP = 0x0001; // スケジューラが STOP 状態 - static const uint32 SCHED_SYNC = 0x0002; // 同期(%1)/高速(%0)モード - - // 演算を1回で済ませるために、この2値は同じ値にすること。 - // ただしこれをチェックするために全員が m68030.h/m88100.h を include すると - // m88k を修正するたびに m68k も含めて全部ビルドが走るのはかなりつらいので - // 定義されてる時だけチェックすることにする。 - // もともと将来のポカよけなのでビルド中1回でも通過すれば十分。 -#if defined(CPU_REQ_STOP) - static_assert(SCHED_STOP == CPU_REQ_STOP, "must be the same"); -#endif + static const uint32 REQUEST_EXIT = 0x80000000; // 終了要求 + static const uint32 REQUEST_MESSAGE = 0x00000001; // メッセージ要求 + + // メッセージハンドラを覚えておくための構造体 + struct MessageHandler + { + Device *dev {}; + MessageCallback_t func {}; + }; public: Scheduler(); @@ -53,77 +42,78 @@ class Scheduler : public Device bool Init() override; - nnSize GetMonitorSize() override; - void MonitorUpdate(TextScreen&) override; + void StartTime(); - // スレッド開始 - void ThreadRun(); + // スレッド終了指示 + void Terminate() override; - // スレッド終了 - void Terminate(); + // 現在のマスター仮想時間を返す + uint64 GetVirtTime() const { return vtime; } - // スケジューラを開始 (電源オン時に vm から呼ばれる) - void Run(); + // イベントを登録する + void RegistEvent(Event& event); - // 電源オフ要求 - void RequestPowerOff() { - atomic_reqflag |= REQ_POWER_OFF; - } + // イベントを開始する + void StartEvent(Event& ev); + void RestartEvent(Event& ev); - // リセット要求 - void RequestResetHard() { - atomic_reqflag |= REQ_RESET; - } + // 実時間間隔を指定してイベントを開始する。 + // rt_now はイベント発行者の実時間での現在時刻で、 + // rt_period は次回のイベントまでの実時間間隔。 + void StartRealtimeEvent(Event& event, uint64 rt_now, uint64 rt_period); + // イベントを停止する + void StopEvent(Event& event); - // スケジューラが高速モードなら true を返す。 - // ここでいう高速モードは STOP 状態中でも高速モード。 - bool GetFullSpeed() const { return ((mode & SCHED_SYNC) == 0); } + // メッセージハンドラを登録する + void ConnectMessage(MessageID, Device *, MessageCallback_t); - // スケジューラの動作モードを設定する。 - // true なら高速モード、false なら同期モード。 - void SetFullSpeed(bool enable); + // メッセージを送る。VM スレッド以外からも呼び出して良い。 + void SendMessage(MessageID, uint32 arg = 0); - // イベントを開始する - void StartEvent(Event *newev); - // イベントを停止する - void StopEvent(Event *event); + // 指定時間が経過するか、リクエストが起きるまでスリープ + void Sleep(uint64 time); - std::list eventlist {}; // 有効なイベントリスト - std::mutex evcs {}; // イベントリストのロック + private: + // スレッド + void ThreadRun() override; - // 状態変更要求 - // スケジューラループの状態を変更したい場合はこのフラグを立てる。 - // REQ_MODE_* の相反するビット以外は同時に複数立ててもいいはず。 - // 処理されると再び 0 になる。 - std::atomic atomic_reqflag {}; + void EnqueueSlow(Event& ev); + void PushSlow(Event& ev); + void StopSlowEvent(Event& ev); + + // メッセージディスパッチ + void DispatchMessage(); + + DECLARE_MONITOR_CALLBACK(MonitorUpdate); + + // 有効なイベントキュー + uint64 slow_top_vtime {}; + Event *fast {}; + int slow_top {}; + std::array slow {}; + + // 現在の仮想経過時間 + uint64 vtime {}; + + // 外部スレッドからのリクエスト + int request {}; + std::mutex mtx {}; std::condition_variable cv {}; - std::mutex cvmtx {}; - private: - // リセットイベントコールバック - void ResetCallback(Event& ev); - - pthread_t thread {}; - bool thread_created {}; // スレッドが作成されたら true + // 全イベントのリスト (所有はしていない) + std::vector all_events {}; - // スケジューラの動作モード。SCHED_* - uint32 mode {}; + // メッセージハンドラリスト + std::array message_handlers {}; - uint64 rtimestart {}; // スケジューラ開始実時刻 - uint64 rtimebase {}; // 実時間の基準時刻 - uint64 vtimebase {}; // 仮想時間の基準時刻 + // メッセージキュー + SPSCQueue msgq {}; - // 前回 RTC へ 32Hz 入力パルスを入れた時刻 (実時間の絶対時刻系) - uint64 rtc_last_clock {}; + Monitor monitor { this }; }; -extern std::unique_ptr gScheduler; +extern const std::string TimeToStr(uint64 t); +extern const std::string SecToStr(uint64 t); -// 現在時刻 [nsec] を取得する -static inline uint64 GetRealTime() -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return (uint64)(tv.tv_sec * 1000 * 1000 + tv.tv_usec) * 1000; -} +extern Scheduler *gScheduler;