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

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

unix.superglobalmegacorp.com

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