Skip to content

Commit 4005d3d

Browse files
committed
---
yaml --- r: 15742 b: refs/heads/try c: 18f8983 h: refs/heads/master v: v3
1 parent b14f32c commit 4005d3d

File tree

2 files changed

+83
-71
lines changed

2 files changed

+83
-71
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: beb1a59f82e7689b72bf10d2a86d0d1b6060983e
5+
refs/heads/try: 18f898315a6b94fec729da65b536e311bc0a9f3d
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/comm.rs

Lines changed: 82 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,26 @@ io::println(comm::recv(p));
2626

2727
import either::either;
2828

29+
export port::{};
30+
export chan::{};
2931
export send;
3032
export recv;
31-
export recv_chan;
3233
export peek;
34+
export recv_chan;
3335
export select2;
34-
export chan::{};
35-
export port::{};
3636

37-
enum rust_port {}
3837

39-
#[abi = "cdecl"]
40-
native mod rustrt {
41-
fn rust_port_id_send<T: send>(target_port: port_id,
42-
data: T) -> libc::uintptr_t;
43-
44-
fn new_port(unit_sz: libc::size_t) -> *rust_port;
45-
fn del_port(po: *rust_port);
46-
fn rust_port_begin_detach(po: *rust_port,
47-
yield: *libc::uintptr_t);
48-
fn rust_port_end_detach(po: *rust_port);
49-
fn get_port_id(po: *rust_port) -> port_id;
50-
fn rust_port_size(po: *rust_port) -> libc::size_t;
51-
fn port_recv(dptr: *uint, po: *rust_port,
52-
yield: *libc::uintptr_t);
53-
fn rust_port_select(dptr: **rust_port, ports: **rust_port,
54-
n_ports: libc::size_t,
55-
yield: *libc::uintptr_t);
56-
fn rust_port_take(port_id: port_id) -> *rust_port;
57-
fn rust_port_drop(p: *rust_port);
58-
fn rust_port_task(p: *rust_port) -> libc::uintptr_t;
59-
fn get_task_id() -> libc::uintptr_t;
60-
}
38+
#[doc = "
39+
A communication endpoint that can receive messages
6140
62-
#[abi = "rust-intrinsic"]
63-
native mod rusti {
64-
fn init<T>() -> T;
41+
Each port has a unique per-task identity and may not be replicated or
42+
transmitted. If a port value is copied, both copies refer to the same
43+
port. Ports may be associated with multiple `chan`s.
44+
"]
45+
enum port<T: send> {
46+
port_t(@port_ptr<T>)
6547
}
6648

67-
type port_id = int;
68-
6949
// It's critical that this only have one variant, so it has a record
7050
// layout, and will work in the rust_task structure in task.rs.
7151
#[doc = "
@@ -82,35 +62,18 @@ enum chan<T: send> {
8262
chan_t(port_id)
8363
}
8464

85-
resource port_ptr<T: send>(po: *rust_port) {
86-
// Once the port is detached it's guaranteed not to receive further
87-
// messages
88-
let yield = 0u;
89-
let yieldp = ptr::addr_of(yield);
90-
rustrt::rust_port_begin_detach(po, yieldp);
91-
if yield != 0u {
92-
// Need to wait for the port to be detached
93-
// FIXME: If this fails then we're going to leave our port
94-
// in a bogus state. (Issue #1988)
95-
task::yield();
96-
}
97-
rustrt::rust_port_end_detach(po);
98-
99-
// Drain the port so that all the still-enqueued items get dropped
100-
while rustrt::rust_port_size(po) > 0u {
101-
recv_::<T>(po);
102-
}
103-
rustrt::del_port(po);
65+
#[doc = "Constructs a port"]
66+
fn port<T: send>() -> port<T> {
67+
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
10468
}
10569

10670
#[doc = "
107-
A communication endpoint that can receive messages
108-
109-
Each port has a unique per-task identity and may not be replicated or
110-
transmitted. If a port value is copied, both copies refer to the same
111-
port. Ports may be associated with multiple `chan`s.
71+
Constructs a channel. The channel is bound to the port used to
72+
construct it.
11273
"]
113-
enum port<T: send> { port_t(@port_ptr<T>) }
74+
fn chan<T: send>(p: port<T>) -> chan<T> {
75+
chan_t(rustrt::get_port_id(***p))
76+
}
11477

11578
#[doc = "
11679
Sends data over a channel. The sent data is moved into the channel,
@@ -126,17 +89,39 @@ fn send<T: send>(ch: chan<T>, -data: T) {
12689
task::yield();
12790
}
12891

129-
#[doc = "Constructs a port"]
130-
fn port<T: send>() -> port<T> {
131-
port_t(@port_ptr(rustrt::new_port(sys::size_of::<T>())))
132-
}
133-
13492
#[doc = "
13593
Receive from a port. If no data is available on the port then the
13694
task will block until data becomes available.
13795
"]
13896
fn recv<T: send>(p: port<T>) -> T { recv_(***p) }
13997

98+
#[doc = "Returns true if there are messages available"]
99+
fn peek<T: send>(p: port<T>) -> bool {
100+
rustrt::rust_port_size(***p) != 0u as libc::size_t
101+
}
102+
103+
resource port_ptr<T: send>(po: *rust_port) {
104+
// Once the port is detached it's guaranteed not to receive further
105+
// messages
106+
let yield = 0u;
107+
let yieldp = ptr::addr_of(yield);
108+
rustrt::rust_port_begin_detach(po, yieldp);
109+
if yield != 0u {
110+
// Need to wait for the port to be detached
111+
// FIXME: If this fails then we're going to leave our port
112+
// in a bogus state. (Issue #1988)
113+
task::yield();
114+
}
115+
rustrt::rust_port_end_detach(po);
116+
117+
// Drain the port so that all the still-enqueued items get dropped
118+
while rustrt::rust_port_size(po) > 0u {
119+
recv_::<T>(po);
120+
}
121+
rustrt::del_port(po);
122+
}
123+
124+
140125
#[doc(hidden)]
141126
fn recv_chan<T: send>(ch: comm::chan<T>) -> T {
142127
resource portref(p: *rust_port) {
@@ -210,19 +195,46 @@ fn select2<A: send, B: send>(p_a: port<A>, p_b: port<B>)
210195
}
211196
}
212197

213-
#[doc = "Returns true if there are messages available"]
214-
fn peek<T: send>(p: port<T>) -> bool {
215-
rustrt::rust_port_size(***p) != 0u as libc::size_t
198+
199+
/* Implementation details */
200+
201+
202+
enum rust_port {}
203+
204+
type port_id = int;
205+
206+
#[abi = "cdecl"]
207+
native mod rustrt {
208+
fn rust_port_id_send<T: send>(target_port: port_id,
209+
data: T) -> libc::uintptr_t;
210+
211+
fn new_port(unit_sz: libc::size_t) -> *rust_port;
212+
fn del_port(po: *rust_port);
213+
fn rust_port_begin_detach(po: *rust_port,
214+
yield: *libc::uintptr_t);
215+
fn rust_port_end_detach(po: *rust_port);
216+
fn get_port_id(po: *rust_port) -> port_id;
217+
fn rust_port_size(po: *rust_port) -> libc::size_t;
218+
fn port_recv(dptr: *uint, po: *rust_port,
219+
yield: *libc::uintptr_t);
220+
fn rust_port_select(dptr: **rust_port, ports: **rust_port,
221+
n_ports: libc::size_t,
222+
yield: *libc::uintptr_t);
223+
fn rust_port_take(port_id: port_id) -> *rust_port;
224+
fn rust_port_drop(p: *rust_port);
225+
fn rust_port_task(p: *rust_port) -> libc::uintptr_t;
226+
fn get_task_id() -> libc::uintptr_t;
216227
}
217228

218-
#[doc = "
219-
Constructs a channel. The channel is bound to the port used to
220-
construct it.
221-
"]
222-
fn chan<T: send>(p: port<T>) -> chan<T> {
223-
chan_t(rustrt::get_port_id(***p))
229+
#[abi = "rust-intrinsic"]
230+
native mod rusti {
231+
fn init<T>() -> T;
224232
}
225233

234+
235+
/* Tests */
236+
237+
226238
#[test]
227239
fn create_port_and_chan() { let p = port::<int>(); chan(p); }
228240

0 commit comments

Comments
 (0)