Skip to content

Commit 81232c3

Browse files
committed
---
yaml --- r: 315 b: refs/heads/master c: 00d1465 h: refs/heads/master i: 313: 6ac35a5 311: c92a2b7 v: v3
1 parent 6250301 commit 81232c3

36 files changed

+1495
-835
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: 1f0656d9084970fcc02ba9c27277265b8b3b7217
2+
refs/heads/master: 00d1465d13980fc3acf650f182ee0723fbda0e06

trunk/src/Makefile

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ BOOT_CMXS := $(BOOT_MLS:.ml=.cmx)
244244
BOOT_OBJS := $(BOOT_MLS:.ml=.o)
245245
BOOT_CMIS := $(BOOT_MLS:.ml=.cmi)
246246

247-
RUNTIME_CS := rt/rust.cpp \
247+
RUNTIME_CS := rt/sync/spin_lock.cpp \
248+
rt/sync/lock_free_queue.cpp \
249+
rt/sync/condition_variable.cpp \
250+
rt/rust.cpp \
248251
rt/rust_builtin.cpp \
249252
rt/rust_crate.cpp \
250253
rt/rust_crate_cache.cpp \
@@ -256,12 +259,19 @@ RUNTIME_CS := rt/rust.cpp \
256259
rt/rust_upcall.cpp \
257260
rt/rust_log.cpp \
258261
rt/rust_timer.cpp \
262+
rt/circular_buffer.cpp \
259263
rt/isaac/randport.cpp
260264

261-
RUNTIME_HDR := rt/rust.h \
265+
RUNTIME_HDR := rt/globals.h \
266+
rt/rust.h \
262267
rt/rust_dwarf.h \
263268
rt/rust_internal.h \
264-
rt/rust_util.h
269+
rt/rust_util.h \
270+
rt/rust_chan.h \
271+
rt/rust_dom.h \
272+
rt/rust_task.h \
273+
rt/rust_proxy.h \
274+
rt/circular_buffer.h
265275

