aboutsummaryrefslogtreecommitdiff
path: root/src/rt/rust_chan.cpp
blob: 43744a115be1ed5ff1f30cfdcb650e78f2cb82c4 (plain) (blame)
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
73
#include "rust_internal.h"
#include "rust_chan.h"

rust_chan::rust_chan(rust_task *task, rust_port *port) :
    task(task), port(port), buffer(task->dom, port->unit_sz), token(this) {

    if (port) {
        port->chans.push(this);
        ref();
    }

    task->log(rust_log::MEM | rust_log::COMM,
              "new rust_chan(task=0x%" PRIxPTR
              ", port=0x%" PRIxPTR ") -> chan=0x%" PRIxPTR,
              (uintptr_t) task, (uintptr_t) port, (uintptr_t) this);
}

rust_chan::~rust_chan() {
    if (port) {
        if (token.pending())
            token.withdraw();
        port->chans.swap_delete(this);
    }
}

void rust_chan::disassociate() {
    I(task->dom, port);

    if (token.pending())
        token.withdraw();

    // Delete reference to the port/
    port = NULL;

    deref();
}

/**
 * Attempt to transmit channel data to the associated port.
 */
int rust_chan::transmit() {
    rust_dom *dom = task->dom;

    // TODO: Figure out how and why the port would become null.
    if (port == NULL) {
        dom->log(rust_log::COMM, "invalid port, transmission incomplete");
        return ERROR;
    }

    if (buffer.is_empty()) {
        dom->log(rust_log::COMM, "buffer is empty, transmission incomplete");
        return ERROR;
    }

    if(port->task->blocked_on(port)) {
        buffer.dequeue(port->task->rendezvous_ptr);
        port->task->wakeup(port);
    }

    return 0;

}

//
// 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:
//