Annotation of nono/cli/cuimessage.cpp, revision 1.1.1.4

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2021 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
1.1.1.2   root        7: //
                      8: // UI スレッドへのメッセージ機構 (CLI 側)
                      9: //
                     10: 
1.1.1.3   root       11: #include "nono.h"
1.1       root       12: #include "cuimessage.h"
1.1.1.2   root       13: #include "kevent.h"
1.1       root       14: 
1.1.1.2   root       15: /*static*/ autofd CUIMessage::wpipe;
                     16: /*static*/ autofd CUIMessage::rpipe;
                     17: 
                     18: // CUIMessage の初期化。パイプを用意して kqueue ディスクリプタを返す。
                     19: int
                     20: CUIMessage::Init()
                     21: {
                     22:        int kq;
                     23:        int fds[2];
                     24: 
                     25:        kq = kqueue();
                     26:        if (kq < 0) {
                     27:                warn("CUIMessage::Init: kqueue");
                     28:                return -1;
                     29:        }
                     30: 
                     31:        if (pipe(fds) < 0) {
                     32:                warn("CUIMessage::Init: pipe");
                     33:                close(kq);
                     34:                return -1;
                     35:        }
                     36: 
                     37:        rpipe = fds[0];
                     38:        wpipe = fds[1];
                     39: 
                     40:        if (kevent_add(kq, rpipe, EVFILT_READ, EV_ADD, 0) < 0) {
                     41:                warn("CUIMessage::Init: kevent_add");
                     42:                close(kq);
                     43:                return -1;
                     44:        }
                     45: 
                     46:        return kq;
                     47: }
                     48: 
1.1.1.4 ! root       49: // UIMessage::Post() で呼ばれるコールバック関数。
1.1       root       50: /*static*/ void
1.1.1.4 ! root       51: CUIMessage::Process(uint id, int arg)
1.1       root       52: {
1.1.1.4 ! root       53:        if (id >= UIMessage::ID_MAX) {
        !            54:                PANIC("Invalid UIMessage %u", (uint)id);
1.1       root       55:        }
1.1.1.2   root       56: 
1.1.1.4 ! root       57:        union64 m;
        !            58:        m.h = id;
        !            59:        m.l = arg;
        !            60: 
        !            61:        // パイプで通知。
        !            62:        int r;
        !            63:        r = write(wpipe, &m, sizeof(m));
        !            64:        if (r < 0) {
        !            65:                warn("CUIMessage::Process: write");
        !            66:                return;
        !            67:        }
        !            68:        if (r < sizeof(m)) {
        !            69:                warnx("CUIMessage::Process: write: too short");
        !            70:                return;
1.1.1.2   root       71:        }
                     72: }

unix.superglobalmegacorp.com

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