Annotation of nono/debugger/cvprompt.h, revision 1.1.1.1

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2019 [email protected]
                      4: //
                      5: 
                      6: #pragma once
                      7: 
                      8: #include "consio.h"
                      9: #include <condition_variable>
                     10: #include <mutex>
                     11: 
                     12: // デバッガプロンプトについての条件変数
                     13: class CVPrompt
                     14: {
                     15:  public:
                     16:        // プロンプトを出してよいというCPUからの通知
                     17:        void NotifyAcquire()
                     18:        {
                     19:                std::unique_lock<std::mutex> lk(mtx);
                     20:                is_prompt = true;
                     21:                cvAcquire.notify_one();
                     22:        }
                     23: 
                     24:        // プロンプトを出せるようになるのを待つ
                     25:        void WaitAcquire(Consio* cons)
                     26:        {
                     27:                std::chrono::milliseconds timeout(200);
                     28: 
                     29:                for (;;) {
                     30:                        std::unique_lock<std::mutex> lk(mtx);
                     31:                        cvAcquire.wait_for(lk, timeout, [&] { return is_prompt; });
                     32: 
                     33:                        // 条件成立
                     34:                        if (is_prompt) {
                     35:                                return;
                     36:                        }
                     37: 
                     38:                        // XXX 仕方ないのでここで定期的にコネクションの状態を調べる。
                     39:                        // XXX Poll() は EOF で 1 を返すのでここでは着信と EOF の区別は
                     40:                        //     できない。
                     41:                        if (cons->Poll() != 0) {
                     42:                                return;
                     43:                        }
                     44:                }
                     45:        }
                     46: 
                     47:        // プロンプト終了したことをCPUに通知
                     48:        void NotifyRelease()
                     49:        {
                     50:                std::unique_lock<std::mutex> lk(mtx);
                     51:                is_prompt = false;
                     52:                cvRelease.notify_one();
                     53:        }
                     54: 
                     55:        // プロンプト終了を待つ
                     56:        void WaitRelease()
                     57:        {
                     58:                std::unique_lock<std::mutex> lk(mtx);
                     59:                cvRelease.wait(lk, [&] { return !is_prompt; });
                     60:        }
                     61: 
                     62:        // プロンプト出している状態なら true を返す
                     63:        bool IsPrompt() const {
                     64:                return is_prompt;
                     65:        }
                     66: 
                     67:  private:
                     68:        bool is_prompt = false;         // プロンプト中なら true
                     69:        std::mutex mtx;
                     70:        std::condition_variable cvAcquire;
                     71:        std::condition_variable cvRelease;
                     72: };
                     73: 
                     74: // debugger.cpp で初期化
                     75: extern CVPrompt *gCVPrompt;

unix.superglobalmegacorp.com

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