about summary refs log tree commit diff
path: root/src/rt/rust_port.cpp
blob: 82054687f2f6d63459ed9a18906d441e46d28005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "rust_internal.h"
#include "rust_port.h"

rust_port::rust_port(rust_task *task, size_t unit_sz) :
                     maybe_proxy<rust_port>(this), task(task),
                     unit_sz(unit_sz), writers(task->dom), chans(task->dom) {

    task->log(rust_log::MEM | rust_log::COMM,
              "new rust_port(task=0x%" PRIxPTR ", unit_sz=%d) -> port=0x%"
              PRIxPTR, (uintptr_t)task, unit_sz, (uintptr_t)this);

    // Allocate a remote channel, for remote channel data.
    remote_channel = new (task->dom) rust_chan(task, this, unit_sz);
}

rust_port::~rust_port() {
    task->log(rust_log::COMM | rust_log::MEM,
              "~rust_port 0x%" PRIxPTR, (uintptr_t) this);

    log_state();

    // Disassociate channels from this port.
    while (chans.is_empty() == false) {
        rust_chan *chan = chans.peek();
        chan->disassociate();

        if (chan->ref_count == 0) {
            task->log(rust_log::COMM,
                "chan: 0x%" PRIxPTR " is dormant, freeing", chan);
            delete chan;
        }
    }

    delete remote_channel;
}

bool rust_port::receive(void *dptr) {
    for (uint32_t i = 0; i < chans.length(); i++) {
        rust_chan *chan = chans[i];
        if (chan->buffer.is_empty() == false) {
            chan->buffer.dequeue(dptr);
            task->log(rust_log::COMM, "<=== read data ===");
            return true;
        }
    }
    return false;
}

void rust_port::log_state() {
    task->log(rust_log::COMM,
              "rust_port: 0x%" PRIxPTR ", associated channel(s): %d",
              this, chans.length());
    for (uint32_t i = 0; i < chans.length(); i++) {
        rust_chan *chan = chans[i];
        task->log(rust_log::COMM,
            "\tchan: 0x%" PRIxPTR ", size: %d, remote: %s",
            chan,
            chan->buffer.size(),
            chan == remote_channel ? "yes" : "no");
    }
}

//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//