266276
RUNTIME_INCS := -Irt/isaac -Irt/uthash
267277
RUNTIME_OBJS := $(RUNTIME_CS:.cpp=$(CFG_OBJ_SUFFIX))
@@ -363,6 +373,8 @@ TEST_XFAILS_X86 := $(MUT_BOX_XFAILS) \
363373
test/run-pass/task-comm.rs \
364374
test/run-pass/vec-alloc-append.rs \
365375
test/run-pass/vec-slice.rs \
376+
test/run-pass/task-comm-3.rs \
377+
test/run-pass/task-comm-4.rs \
366378
test/compile-fail/bad-recv.rs \
367379
test/compile-fail/bad-send.rs \
368380
test/compile-fail/infinite-tag-type-recursion.rs \
@@ -452,6 +464,11 @@ TEST_XFAILS_LLVM := $(addprefix test/run-pass/, \
452464
tail-cps.rs \
453465
tail-direct.rs \
454466
task-comm.rs \
467+
task-comm-0.rs \
468+
task-comm-1.rs \
469+
task-comm-2.rs \
470+
task-comm-3.rs \
471+
task-comm-4.rs \
455472
threads.rs \
456473
tup.rs \
457474
type-sizes.rs \

trunk/src/boot/be/abi.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
let rc_base_field_refcnt = 0;;
1414

1515
let task_field_refcnt = rc_base_field_refcnt;;
16-
let task_field_stk = task_field_refcnt + 1;;
16+
let task_field_stk = task_field_refcnt + 2;;
1717
let task_field_runtime_sp = task_field_stk + 1;;
1818
let task_field_rust_sp = task_field_runtime_sp + 1;;
1919
let task_field_gc_alloc_chain = task_field_rust_sp + 1;;

trunk/src/rt/circular_buffer.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* A simple resizable circular buffer.
3+
*/
4+
5+
#include "rust_internal.h"
6+
7+
circular_buffer::circular_buffer(rust_dom *dom, size_t unit_sz) :
8+
dom(dom),
9+
_buffer_sz(INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz),
10+
unit_sz(unit_sz),
11+
_next(0),
12+
_unread(0),
13+
_buffer((uint8_t *)dom->calloc(_buffer_sz)) {
14+
15+
A(dom, unit_sz, "Unit size must be larger than zero.");
16+
17+
dom->log(rust_log::MEM | rust_log::COMM,
18+
"new circular_buffer(buffer_sz=%d, unread=%d)"
19+
"-> circular_buffer=0x%" PRIxPTR,
20+
_buffer_sz, _unread, this);
21+
22+
A(dom, _buffer, "Failed to allocate buffer.");
23+
}
24+
25+
circular_buffer::~circular_buffer() {
26+
dom->log(rust_log::MEM | rust_log::COMM,
27+
"~circular_buffer 0x%" PRIxPTR,
28+
this);
29+
I(dom, _buffer);
30+
// I(dom, unread == 0);
31+
dom->free(_buffer);
32+
}
33+
34+
/**
35+
* Copies the unread data from this buffer to the "dst" address.
36+
*/
37+
void
38+
circular_buffer::transfer(void *dst) {
39+
I(dom, dst);
40+
uint8_t *ptr = (uint8_t *) dst;
41+
for (size_t i = 0; i < _unread; i += unit_sz) {
42+
memcpy(&ptr[i], &_buffer[_next + i % _buffer_sz], unit_sz);
43+
}
44+
}
45+
46+
/**
47+
* Copies the data at the "src" address into this buffer. The buffer is
48+
* grown if it isn't large enough.
49+
*/
50+
void
51+
circular_buffer::enqueue(void *src) {
52+
I(dom, src);
53+
I(dom, _unread <= _buffer_sz);
54+
55+
// Grow if necessary.
56+
if (_unread == _buffer_sz) {
57+
I(dom, _buffer_sz <= MAX_CIRCULAR_BUFFFER_SIZE);
58+
void *tmp = dom->malloc(_buffer_sz << 1);
59+
transfer(tmp);
60+
_buffer_sz <<= 1;
61+
dom->free(_buffer);
62+
_buffer = (uint8_t *)tmp;
63+
}
64+
65+
dom->log(rust_log::MEM | rust_log::COMM,
66+
"circular_buffer enqueue "
67+
"unread: %d, buffer_sz: %d, unit_sz: %d",
68+
_unread, _buffer_sz, unit_sz);
69+
70+
I(dom, _unread < _buffer_sz);
71+
I(dom, _unread + unit_sz <= _buffer_sz);
72+
73+
// Copy data
74+
size_t i = (_next + _unread) % _buffer_sz;
75+
memcpy(&_buffer[i], src, unit_sz);
76+
_unread += unit_sz;
77+
78+
dom->log(rust_log::MEM | rust_log::COMM,
79+
"circular_buffer pushed data at index: %d", i);
80+
}
81+
82+
/**
83+
* Copies data from this buffer to the "dst" address. The buffer is
84+
* shrunk if possible.
85+
*/
86+
void
87+
circular_buffer::dequeue(void *dst) {
88+
I(dom, dst);
89+
I(dom, unit_sz > 0);
90+
I(dom, _unread >= unit_sz);
91+
I(dom, _unread <= _buffer_sz);
92+
I(dom, _buffer);
93+
size_t i = _next;
94+
memcpy(dst, &_buffer[i], unit_sz);
95+
dom->log(rust_log::MEM | rust_log::COMM,
96+
"shifted data from index %d", i);
97+
_unread -= unit_sz;
98+
_next += unit_sz;
99+
I(dom, _next <= _buffer_sz);
100+
if (_next == _buffer_sz) {
101+
_next = 0;
102+
}
103+
104+
// Shrink if possible.
105+
if (_buffer_sz >= INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz &&
106+
_unread <= _buffer_sz / 4) {
107+
void *tmp = dom->malloc(_buffer_sz / 2);
108+
transfer(tmp);
109+
_buffer_sz >>= 1;
110+
dom->free(_buffer);
111+
_buffer = (uint8_t *)tmp;
112+
}
113+
}
114+
115+
bool
116+
circular_buffer::is_empty() {
117+
return _unread == 0;
118+
}

trunk/src/rt/circular_buffer.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
*/
4+
5+
#ifndef CIRCULAR_BUFFER_H
6+
#define CIRCULAR_BUFFER_H
7+
8+
class
9+
circular_buffer : public dom_owned<circular_buffer> {
10+
static const size_t INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS = 8;
11+
static const size_t MAX_CIRCULAR_BUFFFER_SIZE = 1 << 24;
12+
13+
public:
14+
rust_dom *dom;
15+
circular_buffer(rust_dom *dom, size_t unit_sz);
16+
~circular_buffer();
17+
void transfer(void *dst);
18+
void enqueue(void *src);
19+
void dequeue(void *dst);
20+
bool is_empty();
21+
22+
private:
23+
size_t _buffer_sz;
24+
size_t unit_sz;
25+
size_t _next;
26+
size_t _unread;
27+
uint8_t *_buffer;
28+
};
29+
30+
#endif /* CIRCULAR_BUFFER_H */

trunk/src/rt/globals.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef GLOBALS_H
2+
#define GLOBALS_H
3+
4+
#define __STDC_LIMIT_MACROS 1
5+
#define __STDC_CONSTANT_MACROS 1
6+
#define __STDC_FORMAT_MACROS 1
7+
8+
#include <stdlib.h>
9+
#include <stdint.h>
10+
#include <inttypes.h>
11+
12+
#include <stdio.h>
13+
#include <string.h>
14+
15+
#if defined(__WIN32__)
16+
extern "C" {
17+
#include <windows.h>
18+
#include <tchar.h>
19+
#include <wincrypt.h>
20+
}
21+
#elif defined(__GNUC__)
22+
#include <unistd.h>
23+
#include <sys/types.h>
24+
#include <sys/stat.h>
25+
#include <fcntl.h>
26+
#include <dlfcn.h>
27+
#include <pthread.h>
28+
#include <errno.h>
29+
#else
30+
#error "Platform not supported."
31+
#endif
32+
33+
#endif /* GLOBALS_H */

trunk/src/rt/rust.cpp

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "rust_internal.h"
22
#include "util/array_list.h"
33

4-
54
// #define TRACK_ALLOCATIONS
65
// For debugging, keeps track of live allocations, so you can find out
76
// exactly what leaked.
@@ -100,52 +99,6 @@ rust_srv::clone()
10099
return new rust_srv();
101100
}
102101

