--- nono/vm/thread.h 2026/04/29 17:05:50 1.1.1.5 +++ nono/vm/thread.h 2026/04/29 17:05:54 1.1.1.6 @@ -11,8 +11,32 @@ #pragma once #include "device.h" +#include "fixedqueue.h" #include #include +#include + +// スレッド情報。 +struct ThreadInfo +{ + static constexpr uint Capacity = 60; + using LoadQueue = FixedQueue; + + Object *obj {}; + // 表示するスレッド名 + std::string name {}; + // CPU 利用率を取得するための ID + clockid_t clockid {}; + + std::unique_ptr load {}; + uint64 last_clock {}; + + ThreadInfo(Object *obj_, const std::string& name_, clockid_t clockid_) + : obj(obj_), name(name_), clockid(clockid_) + { + load.reset(new LoadQueue()); + } +}; class ThreadBase { @@ -22,8 +46,11 @@ class ThreadBase Heavy, // 処理の重いスレッド }; - // このスレッドのアフィニティを示唆する + // このスレッドのアフィニティを設定する static void SetThreadAffinityHint(AffinityClass hint); + + // 排他実行用 + static std::mutex exlock; }; class ThreadDevice : public Device, public ThreadBase @@ -47,7 +74,10 @@ class ThreadDevice : public Device, publ // スレッド名を設定する void SetThreadName(const std::string& threadname_); - // スレッドエントリポイント + // 開始されたスレッドでのエントリポイント + void OnStart(); + + // 派生先で用意するエントリポイント virtual void ThreadRun() = 0; // スレッドに終了を指示する @@ -61,3 +91,35 @@ class ThreadDevice : public Device, publ // スレッド名 std::string threadname {}; }; + +// +// スレッドマネージャ +// +class ThreadManager : public Object +{ + using inherited = Object; + friend class WXThreadMonitorPanel; + + public: + ThreadManager(); + ~ThreadManager() override; + + // スレッドリストに登録する。 + void RegistThread(Object *, const std::string& name); + // スレッドリストから削除する。 + void UnregistThread(Object *); + + // パフォーマンス測定。 + void CalcPerf(uint64 rtime); + + // x86 CPU のアフィニティを調べる。 + static bool DetectCPUAffinity_x86(std::vector&); + + private: + // スレッド一覧。 + std::vector threads {}; + std::mutex threads_mutex {}; + + // 前回のパフォーマンス測定時刻。 + uint64 last_rtime {}; +};