Skip to content

Commit a73268e

Browse files
committed
---
yaml --- r: 7876 b: refs/heads/snap-stage3 c: 3d76922 h: refs/heads/master v: v3
1 parent 878f059 commit a73268e

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
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: e48bf6f3f4d1ca07f0c2820c6cfaad275bc106ac
4+
refs/heads/snap-stage3: 3d76922f973caa358cf803d5539dbfadeb3ca830
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/snap-stage3/src/libstd/uvtmp.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ native mod rustrt {
2727
thread: thread,
2828
req_id: u32,
2929
chan: comm::chan<iomsg>);
30+
fn rust_uvtmp_timer(
31+
thread: thread,
32+
timeout: u32,
33+
req_id: u32,
34+
chan: comm::chan<iomsg>);
3035
fn rust_uvtmp_delete_buf(buf: *u8);
3136
fn rust_uvtmp_get_req_id(cd: connect_data) -> u32;
3237
}
@@ -39,7 +44,9 @@ enum iomsg {
3944
whatever,
4045
connected(connect_data),
4146
wrote(connect_data),
42-
read(connect_data, *u8, ctypes::ssize_t)
47+
read(connect_data, *u8, ctypes::ssize_t),
48+
timer(u32),
49+
exit
4350
}
4451

4552
fn create_thread() -> thread {
@@ -58,8 +65,7 @@ fn delete_thread(thread: thread) {
5865
rustrt::rust_uvtmp_delete_thread(thread)
5966
}
6067

61-
fn connect(thread: thread, req_id: u32,
62-
ip: str, ch: comm::chan<iomsg>) -> connect_data {
68+
fn connect(thread: thread, req_id: u32, ip: str, ch: comm::chan<iomsg>) -> connect_data {
6369
str::as_buf(ip) {|ipbuf|
6470
rustrt::rust_uvtmp_connect(thread, req_id, ipbuf, ch)
6571
}
@@ -80,6 +86,11 @@ fn read_start(thread: thread, req_id: u32,
8086
rustrt::rust_uvtmp_read_start(thread, req_id, chan);
8187
}
8288

89+
fn timer_start(thread: thread, timeout: u32, req_id: u32,
90+
chan: comm::chan<iomsg>) {
91+
rustrt::rust_uvtmp_timer(thread, timeout, req_id, chan);
92+
}
93+
8394
fn delete_buf(buf: *u8) {
8495
rustrt::rust_uvtmp_delete_buf(buf);
8596
}
@@ -106,7 +117,7 @@ fn test_connect() {
106117
connect(thread, 0u32, "74.125.224.146", chan);
107118
alt comm::recv(port) {
108119
connected(cd) {
109-
close_connection(thread, 0u32);
120+
close_connection(thread, cd);
110121
}
111122
}
112123
join_thread(thread);
@@ -123,10 +134,10 @@ fn test_http() {
123134
connect(thread, 0u32, "74.125.224.146", chan);
124135
alt comm::recv(port) {
125136
connected(cd) {
126-
write(thread, 0u32, str::bytes("GET / HTTP/1.0\n\n"), chan);
137+
write(thread, cd, str::bytes("GET / HTTP/1.0\n\n"), chan);
127138
alt comm::recv(port) {
128139
wrote(cd) {
129-
read_start(thread, 0u32, chan);
140+
read_start(thread, cd, chan);
130141
let keep_going = true;
131142
while keep_going {
132143
alt comm::recv(port) {
@@ -146,7 +157,7 @@ fn test_http() {
146157
}
147158
}
148159
}
149-
close_connection(thread, 0u32);
160+
close_connection(thread, cd);
150161
}
151162
}
152163
}

branches/snap-stage3/src/rt/rust_uvtmp.cpp

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ struct connect_data {
1515
chan_handle chan;
1616
};
1717

18+
const intptr_t whatever_tag = 0;
1819
const intptr_t connected_tag = 1;
1920
const intptr_t wrote_tag = 2;
2021
const intptr_t read_tag = 3;
22+
const intptr_t timer_tag = 4;
23+
const intptr_t exit_tag = 5;
2124

2225
struct iomsg {
2326
intptr_t tag;
@@ -29,6 +32,7 @@ struct iomsg {
2932
uint8_t *buf;
3033
ssize_t nread;
3134
} read_val;
35+
uint32_t timer_req_id;
3236
} val;
3337
};
3438

@@ -44,6 +48,13 @@ struct read_start_data {
4448
chan_handle chan;
4549
};
4650

51+
struct timer_start_data {
52+
rust_uvtmp_thread *thread;
53+
uint32_t timeout;
54+
uint32_t req_id;
55+
chan_handle chan;
56+
};
57+
4758
// FIXME: Copied from rust_builtins.cpp. Could bitrot easily
4859
static void
4960
send(rust_task *task, chan_handle chan, void *data) {
@@ -72,7 +83,7 @@ class rust_uvtmp_thread : public rust_thread {
7283
std::queue<connect_data*> close_connection_queue;
7384
std::queue<write_data*> write_queue;
7485
std::queue<read_start_data*> read_start_queue;
75-
86+
std::queue<timer_start_data*> timer_start_queue;
7687
public:
7788

7889
rust_uvtmp_thread() {
@@ -139,6 +150,17 @@ class rust_uvtmp_thread : public rust_thread {
139150
read_start_queue.push(rd);
140151
}
141152

153+
void
154+
timer(uint32_t timeout, uint32_t req_id, chan_handle chan) {
155+
scoped_lock with(lock);
156+
157+
timer_start_data *td = new timer_start_data();
158+
td->timeout = timeout;
159+
td->req_id = req_id;
160+
td->chan = chan;
161+
timer_start_queue.push(td);
162+
}
163+
142164
private:
143165

144166
virtual void
@@ -159,6 +181,7 @@ class rust_uvtmp_thread : public rust_thread {
159181
close_connections();
160182
write_buffers();
161183
start_reads();
184+
start_timers();
162185
close_idle_if_stop();
163186
}
164187

@@ -246,7 +269,7 @@ class rust_uvtmp_thread : public rust_thread {
246269
void
247270
on_write(uv_write_t *handle, write_data *wd) {
248271
iomsg msg;
249-
msg.tag = wrote_tag;
272+
msg.tag = timer_tag;
250273
msg.val.wrote_val = wd->cd;
251274

252275
send(task, wd->chan, &msg);
@@ -299,6 +322,40 @@ class rust_uvtmp_thread : public rust_thread {
299322
}
300323
}
301324

325+
void
326+
start_timers() {
327+
assert (lock.lock_held_by_current_thread());
328+
while (!timer_start_queue.empty()) {
329+
timer_start_data *td = timer_start_queue.front();
330+
timer_start_queue.pop();
331+
332+
td->thread = this;
333+
334+
uv_timer_t *timer = (uv_timer_t *)malloc(sizeof(uv_timer_t));
335+
timer->data = td;
336+
int result = uv_timer_init(loop, timer);
337+
result = uv_timer_start(timer, timer_cb, td->timeout, 0);
338+
}
339+
}
340+
341+
static void
342+
timer_cb(uv_timer_t *handle, int what) {
343+
timer_start_data *td = (timer_start_data*)handle->data;
344+
rust_uvtmp_thread *self = td->thread;
345+
self->on_timer(td);
346+
free(handle);
347+
}
348+
349+
void
350+
on_timer(timer_start_data *rd) {
351+
iomsg msg;
352+
msg.tag = timer_tag;
353+
msg.val.timer_req_id = rd->req_id;
354+
355+
send(task, rd->chan, &msg);
356+
delete rd;
357+
}
358+
302359
void
303360
close_idle_if_stop() {
304361
assert(lock.lock_held_by_current_thread());
@@ -353,6 +410,11 @@ rust_uvtmp_read_start(rust_uvtmp_thread *thread, uint32_t req_id,
353410
thread->read_start(req_id, *chan);
354411
}
355412

413+
extern "C" void
414+
rust_uvtmp_timer(rust_uvtmp_thread *thread, uint32_t timeout, uint32_t req_id, chan_handle *chan) {
415+
thread->timer(timeout, req_id, *chan);
416+
}
417+
356418
extern "C" void
357419
rust_uvtmp_delete_buf(uint8_t *buf) {
358420
delete [] buf;

branches/snap-stage3/src/rt/rustrt.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ rust_uvtmp_connect
9696
rust_uvtmp_close_connection
9797
rust_uvtmp_write
9898
rust_uvtmp_read_start
99+
rust_uvtmp_timer
99100
rust_uvtmp_delete_buf
100101
rust_uvtmp_get_req_id
101102

0 commit comments

Comments
 (0)