103-
104-
int
105-
rust_main_loop(rust_dom *dom)
106-
{
107-
// Make sure someone is watching, to pull us out of infinite loops.
108-
rust_timer timer(*dom);
109-
110-
int rval;
111-
rust_task *task;
112-
113-
dom->log(rust_log::DOM,
114-
"running main-loop on domain 0x%" PRIxPTR, dom);
115-
dom->logptr("exit-task glue",
116-
dom->root_crate->get_exit_task_glue());
117-
118-
while ((task = dom->sched()) != NULL) {
119-
I(dom, task->running());
120-
121-
dom->log(rust_log::TASK,
122-
"activating task 0x%" PRIxPTR ", sp=0x%" PRIxPTR,
123-
(uintptr_t)task, task->rust_sp);
124-
125-
dom->interrupt_flag = 0;
126-
127-
dom->activate(task);
128-
129-
dom->log(rust_log::TASK,
130-
"returned from task 0x%" PRIxPTR
131-
" in state '%s', sp=0x%" PRIxPTR,
132-
(uintptr_t)task,
133-
dom->state_vec_name(task->state),
134-
task->rust_sp);
135-
136-
I(dom, task->rust_sp >= (uintptr_t) &task->stk->data[0]);
137-
I(dom, task->rust_sp < task->stk->limit);
138-
139-
dom->reap_dead_tasks();
140-
}
141-
142-
dom->log(rust_log::DOM, "finished main-loop (dom.rval = %d)", dom->rval);
143-
rval = dom->rval;
144-
145-
return rval;
146-
}
147-
148-
149102
struct
150103
command_line_args
151104
{
@@ -243,7 +196,7 @@ rust_start(uintptr_t main_fn, rust_crate const *crate, int argc, char **argv)
243196
(uintptr_t)&main_args,
244197
sizeof(main_args));
245198

246-
ret = rust_main_loop(&dom);
199+
ret = dom.start_main_loop();
247200
}
248201

249202
#if !defined(__WIN32__)

trunk/src/rt/rust_builtin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ str_alloc(rust_task *task, size_t n_bytes)
1919
extern "C" CDECL rust_str*
2020
last_os_error(rust_task *task) {
2121
rust_dom *dom = task->dom;
22-
dom->log(rust_log::TASK, "last_os_error()");
22+
task->log(rust_log::TASK, "last_os_error()");
2323

2424
#if defined(__WIN32__)
2525
LPTSTR buf;
@@ -95,7 +95,7 @@ extern "C" CDECL rust_vec*
9595
vec_alloc(rust_task *task, type_desc *t, size_t n_elts)
9696
{
9797
rust_dom *dom = task->dom;
98-
dom->log(rust_log::MEM,
98+
task->log(rust_log::MEM,
9999
"vec_alloc %" PRIdPTR " elements of size %" PRIdPTR,
100100
n_elts, t->size);
101101
size_t fill = n_elts * t->size;

0 commit comments

Comments
 (0)