Annotation of nono/vm/thread.cpp, revision 1.1.1.1

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: #include "thread.h"
                     12: #include "mythread.h"
                     13: 
                     14: // コンストラクタ
                     15: ThreadDevice::ThreadDevice(const std::string& objname_)
                     16:        : inherited(objname_)
                     17: {
                     18:        // スレッド名はデフォルトでこのオブジェクトと同じ名前にしておく。
                     19:        // 継承側コンストラクタ等で SetThreadName() で変更してもよい。
                     20:        SetThreadName(GetName().c_str());
                     21: }
                     22: 
                     23: // デストラクタ
                     24: ThreadDevice::~ThreadDevice()
                     25: {
                     26:        // ここで virtual の Terminate() は呼べないので、
                     27:        // 継承側のデストラクタで TerminateThread() をそれぞれ呼ぶこと。
                     28: }
                     29: 
                     30: // スレッド名を設定する
                     31: // (ポインタしか管理しないので実体は呼び出し側で責任を持つこと)
                     32: void
                     33: ThreadDevice::SetThreadName(const char *threadname_)
                     34: {
                     35:        assert(threadname_);
                     36:        threadname = threadname_;
                     37: }
                     38: 
                     39: // スレッド開始
                     40: bool
                     41: ThreadDevice::StartThread()
                     42: {
                     43:        auto func = [this]() {
                     44:                PTHREAD_SETNAME(threadname);
                     45:                std::lock_guard<std::mutex> lock_sub(this->thread_starter);
                     46:                this->ThreadRun();
                     47:        };
                     48: 
                     49:        // スレッド起動
                     50:        std::lock_guard<std::mutex> lock(thread_starter);
                     51: 
                     52:        thread.reset(new std::thread(func));
                     53:        if ((bool)thread == false) {
                     54:                return false;
                     55:        }
                     56:        return true;
                     57: }
                     58: 
                     59: // スレッドに終了を要求し、その終了を待つ。
                     60: // スレッド外から呼ぶこと。
                     61: void
                     62: ThreadDevice::TerminateThread()
                     63: {
                     64:        if ((bool)thread) {
                     65:                Terminate();
                     66: 
                     67:                if (thread->joinable()) {
                     68:                        thread->join();
                     69:                }
                     70: 
                     71:                thread.reset();
                     72:        }
                     73: }

unix.superglobalmegacorp.com

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