Skip to content

Commit 082fc39

Browse files
author
Eric Holk
committed
---
yaml --- r: 4305 b: refs/heads/master c: 5a673cc h: refs/heads/master i: 4303: ea9c271 v: v3
1 parent 982d196 commit 082fc39

File tree

9 files changed

+177
-20
lines changed

9 files changed

+177
-20
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 7a05f1db7cb85ead062d6a7dde566f6e3010f2f2
2+
refs/heads/master: 5a673cc2c9d1a85efb967c47e40cb805ba691b90

trunk/src/lib/comm.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys;
2+
3+
export _chan;
4+
export _port;
5+
6+
export mk_port;
7+
export mk_chan;
8+
9+
native "rust" mod rustrt {
10+
type rust_chan;
11+
type rust_port;
12+
13+
fn new_chan(po : *rust_port) -> *rust_chan;
14+
fn del_chan(ch : *rust_chan);
15+
fn drop_chan(ch : *rust_chan);
16+
17+
fn new_port(unit_sz : uint) -> *rust_port;
18+
fn del_port(po : *rust_port);
19+
fn drop_port(po : *rust_port);
20+
}
21+
22+
resource chan_ptr(ch: *rustrt::rust_chan) {
23+
rustrt::drop_chan(ch);
24+
rustrt::drop_chan(ch); // FIXME: We shouldn't have to do this
25+
// twice.
26+
rustrt::del_chan(ch);
27+
}
28+
29+
tag _chan[T] { _chan(@chan_dtor); }
30+
31+
resource port_ptr(po: *rustrt::rust_port) {
32+
rustrt::drop_port(po);
33+
rustrt::del_port(po);
34+
}
35+
36+
tag _port[T] { _port(@port_dtor); }
37+
38+
fn mk_port[T]() -> _port[T] {
39+
_port(@port_dtor(rustrt::new_port(sys::size_of[T]())))
40+
}
41+
42+
fn mk_chan[T](po : &_port[T]) -> _chan[T] {
43+
alt po {
44+
_port(_po) {
45+
_chan(@chan_dtor(rustrt::new_chan(**_po)))
46+
}
47+
}
48+
}

trunk/src/lib/std.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod io;
2323
mod ioivec;
2424
mod sys;
2525
mod task;
26+
mod comm;
2627

2728
// Utility modules.
2829

trunk/src/rt/rust_builtin.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,53 @@ sched_threads(rust_task *task) {
868868
return task->kernel->num_threads;
869869
}
870870

871+
extern "C" CDECL rust_port*
872+
new_port(rust_task *task, size_t unit_sz) {
873+
LOG(task, comm, "new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
874+
(uintptr_t) task, task->name, unit_sz);
875+
// take a reference on behalf of the port
876+
task->ref();
877+
return new (task->kernel, "rust_port") rust_port(task, unit_sz);
878+
}
879+
880+
extern "C" CDECL void
881+
del_port(rust_task *task, rust_port *port) {
882+
LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
883+
I(task->sched, !port->ref_count);
884+
delete port;
885+
886+
// FIXME: this should happen in the port.
887+
task->deref();
888+
}
889+
890+
extern "C" CDECL rust_chan*
891+
new_chan(rust_task *task, rust_port *port) {
892+
rust_scheduler *sched = task->sched;
893+
LOG(task, comm, "new_chan("
894+
"task=0x%" PRIxPTR " (%s), port=0x%" PRIxPTR ")",
895+
(uintptr_t) task, task->name, port);
896+
I(sched, port);
897+
return new (task->kernel, "rust_chan")
898+
rust_chan(task->kernel, port, port->unit_sz);
899+
}
900+
901+
extern "C" CDECL
902+
void del_chan(rust_task *task, rust_chan *chan) {
903+
LOG(task, comm, "del_chan(0x%" PRIxPTR ")", (uintptr_t) chan);
904+
chan->destroy();
905+
}
906+
907+
extern "C" CDECL
908+
void drop_chan(rust_task *task, rust_chan *chan) {
909+
chan->ref_count--;
910+
}
911+
912+
extern "C" CDECL
913+
void drop_port(rust_task *, rust_port *port) {
914+
port->ref_count--;
915+
>>>>>>> Started working on a library-based comm system. Creating and deleting ports work.
916+
}
917+
871918
//
872919
// Local Variables:
873920
// mode: C++

trunk/src/rt/rust_upcall.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,40 +95,33 @@ upcall_trace_str(rust_task *task, char const *c) {
9595
task->sched->log(task, 2, "trace: %s", c);
9696
}
9797

