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

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       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.11  root       55:        // イベントを登録する
1.1.1.13! root       56:        void RegistEvent(Event& ev);
1.1.1.8   root       57: 
1.1.1.2   root       58:        // イベントを開始する
1.1.1.11  root       59:        void StartEvent(Event& ev);
                     60:        void RestartEvent(Event& ev);
1.1       root       61: 
1.1.1.11  root       62:        // 実時間間隔を指定してイベントを開始する。
                     63:        // rt_now はイベント発行者の実時間での現在時刻で、
                     64:        // rt_period は次回のイベントまでの実時間間隔。
1.1.1.13! root       65:        void StartRealtimeEvent(Event& ev, uint64 rt_now, uint64 rt_period);
1.1.1.9   root       66: 
1.1.1.11  root       67:        // イベントを停止する
1.1.1.13! root       68:        void StopEvent(Event& ev);
1.1.1.7   root       69: 
1.1.1.11  root       70:        // メッセージハンドラを登録する
                     71:        void ConnectMessage(MessageID, Device *, MessageCallback_t);
1.1.1.7   root       72: 
1.1.1.11  root       73:        // メッセージを送る。VM スレッド以外からも呼び出して良い。
                     74:        void SendMessage(MessageID, uint32 arg = 0);
1.1.1.8   root       75: 
1.1.1.11  root       76:        // 指定時間が経過するか、リクエストが起きるまでスリープ
                     77:        void Sleep(uint64 time);
1.1.1.8   root       78: 
1.1.1.11  root       79:  private:
                     80:        // スレッド
                     81:        void ThreadRun() override;
1.1       root       82: 
1.1.1.11  root       83:        void EnqueueSlow(Event& ev);
                     84:        void PushSlow(Event& ev);
                     85:        void StopSlowEvent(Event& ev);
1.1.1.8   root       86: 
1.1.1.11  root       87:        // メッセージディスパッチ
                     88:        void DispatchMessage();
1.1.1.12  root       89:        void InvokeMessage(MessageID, uint32);
1.1.1.8   root       90: 
1.1.1.11  root       91:        DECLARE_MONITOR_CALLBACK(MonitorUpdate);
1.1.1.8   root       92: 
1.1.1.11  root       93:        // 有効なイベントキュー
                     94:        uint64 slow_top_vtime {};
                     95:        Event *fast {};
                     96:        int slow_top {};
                     97:        std::array<Event *, 64> slow {};
                     98: 
                     99:        // 現在の仮想経過時間
                    100:        uint64 vtime {};
                    101: 
                    102:        // 外部スレッドからのリクエスト
1.1.1.12  root      103:        uint32 request {};
1.1.1.11  root      104:        std::mutex mtx {};
                    105:        std::condition_variable cv {};
1.1.1.8   root      106: 
1.1.1.11  root      107:        // 全イベントのリスト (所有はしていない)
                    108:        std::vector<Event*> all_events {};
1.1.1.8   root      109: 
1.1.1.11  root      110:        // メッセージハンドラリスト
                    111:        std::array<MessageHandler, MessageID::MAX> message_handlers {};
1.1.1.8   root      112: 
1.1.1.11  root      113:        // メッセージキュー
                    114:        SPSCQueue<uint64, 16> msgq {};
1.1.1.9   root      115: 
1.1.1.12  root      116:        Syncer *syncer {};
                    117: 
1.1.1.9   root      118:        Monitor monitor { this };
1.1       root      119: };
                    120: 
1.1.1.9   root      121: extern const std::string TimeToStr(uint64 t);
1.1.1.11  root      122: extern const std::string SecToStr(uint64 t);
1.1.1.9   root      123: 
1.1.1.12  root      124: static inline Scheduler *GetScheduler() {
                    125:        return Object::GetObject<Scheduler>(OBJ_SCHEDULER);
                    126: }

unix.superglobalmegacorp.com

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