Skip to content

Commit 5d1e321

Browse files
committed
rt: Remove rust_chan
1 parent 39084fb commit 5d1e321

File tree

9 files changed

+29
-140
lines changed

9 files changed

+29
-140
lines changed

mk/rt.mk

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ RUNTIME_CS_$(1) := \
4545
rt/rust_scheduler.cpp \
4646
rt/rust_task.cpp \
4747
rt/rust_task_list.cpp \
48-
rt/rust_chan.cpp \
4948
rt/rust_port.cpp \
5049
rt/rust_upcall.cpp \
5150
rt/rust_log.cpp \
@@ -78,7 +77,6 @@ RUNTIME_HDR_$(1) := rt/globals.h \
7877
rt/rust_gc.h \
7978
rt/rust_internal.h \
8079
rt/rust_util.h \
81-
rt/rust_chan.h \
8280
rt/rust_env.h \
8381
rt/rust_obstack.h \
8482
rt/rust_unwind.h \

src/rt/rust_builtin.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,20 +463,15 @@ new_port(size_t unit_sz) {
463463
rust_task *task = rust_scheduler::get_task();
464464
LOG(task, comm, "new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
465465
(uintptr_t) task, task->name, unit_sz);
466-
// take a reference on behalf of the port
467-
task->ref();
466+
// port starts with refcount == 1
468467
return new (task->kernel, "rust_port") rust_port(task, unit_sz);
469468
}
470469

471470
extern "C" CDECL void
472471
del_port(rust_port *port) {
473472
rust_task *task = rust_scheduler::get_task();
474473
LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
475-
I(task->sched, !port->ref_count);
476-
delete port;
477-
478-
// FIXME: this should happen in the port.
479-
task->deref();
474+
port->deref();
480475
}
481476