98+
extern "C" CDECL rust_port*
99+
new_port(rust_task *task, size_t unit_sz);
98100
extern "C" CDECL rust_port*
99101
upcall_new_port(rust_task *task, size_t unit_sz) {
100102
LOG_UPCALL_ENTRY(task);
101103
LOG(task, comm, "upcall_new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
102104
(uintptr_t) task, task->name, unit_sz);
103-
// take a reference on behalf of the port
104-
task->ref();
105-
return new (task->kernel, "rust_port") rust_port(task, unit_sz);
105+
return new_port(task, unit_sz);
106106
}
107107

108+
extern "C" CDECL void
109+
del_port(rust_task *task, rust_port *port);
108110
extern "C" CDECL void
109111
upcall_del_port(rust_task *task, rust_port *port) {
110112
LOG_UPCALL_ENTRY(task);
111-
LOG(task, comm, "upcall del_port(0x%" PRIxPTR ")", (uintptr_t) port);
112-
I(task->sched, !port->ref_count);
113-
delete port;
114-
115-
// FIXME: this should happen in the port.
116-
task->deref();
113+
return del_port(task, port);
117114
}
118115

119116
/**
120117
* Creates a new channel pointing to a given port.
121118
*/
122119
extern "C" CDECL rust_chan*
120+
new_chan(rust_task *task, rust_port *port);
121+
extern "C" CDECL rust_chan*
123122
upcall_new_chan(rust_task *task, rust_port *port) {
124123
LOG_UPCALL_ENTRY(task);
125-
rust_scheduler *sched = task->sched;
126-
LOG(task, comm, "upcall_new_chan("
127-
"task=0x%" PRIxPTR " (%s), port=0x%" PRIxPTR ")",
128-
(uintptr_t) task, task->name, port);
129-
I(sched, port);
130-
return new (task->kernel, "rust_chan")
131-
rust_chan(task->kernel, port, port->unit_sz);
124+
return new_chan(task, port);
132125
}
133126

134127
/**
@@ -148,11 +141,11 @@ upcall_flush_chan(rust_task *task, rust_chan *chan) {
148141
* appear to be live, causing modify-after-free errors.
149142
*/
150143
extern "C" CDECL
144+
void del_chan(rust_task *task, rust_chan *chan);
145+
extern "C" CDECL
151146
void upcall_del_chan(rust_task *task, rust_chan *chan) {
152147
LOG_UPCALL_ENTRY(task);
153-
154-
LOG(task, comm, "upcall del_chan(0x%" PRIxPTR ")", (uintptr_t) chan);
155-
chan->destroy();
148+
del_chan(task, chan);
156149
}
157150

158151
/**

trunk/src/rt/rustrt.def.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ debug_box
55
debug_fn
66
debug_obj
77
debug_opaque
8+
del_chan
9+
del_port
810
debug_ptrcast
911
debug_tag
1012
debug_trap
1113
debug_tydesc
1214
do_gc
15+
drop_chan
16+
drop_port
1317
get_time
1418
hack_allow_leaks
1519
ivec_copy_from_buf
@@ -20,6 +24,8 @@ ivec_reserve_shared
2024
ivec_to_ptr
2125
last_os_error
2226
nano_time
27+
new_chan
28+
new_port
2329
pin_task
2430
unpin_task
2531
rand_free
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// xfail-stage0
2+
// xfail-stage1
3+
// xfail-stage2
4+
// xfail-stage3
5+
6+
// Test case for issue #763, provided by robarnold.
7+
8+
use std;
9+
import std::task;
10+
11+
tag request {
12+
quit;
13+
close(int, chan[bool]);
14+
}
15+
16+
type ctx = chan[request];
17+
18+
fn request_task(c : chan[ctx]) {
19+
let p = port();
20+
c <| chan(p);
21+
let req;
22+
while (true) {
23+
p |> req;
24+
alt (req) {
25+
quit. {
26+
ret;
27+
}
28+
close(what, status) {
29+
log "closing now";
30+
log what;
31+
status <| true;
32+
}
33+
}
34+
}
35+
}
36+
37+
fn new() -> ctx {
38+
let p = port();
39+
let t = spawn request_task(chan(p));
40+
let cx;
41+
p |> cx;
42+
ret cx;
43+
}
44+
45+
fn main() {
46+
let cx = new();
47+
48+
let p = port();
49+
cx <| close(4, chan(p));
50+
let result;
51+
p |> result;
52+
cx <| quit;
53+
}

trunk/src/test/stdtest/comm.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std;
2+
import std::comm;
3+
4+
#[test]
5+
fn create_port_and_chan() {
6+
let p = comm::mk_port[int]();
7+
let c = comm::mk_chan(p);
8+
}

trunk/src/test/stdtest/stdtest.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std;
22

33
mod bitv;
44
mod box;
5+
mod comm;
56
mod deque;
67
mod either;
78
mod fs;

0 commit comments

Comments
 (0)