2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if !defined(__WIN32__)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
/* Native builtins. */
|
|
|
|
|
|
|
|
extern "C" CDECL rust_str*
|
|
|
|
last_os_error(rust_task *task) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, task, "last_os_error()");
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
LPTSTR buf;
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
DWORD res = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) &buf, 0, NULL);
|
|
|
|
if (!res) {
|
2011-07-13 16:04:38 -05:00
|
|
|
task->fail();
|
2010-06-23 23:03:09 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#elif defined(_GNU_SOURCE)
|
2011-01-14 15:41:39 -06:00
|
|
|
char cbuf[BUF_BYTES];
|
2010-06-23 23:03:09 -05:00
|
|
|
char *buf = strerror_r(errno, cbuf, sizeof(cbuf));
|
|
|
|
if (!buf) {
|
2011-07-13 15:43:35 -05:00
|
|
|
task->fail();
|
2010-06-23 23:03:09 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
2011-01-14 15:41:39 -06:00
|
|
|
char buf[BUF_BYTES];
|
2010-06-23 23:03:09 -05:00
|
|
|
int err = strerror_r(errno, buf, sizeof(buf));
|
|
|
|
if (err) {
|
2011-07-13 16:04:38 -05:00
|
|
|
task->fail();
|
2010-06-23 23:03:09 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
size_t fill = strlen(buf) + 1;
|
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_str) + fill);
|
2011-07-18 14:02:26 -05:00
|
|
|
void *mem = task->malloc(alloc, "rust_str(last_os_error)");
|
2010-06-23 23:03:09 -05:00
|
|
|
if (!mem) {
|
2011-07-13 15:43:35 -05:00
|
|
|
task->fail();
|
2010-06-23 23:03:09 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-07-18 14:02:26 -05:00
|
|
|
rust_str *st = new (mem) rust_str(alloc, fill,
|
2011-06-29 17:11:20 -05:00
|
|
|
(const uint8_t *)buf);
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#ifdef __WIN32__
|
|
|
|
LocalFree((HLOCAL)buf);
|
|
|
|
#endif
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2011-06-17 13:12:51 -05:00
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
rust_getcwd(rust_task *task) {
|
|
|
|
LOG(task, task, "rust_getcwd()");
|
|
|
|
|
|
|
|
char cbuf[BUF_BYTES];
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
if (!_getcwd(cbuf, sizeof(cbuf))) {
|
|
|
|
#else
|
|
|
|
if (!getcwd(cbuf, sizeof(cbuf))) {
|
|
|
|
#endif
|
2011-07-13 15:43:35 -05:00
|
|
|
task->fail();
|
2011-06-17 13:12:51 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t fill = strlen(cbuf) + 1;
|
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_str) + fill);
|
2011-07-18 14:02:26 -05:00
|
|
|
void *mem = task->malloc(alloc, "rust_str(getcwd)");
|
2011-06-17 13:12:51 -05:00
|
|
|
if (!mem) {
|
2011-07-13 15:43:35 -05:00
|
|
|
task->fail();
|
2011-06-17 13:12:51 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_str *st;
|
2011-07-18 14:02:26 -05:00
|
|
|
st = new (mem) rust_str(alloc, fill, (const uint8_t *)cbuf);
|
2011-06-17 13:12:51 -05:00
|
|
|
|
2011-07-13 17:44:09 -05:00
|
|
|
return st;
|
2011-06-17 13:12:51 -05:00
|
|
|
}
|
|
|
|
|
2011-03-24 20:20:49 -05:00
|
|
|
extern "C" CDECL
|
|
|
|
void squareroot(rust_task *task, double *input, double *output) {
|
|
|
|
*output = sqrt(*input);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
extern "C" CDECL size_t
|
|
|
|
size_of(rust_task *task, type_desc *t) {
|
|
|
|
return t->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL size_t
|
|
|
|
align_of(rust_task *task, type_desc *t) {
|
|
|
|
return t->align;
|
|
|
|
}
|
|
|
|
|
2011-08-11 12:46:57 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
leak(rust_task *task, type_desc *t, void *thing) {
|
|
|
|
// Do nothing. Call this with move-mode in order to say "Don't worry rust,
|
|
|
|
// I'll take care of this."
|
|
|
|
}
|
|
|
|
|
2010-11-09 16:15:07 -06:00
|
|
|
extern "C" CDECL intptr_t
|
|
|
|
refcount(rust_task *task, type_desc *t, intptr_t *v) {
|
|
|
|
|
|
|
|
if (*v == CONST_REFCOUNT)
|
|
|
|
return CONST_REFCOUNT;
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Passed-in value has refcount 1 too high
|
|
|
|
// because it was ref'ed while making the call.
|
|
|
|
return (*v) - 1;
|
|
|
|
}
|
|
|
|
|
2010-06-26 01:57:30 -05:00
|
|
|
extern "C" CDECL void
|
2010-11-02 13:11:58 -05:00
|
|
|
do_gc(rust_task *task) {
|
2011-08-19 21:17:05 -05:00
|
|
|
// TODO
|
2010-06-26 01:57:30 -05:00
|
|
|
}
|
|
|
|
|
2010-07-05 16:43:40 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
unsupervise(rust_task *task) {
|
|
|
|
task->unsupervise();
|
|
|
|
}
|
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
/* Helper for str_alloc and str_from_vec. Returns NULL as failure. */
|
2011-08-18 16:43:17 -05:00
|
|
|
static rust_evec*
|
2011-03-10 08:56:51 -06:00
|
|
|
vec_alloc_with_data(rust_task *task,
|
|
|
|
size_t n_elts,
|
2010-08-11 18:06:45 -05:00
|
|
|
size_t fill,
|
2011-03-10 08:56:51 -06:00
|
|
|
size_t elt_size,
|
|
|
|
void *d)
|
2010-08-11 18:06:45 -05:00
|
|
|
{
|
2011-08-18 16:43:17 -05:00
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_evec) + (n_elts * elt_size));
|
|
|
|
void *mem = task->malloc(alloc, "rust_evec (with data)");
|
2011-03-10 08:56:51 -06:00
|
|
|
if (!mem) return NULL;
|
2011-08-18 16:43:17 -05:00
|
|
|
return new (mem) rust_evec(alloc, fill * elt_size, (uint8_t*)d);
|
2010-08-11 18:06:45 -05:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
extern "C" CDECL char const *
|
|
|
|
str_buf(rust_task *task, rust_str *s)
|
|
|
|
{
|
|
|
|
return (char const *)&s->data[0];
|
|
|
|
}
|
|
|
|
|
2010-08-11 18:06:45 -05:00
|
|
|
extern "C" CDECL rust_str *
|
2011-08-25 03:18:02 -05:00
|
|
|
str_from_vec(rust_task *task, rust_vec **vp)
|
2011-07-09 00:23:11 -05:00
|
|
|
{
|
2011-08-25 03:18:02 -05:00
|
|
|
rust_vec* v = *vp;
|
|
|
|
rust_str *st = vec_alloc_with_data(task,
|
|
|
|
v->fill + 1, // +1 for \0
|
|
|
|
v->fill,
|
|
|
|
1,
|
|
|
|
&v->data[0]);
|
2011-07-09 00:23:11 -05:00
|
|
|
if (!st) {
|
2011-07-13 15:43:35 -05:00
|
|
|
task->fail();
|
2011-07-09 00:23:11 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
st->data[st->fill++] = '\0';
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2011-08-25 03:18:02 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
vec_reserve_shared(rust_task* task, type_desc* ty, rust_vec** vp,
|
|
|
|
size_t n_elts) {
|
2011-08-29 15:35:29 -05:00
|
|
|
reserve_vec(task, vp, n_elts * ty->size);
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies elements in an unsafe buffer to the given interior vector. The
|
|
|
|
* vector must have size zero.
|
|
|
|
*/
|
|
|
|
extern "C" CDECL rust_vec*
|
|
|
|
vec_from_buf_shared(rust_task *task, type_desc *ty,
|
|
|
|
void *ptr, size_t count) {
|
|
|
|
size_t fill = ty->size * count;
|
|
|
|
rust_vec* v = (rust_vec*)task->kernel->malloc(fill + sizeof(rust_vec),
|
|
|
|
"vec_from_buf");
|
|
|
|
v->fill = v->alloc = fill;
|
|
|
|
memmove(&v->data[0], ptr, fill);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2011-08-29 15:46:25 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_istr_push(rust_task* task, rust_vec** sp, uint8_t byte) {
|
|
|
|
size_t fill = (*sp)->fill;
|
|
|
|
reserve_vec(task, sp, fill + 1);
|
|
|
|
(*sp)->data[fill-1] = byte;
|
|
|
|
(*sp)->data[fill] = 0;
|
|
|
|
(*sp)->fill = fill + 1;
|
|
|
|
}
|
|
|
|
|
2010-07-25 23:45:09 -05:00
|
|
|
extern "C" CDECL void *
|
|
|
|
rand_new(rust_task *task)
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched = task->sched;
|
2011-07-18 14:02:26 -05:00
|
|
|
randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "randctx");
|
2010-07-25 23:45:09 -05:00
|
|
|
if (!rctx) {
|
2011-07-13 15:43:35 -05:00
|
|
|
task->fail();
|
2010-07-25 23:45:09 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-28 14:15:41 -05:00
|
|
|
isaac_init(sched, rctx);
|
2010-07-25 23:45:09 -05:00
|
|
|
return rctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL size_t
|
|
|
|
rand_next(rust_task *task, randctx *rctx)
|
|
|
|
{
|
|
|
|
return rand(rctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rand_free(rust_task *task, randctx *rctx)
|
|
|
|
{
|
|
|
|
task->free(rctx);
|
|
|
|
}
|
|
|
|
|
2010-08-11 23:23:34 -05:00
|
|
|
extern "C" CDECL void upcall_sleep(rust_task *task, size_t time_in_us);
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
task_sleep(rust_task *task, size_t time_in_us) {
|
|
|
|
upcall_sleep(task, time_in_us);
|
|
|
|
}
|
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
task_yield(rust_task *task) {
|
|
|
|
task->yield(1);
|
|
|
|
}
|
|
|
|
|
2011-07-14 21:39:53 -05:00
|
|
|
extern "C" CDECL intptr_t
|
2011-08-08 15:38:20 -05:00
|
|
|
task_join(rust_task *task, rust_task_id tid) {
|
2011-06-13 20:19:08 -05:00
|
|
|
// If the other task is already dying, we don't have to wait for it.
|
2011-08-12 18:36:17 -05:00
|
|
|
rust_task *join_task = task->kernel->get_task_by_id(tid);
|
2011-08-08 15:38:20 -05:00
|
|
|
// FIXME: find task exit status and return that.
|
|
|
|
if(!join_task) return 0;
|
2011-07-07 20:18:52 -05:00
|
|
|
join_task->lock.lock();
|
2011-06-13 20:19:08 -05:00
|
|
|
if (join_task->dead() == false) {
|
|
|
|
join_task->tasks_waiting_to_join.push(task);
|
|
|
|
task->block(join_task, "joining local task");
|
2011-07-07 20:18:52 -05:00
|
|
|
join_task->lock.unlock();
|
2011-06-13 20:19:08 -05:00
|
|
|
task->yield(2);
|
|
|
|
}
|
2011-07-07 20:18:52 -05:00
|
|
|
else {
|
|
|
|
join_task->lock.unlock();
|
|
|
|
}
|
2011-07-14 21:39:53 -05:00
|
|
|
if (!join_task->failed) {
|
2011-08-13 20:15:56 -05:00
|
|
|
join_task->deref();
|
2011-07-14 21:39:53 -05:00
|
|
|
return 0;
|
|
|
|
} else {
|
2011-08-13 20:15:56 -05:00
|
|
|
join_task->deref();
|
2011-07-14 21:39:53 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-31 19:44:54 -05:00
|
|
|
}
|
|
|
|
|
2010-08-24 20:37:42 -05:00
|
|
|
/* Debug builtins for std.dbg. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
debug_tydesc_helper(rust_task *task, type_desc *t)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " size %" PRIdPTR ", align %" PRIdPTR
|
|
|
|
", stateful %" PRIdPTR ", first_param 0x%" PRIxPTR,
|
|
|
|
t->size, t->align, t->is_stateful, t->first_param);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_tydesc(rust_task *task, type_desc *t)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_tydesc");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_opaque(rust_task *task, type_desc *t, uint8_t *front)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_opaque");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
|
|
|
// FIXME may want to actually account for alignment. `front` may not
|
|
|
|
// indeed be the front byte of the passed-in argument.
|
|
|
|
for (uintptr_t i = 0; i < t->size; ++front, ++i) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i, *front);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 01:33:37 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
hack_allow_leaks(rust_task *task)
|
|
|
|
{
|
|
|
|
LOG(task, stdlib, "hack_allow_leaks");
|
|
|
|
task->local_region.hack_allow_leaks();
|
|
|
|
}
|
|
|
|
|
2011-07-12 17:14:57 -05:00
|
|
|
struct rust_box {
|
|
|
|
RUST_REFCOUNTED(rust_box)
|
|
|
|
|
2010-08-24 20:37:42 -05:00
|
|
|
// FIXME `data` could be aligned differently from the actual box body data
|
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_box(rust_task *task, type_desc *t, rust_box *box)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_box(0x%" PRIxPTR ")", box);
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " refcount %" PRIdPTR,
|
|
|
|
box->ref_count == CONST_REFCOUNT
|
|
|
|
? CONST_REFCOUNT
|
|
|
|
: box->ref_count - 1); // -1 because we ref'ed for this call
|
2010-08-24 20:37:42 -05:00
|
|
|
for (uintptr_t i = 0; i < t->size; ++i) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i, box->data[i]);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rust_tag {
|
|
|
|
uintptr_t discriminant;
|
|
|
|
uint8_t variant[];
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_tag(rust_task *task, type_desc *t, rust_tag *tag)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_tag");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " discriminant %" PRIdPTR, tag->discriminant);
|
2010-08-24 20:37:42 -05:00
|
|
|
|
|
|
|
for (uintptr_t i = 0; i < t->size - sizeof(tag->discriminant); ++i)
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i,
|
|
|
|
tag->variant[i]);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rust_obj {
|
|
|
|
uintptr_t *vtbl;
|
|
|
|
rust_box *body;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2010-08-24 21:49:39 -05:00
|
|
|
debug_obj(rust_task *task, type_desc *t, rust_obj *obj,
|
|
|
|
size_t nmethods, size_t nbytes)
|
2010-08-24 20:37:42 -05:00
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_obj with %" PRIdPTR " methods", nmethods);
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " vtbl at 0x%" PRIxPTR, obj->vtbl);
|
|
|
|
LOG(task, stdlib, " body at 0x%" PRIxPTR, obj->body);
|
2010-08-24 20:37:42 -05:00
|
|
|
|
|
|
|
for (uintptr_t *p = obj->vtbl; p < obj->vtbl + nmethods; ++p)
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " vtbl word: 0x%" PRIxPTR, *p);
|
2010-08-24 21:49:39 -05:00
|
|
|
|
|
|
|
for (uintptr_t i = 0; i < nbytes; ++i)
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " body byte %" PRIdPTR ": 0x%" PRIxPTR,
|
|
|
|
i, obj->body->data[i]);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rust_fn {
|
|
|
|
uintptr_t *thunk;
|
|
|
|
rust_box *closure;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
debug_fn(rust_task *task, type_desc *t, rust_fn *fn)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_fn");
|
2010-08-24 20:37:42 -05:00
|
|
|
debug_tydesc_helper(task, t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " thunk at 0x%" PRIxPTR, fn->thunk);
|
|
|
|
LOG(task, stdlib, " closure at 0x%" PRIxPTR, fn->closure);
|
2010-09-30 19:39:37 -05:00
|
|
|
if (fn->closure) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " refcount %" PRIdPTR, fn->closure->ref_count);
|
2010-09-30 19:39:37 -05:00
|
|
|
}
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
2010-09-07 01:24:01 -05:00
|
|
|
extern "C" CDECL void *
|
|
|
|
debug_ptrcast(rust_task *task,
|
|
|
|
type_desc *from_ty,
|
|
|
|
type_desc *to_ty,
|
|
|
|
void *ptr)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_ptrcast from");
|
2010-09-07 01:24:01 -05:00
|
|
|
debug_tydesc_helper(task, from_ty);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "to");
|
2010-09-07 01:24:01 -05:00
|
|
|
debug_tydesc_helper(task, to_ty);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2010-12-31 12:35:39 -06:00
|
|
|
extern "C" CDECL void
|
|
|
|
debug_trap(rust_task *task, rust_str *s)
|
|
|
|
{
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "trapping: %s", s->data);
|
2010-12-31 12:35:39 -06:00
|
|
|
// FIXME: x86-ism.
|
|
|
|
__asm__("int3");
|
|
|
|
}
|
|
|
|
|
2011-03-10 08:56:51 -06:00
|
|
|
rust_str* c_str_to_rust(rust_task *task, char const *str) {
|
|
|
|
size_t len = strlen(str) + 1;
|
|
|
|
return vec_alloc_with_data(task, len, len, 1, (void*)str);
|
|
|
|
}
|
|
|
|
|
2011-07-12 17:14:57 -05:00
|
|
|
extern "C" CDECL rust_box*
|
2011-08-12 13:02:23 -05:00
|
|
|
rust_list_files(rust_task *task, rust_str *path) {
|
2011-07-12 17:14:57 -05:00
|
|
|
array_list<rust_str*> strings;
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
WIN32_FIND_DATA FindFileData;
|
|
|
|
HANDLE hFind = FindFirstFile((char*)path->data, &FindFileData);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
strings.push(c_str_to_rust(task, FindFileData.cFileName));
|
|
|
|
} while (FindNextFile(hFind, &FindFileData));
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
DIR *dirp = opendir((char*)path->data);
|
|
|
|
if (dirp) {
|
|
|
|
struct dirent *dp;
|
|
|
|
while ((dp = readdir(dirp)))
|
|
|
|
strings.push(c_str_to_rust(task, dp->d_name));
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-25 03:18:02 -05:00
|
|
|
rust_box *box =
|
|
|
|
(rust_box *)task->malloc(sizeof(rust_box) + sizeof(rust_vec*),
|
|
|
|
"rust_box(list_files_vec)");
|
|
|
|
rust_vec* vec =
|
|
|
|
(rust_vec*)task->kernel->malloc(vec_size<rust_str*>(strings.size()),
|
|
|
|
"list_files_vec");
|
2011-07-18 14:02:26 -05:00
|
|
|
|
2011-07-12 17:14:57 -05:00
|
|
|
box->ref_count = 1;
|
2011-08-25 03:18:02 -05:00
|
|
|
rust_vec** box_content = (rust_vec**)&box->data[0];
|
|
|
|
*box_content = vec;
|
|
|
|
size_t alloc_sz = sizeof(rust_str*) * strings.size();
|
|
|
|
vec->fill = vec->alloc = alloc_sz;
|
|
|
|
memcpy(&vec->data[0], strings.data(), alloc_sz);
|
2011-07-12 17:14:57 -05:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2011-03-25 02:09:20 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
rust_dirent_filename(rust_task *task, void* ent) {
|
|
|
|
return NULL;
|
2011-03-10 08:56:51 -06:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
extern "C" CDECL rust_str *
|
|
|
|
rust_dirent_filename(rust_task *task, dirent* ent) {
|
|
|
|
return c_str_to_rust(task, ent->d_name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" CDECL int
|
|
|
|
rust_file_is_dir(rust_task *task, rust_str *path) {
|
|
|
|
struct stat buf;
|
2011-09-01 14:38:36 -05:00
|
|
|
if (stat((char*)path->data, &buf)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-10 08:56:51 -06:00
|
|
|
return S_ISDIR(buf.st_mode);
|
|
|
|
}
|
2010-08-24 20:37:42 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
extern "C" CDECL FILE* rust_get_stdin() {return stdin;}
|
|
|
|
extern "C" CDECL FILE* rust_get_stdout() {return stdout;}
|
2011-08-24 23:19:56 -05:00
|
|
|
extern "C" CDECL FILE* rust_get_stderr() {return stderr;}
|
2011-03-10 09:02:53 -06:00
|
|
|
|
2011-04-21 18:44:17 -05:00
|
|
|
extern "C" CDECL int
|
|
|
|
rust_ptr_eq(rust_task *task, type_desc *t, rust_box *a, rust_box *b) {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
extern "C" CDECL void
|
|
|
|
get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
|
|
|
|
SYSTEMTIME systemTime;
|
|
|
|
FILETIME fileTime;
|
|
|
|
GetSystemTime(&systemTime);
|
|
|
|
if (!SystemTimeToFileTime(&systemTime, &fileTime)) {
|
2011-07-13 16:04:38 -05:00
|
|
|
task->fail();
|
2011-04-29 13:54:06 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This is probably completely wrong.
|
|
|
|
*sec = fileTime.dwHighDateTime;
|
|
|
|
*usec = fileTime.dwLowDateTime;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
extern "C" CDECL void
|
|
|
|
get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
*sec = tv.tv_sec;
|
|
|
|
*usec = tv.tv_usec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-28 19:58:44 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
nano_time(rust_task *task, uint64_t *ns) {
|
|
|
|
timer t;
|
2011-07-13 14:25:36 -05:00
|
|
|
*ns = t.time_ns();
|
2011-06-28 19:58:44 -05:00
|
|
|
}
|
|
|
|
|
2011-07-05 16:28:50 -05:00
|
|
|
extern "C" CDECL void
|
2011-06-29 20:47:47 -05:00
|
|
|
pin_task(rust_task *task) {
|
|
|
|
task->pin();
|
|
|
|
}
|
|
|
|
|
2011-07-05 16:28:50 -05:00
|
|
|
extern "C" CDECL void
|
2011-06-29 20:47:47 -05:00
|
|
|
unpin_task(rust_task *task) {
|
|
|
|
task->unpin();
|
|
|
|
}
|
2011-06-16 12:32:52 -05:00
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
extern "C" CDECL rust_task_id
|
|
|
|
get_task_id(rust_task *task) {
|
2011-08-17 16:42:28 -05:00
|
|
|
return task->user.id;
|
2011-08-09 18:07:49 -05:00
|
|
|
}
|
|
|
|
|
2011-08-10 20:48:57 -05:00
|
|
|
extern "C" CDECL rust_task_id
|
|
|
|
new_task(rust_task *task) {
|
|
|
|
return task->kernel->create_task(task, NULL);
|
|
|
|
}
|
|
|
|
|
2011-08-13 20:15:56 -05:00
|
|
|
extern "C" CDECL void
|
2011-08-17 16:42:28 -05:00
|
|
|
drop_task(rust_task *task, rust_task *target) {
|
2011-08-13 20:15:56 -05:00
|
|
|
if(target) {
|
|
|
|
target->deref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-10 20:48:57 -05:00
|
|
|
extern "C" CDECL rust_task *
|
|
|
|
get_task_pointer(rust_task *task, rust_task_id id) {
|
2011-08-17 16:42:28 -05:00
|
|
|
return task->kernel->get_task_by_id(id);
|
2011-08-10 20:48:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
start_task(rust_task *task, rust_task_id id) {
|
2011-08-11 12:46:57 -05:00
|
|
|
rust_task * target = task->kernel->get_task_by_id(id);
|
|
|
|
target->start();
|
2011-08-13 20:15:56 -05:00
|
|
|
target->deref();
|
2011-08-10 20:48:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *task_trampoline asm("task_trampoline");
|
|
|
|
|
|
|
|
extern "C" CDECL void **
|
|
|
|
get_task_trampoline(rust_task *task) {
|
|
|
|
return &task_trampoline;
|
|
|
|
}
|
|
|
|
|
2011-08-12 18:36:17 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
migrate_alloc(rust_task *task, void *alloc, rust_task_id tid) {
|
|
|
|
if(!alloc) return;
|
|
|
|
rust_task *target = task->kernel->get_task_by_id(tid);
|
|
|
|
if(target) {
|
|
|
|
task->local_region.release_alloc(alloc);
|
|
|
|
target->local_region.claim_alloc(alloc);
|
2011-08-13 20:15:56 -05:00
|
|
|
target->deref();
|
2011-08-12 18:36:17 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We couldn't find the target. Maybe we should just free?
|
|
|
|
task->fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 17:02:43 -05:00
|
|
|
// defined in rust_task.cpp
|
2011-07-27 16:34:39 -05:00
|
|
|
extern size_t g_custom_min_stack_size;
|
2011-07-25 17:02:43 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
set_min_stack(rust_task *task, uintptr_t stack_size) {
|
2011-07-27 16:34:39 -05:00
|
|
|
g_custom_min_stack_size = stack_size;
|
2011-07-25 17:02:43 -05:00
|
|
|
}
|
|
|
|
|
2011-07-25 20:07:20 -05:00
|
|
|
extern "C" CDECL int
|
|
|
|
sched_threads(rust_task *task) {
|
|
|
|
return task->kernel->num_threads;
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
extern "C" CDECL rust_port*
|
|
|
|
new_port(rust_task *task, size_t unit_sz) {
|
|
|
|
LOG(task, comm, "new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
|
|
|
|
(uintptr_t) task, task->name, unit_sz);
|
|
|
|
// take a reference on behalf of the port
|
|
|
|
task->ref();
|
|
|
|
return new (task->kernel, "rust_port") rust_port(task, unit_sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
del_port(rust_task *task, rust_port *port) {
|
|
|
|
LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
|
|
|
|
I(task->sched, !port->ref_count);
|
|
|
|
delete port;
|
|
|
|
|
|
|
|
// FIXME: this should happen in the port.
|
|
|
|
task->deref();
|
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
extern "C" CDECL rust_port_id
|
|
|
|
get_port_id(rust_task *task, rust_port *port) {
|
|
|
|
return port->id;
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
extern "C" CDECL rust_chan*
|
|
|
|
new_chan(rust_task *task, rust_port *port) {
|
|
|
|
rust_scheduler *sched = task->sched;
|
|
|
|
LOG(task, comm, "new_chan("
|
|
|
|
"task=0x%" PRIxPTR " (%s), port=0x%" PRIxPTR ")",
|
|
|
|
(uintptr_t) task, task->name, port);
|
|
|
|
I(sched, port);
|
|
|
|
return new (task->kernel, "rust_chan")
|
|
|
|
rust_chan(task->kernel, port, port->unit_sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL
|
|
|
|
void del_chan(rust_task *task, rust_chan *chan) {
|
|
|
|
LOG(task, comm, "del_chan(0x%" PRIxPTR ")", (uintptr_t) chan);
|
2011-08-06 12:07:39 -05:00
|
|
|
I(task->sched, false);
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
|
|
|
|
2011-08-05 17:16:48 -05:00
|
|
|
extern "C" CDECL
|
|
|
|
void take_chan(rust_task *task, rust_chan *chan) {
|
|
|
|
chan->ref();
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
extern "C" CDECL
|
|
|
|
void drop_chan(rust_task *task, rust_chan *chan) {
|
2011-08-05 17:16:48 -05:00
|
|
|
chan->deref();
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL
|
|
|
|
void drop_port(rust_task *, rust_port *port) {
|
|
|
|
port->ref_count--;
|
|
|
|
}
|
|
|
|
|
2011-08-01 16:57:17 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
chan_send(rust_task *task, rust_chan *chan, void *sptr) {
|
|
|
|
chan->send(sptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-08-09 18:07:49 -05:00
|
|
|
chan_id_send(rust_task *task, type_desc *t, rust_task_id target_task_id,
|
|
|
|
rust_port_id target_port_id, void *sptr) {
|
|
|
|
// FIXME: make sure this is thread-safe
|
|
|
|
rust_task *target_task = task->kernel->get_task_by_id(target_task_id);
|
|
|
|
if(target_task) {
|
|
|
|
rust_port *port = target_task->get_port_by_id(target_port_id);
|
|
|
|
if(port) {
|
|
|
|
port->remote_chan->send(sptr);
|
|
|
|
}
|
2011-08-13 20:15:56 -05:00
|
|
|
target_task->deref();
|
2011-08-09 18:07:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-08-01 16:57:17 -05:00
|
|
|
port_recv(rust_task *task, uintptr_t *dptr, rust_port *port) {
|
|
|
|
{
|
|
|
|
scoped_lock with(port->lock);
|
|
|
|
|
|
|
|
LOG(task, comm, "port: 0x%" PRIxPTR ", dptr: 0x%" PRIxPTR
|
|
|
|
", size: 0x%" PRIxPTR ", chan_no: %d",
|
|
|
|
(uintptr_t) port, (uintptr_t) dptr, port->unit_sz,
|
|
|
|
port->chans.length());
|
|
|
|
|
|
|
|
if (port->receive(dptr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No data was buffered on any incoming channel, so block this task on
|
|
|
|
// the port. Remember the rendezvous location so that any sender task
|
|
|
|
// can write to it before waking up this task.
|
|
|
|
|
|
|
|
LOG(task, comm, "<=== waiting for rendezvous data ===");
|
|
|
|
task->rendezvous_ptr = dptr;
|
|
|
|
task->block(port, "waiting for rendezvous data");
|
|
|
|
}
|
|
|
|
task->yield(3);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-10 14:56:42 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-06-23 23:03:09 -05:00
|
|
|
// End:
|
|
|
|
//
|