|
|
1.1 root 1: //
2: // nono
1.1.1.3 root 3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
1.1 root 5: //
6:
1.1.1.11 root 7: //
8: // スケジューラ
9: //
10:
1.1 root 11: #pragma once
12:
1.1.1.13 root 13: #include "thread.h"
1.1.1.11 root 14: #include "message.h"
15: #include "spscqueue.h"
16: #include <array>
1.1 root 17: #include <condition_variable>
18: #include <mutex>
19:
1.1.1.16! root 20: class EventManager;
1.1.1.12 root 21: class Syncer;
22:
1.1 root 23: // スケジューラ
1.1.1.11 root 24: class Scheduler : public ThreadDevice
1.1 root 25: {
1.1.1.11 root 26: using inherited = ThreadDevice;
1.1.1.7 root 27:
28: private:
1.1 root 29: // リクエストフラグ
1.1.1.12 root 30: static const uint32 REQUEST_EXIT = 0x00000001; // 終了要求
31: static const uint32 REQUEST_MESSAGE = 1U << MessageID::MAX_REQUEST;
1.1.1.11 root 32:
33: // メッセージハンドラを覚えておくための構造体
34: struct MessageHandler
35: {
36: Device *dev {};
37: MessageCallback_t func {};
38: };
1.1 root 39:
40: public:
41: Scheduler();
1.1.1.13 root 42: ~Scheduler() override;
1.1 root 43:
1.1.1.3 root 44: bool Init() override;
1.1.1.14 root 45: bool Init2();
1.1 root 46:
1.1.1.11 root 47: void StartTime();
1.1.1.9 root 48:
1.1.1.11 root 49: // スレッド終了指示
50: void Terminate() override;
1.1 root 51:
1.1.1.11 root 52: // 現在のマスター仮想時間を返す
53: uint64 GetVirtTime() const { return vtime; }
1.1.1.8 root 54:
1.1.1.2 root 55: // イベントを開始する
1.1.1.16! root 56: void StartEvent(Event *ev);
! 57: void RestartEvent(Event *ev);
1.1 root 58:
1.1.1.11 root 59: // 実時間間隔を指定してイベントを開始する。
60: // rt_now はイベント発行者の実時間での現在時刻で、
61: // rt_period は次回のイベントまでの実時間間隔。
1.1.1.16! root 62: void StartRealtimeEvent(Event *ev, uint64 rt_now, uint64 rt_period);
1.1.1.9 root 63:
1.1.1.11 root 64: // イベントを停止する
1.1.1.16! root 65: void StopEvent(Event *ev);
1.1.1.7 root 66:
1.1.1.11 root 67: // メッセージハンドラを登録する
68: void ConnectMessage(MessageID, Device *, MessageCallback_t);
1.1.1.7 root 69:
1.1.1.11 root 70: // メッセージを送る。VM スレッド以外からも呼び出して良い。
71: void SendMessage(MessageID, uint32 arg = 0);
1.1.1.8 root 72:
1.1.1.11 root 73: // 指定時間が経過するか、リクエストが起きるまでスリープ
74: void Sleep(uint64 time);
1.1.1.8 root 75:
1.1.1.11 root 76: private:
77: // スレッド
78: void ThreadRun() override;
1.1 root 79:
1.1.1.16! root 80: void EnqueueSlow(Event *);
! 81: void PushSlow(Event *);
! 82: void StopSlowEvent(Event *);
1.1.1.8 root 83:
1.1.1.11 root 84: // メッセージディスパッチ
85: void DispatchMessage();
1.1.1.12 root 86: void InvokeMessage(MessageID, uint32);
1.1.1.8 root 87:
1.1.1.11 root 88: DECLARE_MONITOR_CALLBACK(MonitorUpdate);
1.1.1.8 root 89:
1.1.1.11 root 90: // 有効なイベントキュー
91: uint64 slow_top_vtime {};
92: Event *fast {};
93: int slow_top {};
94: std::array<Event *, 64> slow {};
95:
96: // 現在の仮想経過時間
97: uint64 vtime {};
98:
99: // 外部スレッドからのリクエスト
1.1.1.12 root 100: uint32 request {};
1.1.1.11 root 101: std::mutex mtx {};
102: std::condition_variable cv {};
1.1.1.8 root 103:
1.1.1.11 root 104: // メッセージハンドラリスト
105: std::array<MessageHandler, MessageID::MAX> message_handlers {};
1.1.1.8 root 106:
1.1.1.11 root 107: // メッセージキュー
108: SPSCQueue<uint64, 16> msgq {};
1.1.1.9 root 109:
1.1.1.16! root 110: EventManager *evman {};
1.1.1.12 root 111: Syncer *syncer {};
112:
1.1.1.15 root 113: Monitor *monitor {};
1.1 root 114: };
115:
1.1.1.12 root 116: static inline Scheduler *GetScheduler() {
117: return Object::GetObject<Scheduler>(OBJ_SCHEDULER);
118: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.