--- nono/vm/thread.h 2026/04/29 17:05:24 1.1.1.3 +++ nono/vm/thread.h 2026/04/29 17:05:54 1.1.1.6 @@ -11,19 +11,56 @@ #pragma once #include "device.h" +#include "fixedqueue.h" #include #include +#include -class ThreadDevice : public Device +// スレッド情報。 +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 +{ + protected: + enum class AffinityClass { + Light, // 処理の軽いスレッド + Heavy, // 処理の重いスレッド + }; + + // このスレッドのアフィニティを設定する + static void SetThreadAffinityHint(AffinityClass hint); + + // 排他実行用 + static std::mutex exlock; +}; + +class ThreadDevice : public Device, public ThreadBase { using inherited = Device; + public: - ThreadDevice(int objid_); + explicit ThreadDevice(uint objid_); ~ThreadDevice() override; - // スレッド名を設定する - void SetThreadName(const char *threadname_); - // スレッドを開始する virtual bool StartThread(); @@ -31,7 +68,16 @@ class ThreadDevice : public Device void TerminateThread(); protected: - // スレッドエントリポイント + // オブジェクト名とスレッド名を設定する。 + void SetName(const std::string& newname) override; + + // スレッド名を設定する + void SetThreadName(const std::string& threadname_); + + // 開始されたスレッドでのエントリポイント + void OnStart(); + + // 派生先で用意するエントリポイント virtual void ThreadRun() = 0; // スレッドに終了を指示する @@ -43,5 +89,37 @@ class ThreadDevice : public Device std::mutex thread_starter {}; // スレッド名 - const char *threadname {}; + 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 {}; };