--- nono/vm/thread.cpp 2026/04/29 17:05:28 1.1.1.3 +++ nono/vm/thread.cpp 2026/04/29 17:05:49 1.1.1.5 @@ -16,9 +16,9 @@ ThreadDevice::ThreadDevice(uint objid_) : inherited(objid_) { - // スレッド名はデフォルトでこのオブジェクトと同じ名前にしておく。 - // 継承側コンストラクタ等で SetThreadName() で変更してもよい。 - SetThreadName(GetName().c_str()); + // SetName() はこのクラスでオーバーライドしているので + // ここで(改めて)実行する必要がある。 + SetName(GetName()); } // デストラクタ @@ -28,12 +28,20 @@ ThreadDevice::~ThreadDevice() // 継承側のデストラクタで TerminateThread() をそれぞれ呼ぶこと。 } -// スレッド名を設定する -// (ポインタしか管理しないので実体は呼び出し側で責任を持つこと) +// オブジェクト名を設定(更新)するとともにスレッド名も更新する。 +// Object::SetName() のオーバーロード。 void -ThreadDevice::SetThreadName(const char *threadname_) +ThreadDevice::SetName(const std::string& newname) +{ + inherited::SetName(newname); + SetThreadName(newname); +} + +// スレッドにつけるスレッド名を覚えておく +// (このスレッドにスレッド名を設定する、ではない)。 +void +ThreadDevice::SetThreadName(const std::string& threadname_) { - assert(threadname_); threadname = threadname_; } @@ -41,8 +49,11 @@ ThreadDevice::SetThreadName(const char * bool ThreadDevice::StartThread() { + // ここまでに名前はセットしておくこと。 + assert(threadname.empty() == false); + auto func = [this]() { - PTHREAD_SETNAME(threadname); + PTHREAD_SETNAME(threadname.c_str()); std::lock_guard lock_sub(this->thread_starter); this->ThreadRun(); }; @@ -50,8 +61,12 @@ ThreadDevice::StartThread() // スレッド起動 std::lock_guard lock(thread_starter); - thread.reset(new std::thread(func)); + try { + thread.reset(new std::thread(func)); + } catch (...) { } if ((bool)thread == false) { + warnx("Failed to initialize thread(%s) at %s", + threadname.c_str(), __method__); return false; } return true; @@ -74,8 +89,8 @@ ThreadDevice::TerminateThread() } // このスレッドアフィニティを設定する。 -void -ThreadDevice::SetThreadAffinityHint(AffinityClass hint) +/*static*/ void +ThreadBase::SetThreadAffinityHint(AffinityClass hint) { #if defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(HAVE_MICPUSET) auto aff = gMainApp.cpu_affinity;