Skip to content

Commit 4a9b668

Browse files
committed
---
yaml --- r: 10647 b: refs/heads/snap-stage3 c: e4c2915 h: refs/heads/master i: 10645: 27ef349 10643: fb116e5 10639: 5cef5a1 v: v3
1 parent a9d2d32 commit 4a9b668

File tree

6 files changed

+150
-34
lines changed

6 files changed

+150
-34
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
refs/heads/master: 2898dcc5d97da9427ac367542382b6239d9c0bbf
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: f54829cf13158e146114b090d40260823bf3a56a
4+
refs/heads/snap-stage3: e4c291530e4cc160eca30811e17d614f236147a4
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/snap-stage3/src/libcore/arc.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ resource arc_destruct<T>(data: *libc::c_void) {
2828
unsafe {
2929
let data: ~arc_data<T> = unsafe::reinterpret_cast(data);
3030
let new_count = rustrt::rust_atomic_decrement(&mut data.count);
31+
let data_ptr : *() = unsafe::reinterpret_cast(data);
3132
assert new_count >= 0;
3233
if new_count == 0 {
3334
// drop glue takes over.
@@ -68,17 +69,19 @@ allowing them to share the underlying data."]
6869
fn clone<T: const>(rc: &arc<T>) -> arc<T> {
6970
unsafe {
7071
let ptr: ~arc_data<T> = unsafe::reinterpret_cast(**rc);
71-
rustrt::rust_atomic_increment(&mut ptr.count);
72+
let new_count = rustrt::rust_atomic_increment(&mut ptr.count);
73+
let data_ptr : *() = unsafe::reinterpret_cast(ptr);
74+
assert new_count >= 2;
7275
unsafe::forget(ptr);
7376
}
7477
arc_destruct(**rc)
7578
}
7679

7780
// An arc over mutable data that is protected by a lock.
78-
type ex_data<T> = {lock: sys::lock_and_signal, data: T};
79-
type exclusive<T> = arc_destruct<ex_data<T>>;
81+
type ex_data<T: send> = {lock: sys::lock_and_signal, data: T};
82+
type exclusive<T: send> = arc_destruct<ex_data<T>>;
8083

81-
fn exclusive<T>(-data: T) -> exclusive<T> {
84+
fn exclusive<T:send >(-data: T) -> exclusive<T> {
8285
let data = ~{mut count: 1, data: {lock: sys::create_lock(),
8386
data: data}};
8487
unsafe {
@@ -88,12 +91,14 @@ fn exclusive<T>(-data: T) -> exclusive<T> {
8891
}
8992
}
9093

91-
impl methods<T> for exclusive<T> {
94+
impl methods<T: send> for exclusive<T> {
9295
fn clone() -> exclusive<T> {
9396
unsafe {
9497
// this makes me nervous...
9598
let ptr: ~arc_data<ex_data<T>> = unsafe::reinterpret_cast(*self);
96-
rustrt::rust_atomic_increment(&mut ptr.count);
99+
let new_count = rustrt::rust_atomic_increment(&mut ptr.count);
100+
let data_ptr : *() = unsafe::reinterpret_cast(ptr);
101+
assert new_count > 1;
97102
unsafe::forget(ptr);
98103
}
99104
arc_destruct(*self)

branches/snap-stage3/src/libcore/core.rc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export float, f32, f64;
3939
export box, char, str, ptr, vec, bool;
4040
export either, option, result, iter;
4141
export libc, os, io, run, rand, sys, unsafe, logging;
42-
export arc, comm, task, future;
42+
export arc, newcomm, comm, task, future;
4343
export extfmt;
4444
export tuple;
4545
export to_str;
@@ -176,6 +176,7 @@ mod dvec_iter {
176176

177177
// Concurrency
178178
mod arc;
179+
mod newcomm;
179180
mod comm;
180181
mod task;
181182
mod future;

branches/snap-stage3/src/libcore/dvec.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ impl extensions<A> for dvec<A> {
132132
self.check_not_borrowed();
133133
self.data <- w;
134134
}
135-
}
136-
137-
impl extensions<A:copy> for dvec<A> {
138-
#[doc = "Append a single item to the end of the list"]
139-
fn push(t: A) {
140-
self.check_not_borrowed();
141-
vec::push(self.data, t);
142-
}
143135

144136
#[doc = "Remove and return the last element"]
145137
fn pop() -> A {
@@ -151,6 +143,38 @@ impl extensions<A:copy> for dvec<A> {
151143
}
152144
}
153145

146+
#[doc = "Insert a single item at the front of the list"]
147+
fn unshift(-t: A) {
148+
unsafe {
149+
let mut data = unsafe::reinterpret_cast(null::<()>());
150+
data <-> self.data;
151+
let data_ptr: *() = unsafe::reinterpret_cast(data);
152+
if data_ptr.is_null() { fail "Recursive use of dvec"; }
153+
log(error, "a");
154+
self.data <- [mut t] + data;
155+
log(error, "b");
156+
}
157+
}
158+
159+
#[doc = "Append a single item to the end of the list"]
160+
fn push(+t: A) {
161+
self.check_not_borrowed();
162+
vec::push(self.data, t);
163+
}
164+
165+
166+
#[doc = "Remove and return the first element"]
167+
fn shift() -> A {
168+
self.borrow { |v|
169+
let mut v = vec::from_mut(v);
170+
let result = vec::shift(v);
171+
self.return(vec::to_mut(v));
172+
result
173+
}
174+
}
175+
}
176+
177+
impl extensions<A:copy> for dvec<A> {
154178
#[doc = "
155179
Append all elements of a vector to the end of the list
156180
@@ -213,16 +237,6 @@ impl extensions<A:copy> for dvec<A> {
213237
}
214238
}
215239

216-
#[doc = "Remove and return the first element"]
217-
fn shift() -> A {
218-
self.borrow { |v|
219-
let mut v = vec::from_mut(v);
220-
let result = vec::shift(v);
221-
self.return(vec::to_mut(v));
222-
result
223-
}
224-
}
225-
226240
#[doc = "Copy out an individual element"]
227241
#[inline(always)]
228242
fn [](idx: uint) -> A {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#[doc="A new implementation of communication.
2+
3+
This should be implementing almost entirely in Rust, and hopefully
4+
avoid needing a single global lock."]
5+
6+
import arc::methods;
7+
import dvec::dvec;
8+
import dvec::{extensions};
9+
10+
export port;
11+
export chan;
12+
export send, recv;
13+
export methods;
14+
15+
type raw_port<T: send> = arc::exclusive<dvec<T>>;
16+
17+
enum port<T: send> {
18+
port_(raw_port<T>)
19+
}
20+
enum chan<T: send> {
21+
chan_(raw_port<T>)
22+
}
23+
24+
fn port<T: send>() -> port<T> {
25+
port_(arc::exclusive(dvec()))
26+
}
27+
28+
fn chan<T: send>(p: port<T>) -> chan<T> {
29+
chan_((*p).clone())
30+
}
31+
32+
fn send<T: send>(c: chan<T>, -x: T) {
33+
let mut x <- some(x);
34+
(*c).with {|cond, data|
35+
let mut xx = none;
36+
xx <-> x;
37+
alt xx {
38+
some(y) {
39+
let mut x <- y;
40+
(*data).push(x);
41+
cond.signal();
42+
}
43+
none { fail }
44+
};
45+
}
46+
}
47+
48+
fn recv<T: send>(p: port<T>) -> T {
49+
(*p).with {|cond, data|
50+
if (*data).len() == 0u {
51+
cond.wait();
52+
}
53+
assert (*data).len() > 0u;
54+
(*data).shift()
55+
}
56+
}
57+
58+
impl methods<T: send> for chan<T> {
59+
fn send(-x: T) {
60+
send(self, x)
61+
}
62+
63+
fn clone() -> chan<T> {
64+
chan_((*self).clone())
65+
}
66+
}
67+
68+
impl methods<T: send> for port<T> {
69+
fn recv() -> T {
70+
recv(self)
71+
}
72+
73+
fn chan() -> chan<T> {
74+
chan(self)
75+
}
76+
}
77+
78+
#[cfg(test)]
79+
mod test {
80+
#[test]
81+
fn newport_simple() {
82+
let p = port();
83+
let c = chan(p);
84+
85+
c.send(42);
86+
assert p.recv() == 42;
87+
}
88+
}

branches/snap-stage3/src/libcore/vec.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,25 @@ fn rsplitn<T: copy>(v: [T]/&, n: uint, f: fn(T) -> bool) -> [[T]] {
371371
// Mutators
372372

373373
#[doc = "Removes the first element from a vector and return it"]
374-
fn shift<T: copy>(&v: [T]) -> T {
374+
fn shift<T>(&v: [T]) -> T {
375375
let ln = len::<T>(v);
376376
assert (ln > 0u);
377-
let e = v[0];
378-
v = slice::<T>(v, 1u, ln);
379-
ret e;
380-
}
381377

382-
#[doc = "Prepend an element to a vector"]
383-
fn unshift<T: copy>(&v: [T], +t: T) {
384-
v = [t] + v;
378+
let mut vv = [];
379+
v <-> vv;
380+
381+
unsafe {
382+
let vv = unsafe::to_ptr(vv);
383+
let r <- *vv;
384+
385+
for uint::range(1u, ln) {|i|
386+
// FIXME: this isn't legal, per se...
387+
let r <- *ptr::offset(vv, i);
388+
push(v, r);
389+
}
390+
391+
r
392+
}
385393
}
386394

387395
#[doc = "Remove the last element from a vector and return it"]

0 commit comments

Comments
 (0)