|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2021 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // スレッドを持つデバイス
9: //
10:
11: #pragma once
12:
13: #include "device.h"
1.1.1.6 ! root 14: #include "fixedqueue.h"
1.1 root 15: #include <mutex>
16: #include <thread>
1.1.1.6 ! root 17: #include <vector>
! 18:
! 19: // スレッド情報。
! 20: struct ThreadInfo
! 21: {
! 22: static constexpr uint Capacity = 60;
! 23: using LoadQueue = FixedQueue<uint32, Capacity>;
! 24:
! 25: Object *obj {};
! 26: // 表示するスレッド名
! 27: std::string name {};
! 28: // CPU 利用率を取得するための ID
! 29: clockid_t clockid {};
! 30:
! 31: std::unique_ptr<LoadQueue> load {};
! 32: uint64 last_clock {};
! 33:
! 34: ThreadInfo(Object *obj_, const std::string& name_, clockid_t clockid_)
! 35: : obj(obj_), name(name_), clockid(clockid_)
! 36: {
! 37: load.reset(new LoadQueue());
! 38: }
! 39: };
1.1 root 40:
1.1.1.5 root 41: class ThreadBase
1.1 root 42: {
1.1.1.5 root 43: protected:
1.1.1.4 root 44: enum class AffinityClass {
45: Light, // 処理の軽いスレッド
46: Heavy, // 処理の重いスレッド
47: };
48:
1.1.1.6 ! root 49: // このスレッドのアフィニティを設定する
1.1.1.5 root 50: static void SetThreadAffinityHint(AffinityClass hint);
1.1.1.6 ! root 51:
! 52: // 排他実行用
! 53: static std::mutex exlock;
1.1.1.5 root 54: };
55:
56: class ThreadDevice : public Device, public ThreadBase
57: {
58: using inherited = Device;
59:
1.1.1.4 root 60: public:
61: explicit ThreadDevice(uint objid_);
1.1.1.3 root 62: ~ThreadDevice() override;
1.1 root 63:
64: // スレッドを開始する
65: virtual bool StartThread();
66:
67: // スレッドに終了を要求し、その終了を待つ
68: void TerminateThread();
69:
70: protected:
1.1.1.5 root 71: // オブジェクト名とスレッド名を設定する。
72: void SetName(const std::string& newname) override;
73:
74: // スレッド名を設定する
75: void SetThreadName(const std::string& threadname_);
76:
1.1.1.6 ! root 77: // 開始されたスレッドでのエントリポイント
! 78: void OnStart();
! 79:
! 80: // 派生先で用意するエントリポイント
1.1 root 81: virtual void ThreadRun() = 0;
82:
83: // スレッドに終了を指示する
84: virtual void Terminate() = 0;
85:
86: std::unique_ptr<std::thread> thread {};
87:
88: // スレッド開始同期用
89: std::mutex thread_starter {};
90:
91: // スレッド名
1.1.1.5 root 92: std::string threadname {};
1.1 root 93: };
1.1.1.6 ! root 94:
! 95: //
! 96: // スレッドマネージャ
! 97: //
! 98: class ThreadManager : public Object
! 99: {
! 100: using inherited = Object;
! 101: friend class WXThreadMonitorPanel;
! 102:
! 103: public:
! 104: ThreadManager();
! 105: ~ThreadManager() override;
! 106:
! 107: // スレッドリストに登録する。
! 108: void RegistThread(Object *, const std::string& name);
! 109: // スレッドリストから削除する。
! 110: void UnregistThread(Object *);
! 111:
! 112: // パフォーマンス測定。
! 113: void CalcPerf(uint64 rtime);
! 114:
! 115: // x86 CPU のアフィニティを調べる。
! 116: static bool DetectCPUAffinity_x86(std::vector<bool>&);
! 117:
! 118: private:
! 119: // スレッド一覧。
! 120: std::vector<ThreadInfo> threads {};
! 121: std::mutex threads_mutex {};
! 122:
! 123: // 前回のパフォーマンス測定時刻。
! 124: uint64 last_rtime {};
! 125: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.