482477
extern "C" CDECL rust_port_id
@@ -486,7 +481,6 @@ get_port_id(rust_port *port) {
486481

487482
extern "C" CDECL
488483
void drop_port(rust_port *port) {
489-
port->ref_count--;
490484
}
491485

492486
extern "C" CDECL void
@@ -497,10 +491,11 @@ chan_id_send(type_desc *t, rust_task_id target_task_id,
497491
rust_task *target_task = task->kernel->get_task_by_id(target_task_id);
498492
if(target_task) {
499493
rust_port *port = target_task->get_port_by_id(target_port_id);
494+
target_task->deref();
500495
if(port) {
501496
port->send(sptr);
497+
port->deref();
502498
}
503-
target_task->deref();
504499
}
505500
}
506501

src/rt/rust_chan.cpp

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/rt/rust_chan.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/rt/rust_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ struct rust_scheduler;
5555
struct rust_task;
5656
class rust_log;
5757
class rust_port;
58-
class rust_chan;
5958
class rust_kernel;
6059
class rust_crate_cache;
6160

@@ -282,7 +281,6 @@ struct type_desc {
282281

283282
#include "circular_buffer.h"
284283
#include "rust_task.h"
285-
#include "rust_chan.h"
286284
#include "rust_port.h"
287285
#include "memory.h"
288286

src/rt/rust_port.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,47 @@
11
#include "rust_internal.h"
22
#include "rust_port.h"
3-
#include "rust_chan.h"
43

54

65
rust_port::rust_port(rust_task *task, size_t unit_sz)
76
: ref_count(1), kernel(task->kernel), task(task),
8-
unit_sz(unit_sz) {
7+
unit_sz(unit_sz), buffer(kernel, unit_sz) {
98

109
LOG(task, comm,
1110
"new rust_port(task=0x%" PRIxPTR ", unit_sz=%d) -> port=0x%"
1211
PRIxPTR, (uintptr_t)task, unit_sz, (uintptr_t)this);
1312

13+
task->ref();
1414
id = task->register_port(this);
15-
remote_chan = new (task->kernel, "rust_chan")
16-
rust_chan(task->kernel, this, unit_sz);
17-
remote_chan->ref();
18-
remote_chan->port = this;
1915
}
2016

2117
rust_port::~rust_port() {
2218
LOG(task, comm, "~rust_port 0x%" PRIxPTR, (uintptr_t) this);
2319

24-
{
25-
scoped_lock with(lock);
26-
remote_chan->port = NULL;
27-
remote_chan->deref();
28-
remote_chan = NULL;
29-
}
30-
3120
task->release_port(id);
21+
task->deref();
3222
}
3323

3424
void rust_port::send(void *sptr) {
35-
if (!remote_chan->is_associated()) {
36-
W(kernel, remote_chan->is_associated(),
37-
"rust_chan::transmit with no associated port.");
38-
return;
39-
}
40-
25+
// FIXME: Is this lock really necessary? Why do we send with the lock
26+
// but not receive with the lock?
4127
scoped_lock with(lock);
4228

43-
remote_chan->buffer.enqueue(sptr);
29+
buffer.enqueue(sptr);
4430

45-
A(kernel, !remote_chan->buffer.is_empty(),
31+
A(kernel, !buffer.is_empty(),
4632
"rust_chan::transmit with nothing to send.");
4733

4834
if (task->blocked_on(this)) {
4935
KLOG(kernel, comm, "dequeued in rendezvous_ptr");
50-
remote_chan->buffer.dequeue(task->rendezvous_ptr);
36+
buffer.dequeue(task->rendezvous_ptr);
5137
task->rendezvous_ptr = 0;
5238
task->wakeup(this);
5339
}
5440
}
5541

5642
bool rust_port::receive(void *dptr) {
57-
if (remote_chan->buffer.is_empty() == false) {
58-
remote_chan->buffer.dequeue(dptr);
43+
if (buffer.is_empty() == false) {
44+
buffer.dequeue(dptr);
5945
LOG(task, comm, "<=== read data ===");
6046
return true;
6147
}
@@ -64,9 +50,8 @@ bool rust_port::receive(void *dptr) {
6450

6551
void rust_port::log_state() {
6652
LOG(task, comm,
67-
"\tchan: 0x%" PRIxPTR ", size: %d",
68-
remote_chan,
69-
remote_chan->buffer.size());
53+
"port size: %d",
54+
buffer.size());
7055
}
7156

7257
//

src/rt/rust_port.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
class rust_port : public kernel_owned<rust_port>, public rust_cond {
55
public:
6-
RUST_REFCOUNTED(rust_port);
6+
RUST_ATOMIC_REFCOUNT();
77

88
rust_port_id id;
99

1010
rust_kernel *kernel;
1111
rust_task *task;
12-
rust_chan *remote_chan;
1312
size_t unit_sz;
13+
circular_buffer buffer;
1414

1515
lock_and_signal lock;
1616

src/rt/rust_task.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,20 @@ rust_task::rust_task(rust_scheduler *sched, rust_task_list *state,
122122
rust_task::~rust_task()
123123
{
124124
I(sched, !sched->lock.lock_held_by_current_thread());
125+
I(sched, port_table.is_empty());
125126
DLOG(sched, task, "~rust_task %s @0x%" PRIxPTR ", refcnt=%d",
126127
name, (uintptr_t)this, ref_count);
127128

128129
if(user.notify_enabled) {
129-
rust_chan *target =
130-
get_chan_by_handle(&user.notify_chan);
130+
rust_port *target =
131+
get_port_by_chan_handle(&user.notify_chan);
131132
if(target) {
132133
task_notification msg;
133134
msg.id = user.id;
134135
msg.result = failed ? tr_failure : tr_success;
135136

136-
if (target->is_associated()) {
137-
target->port->send(&msg);
138-
target->deref();
139-
}
137+
target->send(&msg);
138+
target->deref();
140139
}
141140
}
142141

@@ -552,16 +551,18 @@ rust_port *rust_task::get_port_by_id(rust_port_id id) {
552551
scoped_lock with(lock);
553552
rust_port *port = NULL;
554553
port_table.get(id, &port);
554+
if (port) {
555+
port->ref();
556+
}
555557
return port;
556558
}
557559

558-
rust_chan *rust_task::get_chan_by_handle(chan_handle *handle) {
560+
rust_port *rust_task::get_port_by_chan_handle(chan_handle *handle) {
559561
rust_task *target_task = kernel->get_task_by_id(handle->task);
560562
if(target_task) {
561563
rust_port *port = target_task->get_port_by_id(handle->port);
562564
target_task->deref();
563-
port->remote_chan->ref();
564-
return port->remote_chan;
565+
return port;
565566
}
566567
return NULL;
567568
}

src/rt/rust_task.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ rust_task : public kernel_owned<rust_task>, rust_cond
209209
// not at all safe.
210210
intptr_t get_ref_count() const { return ref_count; }
211211

212-
rust_chan *get_chan_by_handle(chan_handle *handle);
212+
rust_port *get_port_by_chan_handle(chan_handle *handle);
213213

214214
// FIXME: These functions only exist to get the tasking system off the
215215
// ground. We should never be migrating shared boxes between tasks.

0 commit comments

Comments
 (0)