Annotation of nono/vm/scheduler.h, revision 1.1.1.11

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.3   root       13: #include "event.h"
1.1.1.11! root       14: #include "message.h"
1.1.1.9   root       15: #include "monitor.h"
1.1.1.11! root       16: #include "spscqueue.h"
        !            17: #include "thread.h"
        !            18: #include <array>
1.1       root       19: #include <condition_variable>
                     20: #include <mutex>
                     21: 
                     22: // スケジューラ
1.1.1.11! root       23: class Scheduler : public ThreadDevice
1.1       root       24: {
1.1.1.11! root       25:        using inherited = ThreadDevice;
1.1.1.7   root       26: 
                     27:  private:
1.1       root       28:        // リクエストフラグ
1.1.1.11! root       29:        static const uint32 REQUEST_EXIT        = 0x80000000;   // 終了要求
        !            30:        static const uint32 REQUEST_MESSAGE = 0x00000001;       // メッセージ要求
        !            31: 
        !            32:        // メッセージハンドラを覚えておくための構造体
        !            33:        struct MessageHandler
        !            34:        {
        !            35:                Device *dev {};
        !            36:                MessageCallback_t func {};
        !            37:        };
1.1       root       38: 
                     39:  public:
                     40:        Scheduler();
1.1.1.6   root       41:        virtual ~Scheduler() override;
1.1       root       42: 
1.1.1.3   root       43:        bool Init() override;
1.1       root       44: 
1.1.1.11! root       45:        void StartTime();
1.1.1.9   root       46: 
1.1.1.11! root       47:        // スレッド終了指示
        !            48:        void Terminate() override;
1.1       root       49: 
1.1.1.11! root       50:        // 現在のマスター仮想時間を返す
        !            51:        uint64 GetVirtTime() const { return vtime; }
1.1.1.8   root       52: 
1.1.1.11! root       53:        // イベントを登録する
        !            54:        void RegistEvent(Event& event);
1.1.1.8   root       55: 
1.1.1.2   root       56:        // イベントを開始する
1.1.1.11! root       57:        void StartEvent(Event& ev);
        !            58:        void RestartEvent(Event& ev);
1.1       root       59: 
1.1.1.11! root       60:        // 実時間間隔を指定してイベントを開始する。
        !            61:        // rt_now はイベント発行者の実時間での現在時刻で、
        !            62:        // rt_period は次回のイベントまでの実時間間隔。
        !            63:        void StartRealtimeEvent(Event& event, uint64 rt_now, uint64 rt_period);
1.1.1.9   root       64: 
1.1.1.11! root       65:        // イベントを停止する
        !            66:        void StopEvent(Event& event);
1.1.1.7   root       67: 
1.1.1.11! root       68:        // メッセージハンドラを登録する
        !            69:        void ConnectMessage(MessageID, Device *, MessageCallback_t);
1.1.1.7   root       70: 
1.1.1.11! root       71:        // メッセージを送る。VM スレッド以外からも呼び出して良い。
        !            72:        void SendMessage(MessageID, uint32 arg = 0);
1.1.1.8   root       73: 
1.1.1.11! root       74:        // 指定時間が経過するか、リクエストが起きるまでスリープ
        !            75:        void Sleep(uint64 time);
1.1.1.8   root       76: 
1.1.1.11! root       77:  private:
        !            78:        // スレッド
        !            79:        void ThreadRun() override;
1.1       root       80: 
1.1.1.11! root       81:        void EnqueueSlow(Event& ev);
        !            82:        void PushSlow(Event& ev);
        !            83:        void StopSlowEvent(Event& ev);
1.1.1.8   root       84: 
1.1.1.11! root       85:        // メッセージディスパッチ
        !            86:        void DispatchMessage();
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:        // 外部スレッドからのリクエスト
        !           100:        int request {};
        !           101:        std::mutex mtx {};
        !           102:        std::condition_variable cv {};
1.1.1.8   root      103: 
1.1.1.11! root      104:        // 全イベントのリスト (所有はしていない)
        !           105:        std::vector<Event*> all_events {};
1.1.1.8   root      106: 
1.1.1.11! root      107:        // メッセージハンドラリスト
        !           108:        std::array<MessageHandler, MessageID::MAX> message_handlers {};
1.1.1.8   root      109: 
1.1.1.11! root      110:        // メッセージキュー
        !           111:        SPSCQueue<uint64, 16> msgq {};
1.1.1.9   root      112: 
                    113:        Monitor monitor { this };
1.1       root      114: };
                    115: 
1.1.1.9   root      116: extern const std::string TimeToStr(uint64 t);
1.1.1.11! root      117: extern const std::string SecToStr(uint64 t);
1.1.1.9   root      118: 
1.1.1.11! root      119: extern Scheduler *gScheduler;

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.