--- nono/vm/scheduler.h 2026/04/29 17:04:50 1.1.1.7 +++ nono/vm/scheduler.h 2026/04/29 17:05:25 1.1.1.13 @@ -4,150 +4,123 @@ // Licensed under nono-license.txt // +// +// スケジューラ +// + #pragma once -#include "device.h" +#include "thread.h" #include "event.h" -#include "mpu.h" -#include +#include "message.h" +#include "monitor.h" +#include "spscqueue.h" +#include #include -#include #include -#include + +class Syncer; // スケジューラ -class Scheduler : public Device +class Scheduler : public ThreadDevice { - using inherited = Device; - - public: - // 動作モード - // 動作モード変数 (mode) はスケジューラの走行状態を表している。以下の - // ビットがすべて %0 なら高速モード、どれかでも %1 なら通常モードになる。 - // - // | b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 | - // mode | |SYNC| CPUSTAT | - // | +---+---- CPU の状態 - // +-------------- 高速モード抑制 - // - // bit1,0 (CPUSTAT) は CPU の状態を示す。%00 の時だけ高速走行が可能。 - // これは MPUDevice::Run() の戻り値でもある。 - // b1 b0 - // 0 0 通常状態 - // 0 1 STOP 状態 (m88k なら擬似 STOP 状態) - // 1 0 HALT 状態 (ダブルバスフォールト) - // 1 1 HALT 状態だがこちらのビットパターンは使わない - // - // bit2 (SYNC) が UI からの高速モード指示。%0 なら高速、%1 なら通常。 - // なのでどっちかというと高速モード抑制指示というほうがいいかも。 - - static const uint32 SCHED_CPU_STOP = 0x0001; // CPU が STOP 状態 - static const uint32 SCHED_CPU_HALT = 0x0002; // CPU が HALT 状態 - static const uint32 SCHED_SYNC = 0x0004; // 高速モード抑制指示(UI) - - // 便宜上用意しておく - static const uint32 SCHED_CPU_NORMAL= 0x0000; // CPU が通常状態 - static const uint32 SCHED_CPU_MASK = (SCHED_CPU_STOP | SCHED_CPU_HALT); + using inherited = ThreadDevice; private: // リクエストフラグ - // REQ_CPU_MODE は CPU mode が何らか変化することのフラグ。どの状態に変化 - // するかは new_cpumode 変数で示す。1回のループ期間中に複数回の変化が - // 起きても最終状態だけに変化するため。 - // REQ_USER_MODE と new_usermode も同様。 - static const uint REQ_CPU_MODE = 0x0001; // CPU 状態を変更 - static const uint REQ_USER_MODE = 0x0002; // 同期/高速モード変更 - 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; // ハードウェアリセット + static const uint32 REQUEST_EXIT = 0x00000001; // 終了要求 + static const uint32 REQUEST_MESSAGE = 1U << MessageID::MAX_REQUEST; + + // メッセージハンドラを覚えておくための構造体 + struct MessageHandler + { + Device *dev {}; + MessageCallback_t func {}; + }; public: Scheduler(); - virtual ~Scheduler() override; + ~Scheduler() override; bool Init() override; - nnSize GetMonitorSize() override; - void MonitorUpdate(TextScreen&) override; - - // スレッド開始 - void ThreadRun(); + void StartTime(); - // スレッド終了 - void Terminate(); + // スレッド終了指示 + void Terminate() override; - // スケジューラを開始 (電源オン時に vm から呼ばれる) - void Run(); + // 現在のマスター仮想時間を返す + uint64 GetVirtTime() const { return vtime; } - // 電源オフ要求 - void RequestPowerOff() { - atomic_reqflag |= REQ_POWER_OFF; - } + // イベントを登録する + void RegistEvent(Event& ev); - // リセット要求 - void RequestResetHard() { - atomic_reqflag |= REQ_RESET; - } - - - // スケジューラが高速モードなら true を返す。 - // ここでいう高速モードは STOP 状態中でも高速モード。 - bool GetFullSpeed() const { return ((mode & SCHED_SYNC) == 0); } + // イベントを開始する + void StartEvent(Event& ev); + void RestartEvent(Event& ev); - // スケジューラの動作モードを設定する。 - // true なら高速モード、false なら同期モード。 - void SetFullSpeed(bool enable); + // 実時間間隔を指定してイベントを開始する。 + // rt_now はイベント発行者の実時間での現在時刻で、 + // rt_period は次回のイベントまでの実時間間隔。 + void StartRealtimeEvent(Event& ev, uint64 rt_now, uint64 rt_period); - // イベントを開始する - void StartEvent(Event *newev); // イベントを停止する - void StopEvent(Event *event); + void StopEvent(Event& ev); - std::list eventlist {}; // 有効なイベントリスト - std::mutex evcs {}; // イベントリストのロック + // メッセージハンドラを登録する + void ConnectMessage(MessageID, Device *, MessageCallback_t); - // 状態変更要求 - // スケジューラループの状態を変更したい場合はこのフラグを立てる。 - // REQ_MODE_* の相反するビット以外は同時に複数立ててもいいはず。 - // 処理されると再び 0 になる。 - std::atomic atomic_reqflag {}; - std::condition_variable cv {}; - std::mutex cvmtx {}; + // メッセージを送る。VM スレッド以外からも呼び出して良い。 + void SendMessage(MessageID, uint32 arg = 0); - private: - // リセットイベントコールバック - void ResetCallback(Event& ev); + // 指定時間が経過するか、リクエストが起きるまでスリープ + void Sleep(uint64 time); - // スレッド終了を指示する - void RequestExit(); + private: + // スレッド + void ThreadRun() override; - pthread_t thread {}; - bool thread_created {}; // スレッドが作成されたら true + void EnqueueSlow(Event& ev); + void PushSlow(Event& ev); + void StopSlowEvent(Event& ev); + + // メッセージディスパッチ + void DispatchMessage(); + void InvokeMessage(MessageID, uint32); + + DECLARE_MONITOR_CALLBACK(MonitorUpdate); + + // 有効なイベントキュー + uint64 slow_top_vtime {}; + Event *fast {}; + int slow_top {}; + std::array slow {}; + + // 現在の仮想経過時間 + uint64 vtime {}; + + // 外部スレッドからのリクエスト + uint32 request {}; + std::mutex mtx {}; + std::condition_variable cv {}; - // スケジューラの動作モード。SCHED_* - uint32 mode {}; + // 全イベントのリスト (所有はしていない) + std::vector all_events {}; - // REQ_CPU_MODE フラグで変更するモード - uint32 new_cpumode {}; + // メッセージハンドラリスト + std::array message_handlers {}; - // REQ_USER_MODE フラグで変更するモード - uint32 new_usermode {}; + // メッセージキュー + SPSCQueue msgq {}; - uint64 rtimestart {}; // スケジューラ開始実時刻 - uint64 rtimebase {}; // 実時間の基準時刻 - uint64 vtimebase {}; // 仮想時間の基準時刻 + Syncer *syncer {}; - // 前回 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; +static inline Scheduler *GetScheduler() { + return Object::GetObject(OBJ_SCHEDULER); }