2011-10-03 20:04:43 -05:00
|
|
|
/* Native builtins. */
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#include "rust_internal.h"
|
2011-10-03 20:04:43 -05:00
|
|
|
#include "rust_scheduler.h"
|
2011-12-30 22:46:08 -06:00
|
|
|
#include "rust_task.h"
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if !defined(__WIN32__)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-02 19:00:40 -05:00
|
|
|
extern "C" CDECL rust_str*
|
2011-10-20 05:32:43 -05:00
|
|
|
last_os_error() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_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
|
|
|
|
|
2011-09-02 19:00:40 -05:00
|
|
|
rust_str * st = make_str(task->kernel, buf, strlen(buf),
|
|
|
|
"last_os_error");
|
2010-06-23 23:03:09 -05:00
|
|
|
#ifdef __WIN32__
|
|
|
|
LocalFree((HLOCAL)buf);
|
|
|
|
#endif
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2011-09-02 19:00:40 -05:00
|
|
|
extern "C" CDECL rust_str *
|
2011-10-20 05:32:43 -05:00
|
|
|
rust_getcwd() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-06-17 13:12:51 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:56:07 -05:00
|
|
|
return make_str(task->kernel, cbuf, strlen(cbuf), "rust_str(getcwd");
|
2011-06-17 13:12:51 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 20:04:43 -05:00
|
|
|
// TODO: Allow calling native functions that return double results.
|
2011-03-24 20:20:49 -05:00
|
|
|
extern "C" CDECL
|
2011-10-20 05:32:43 -05:00
|
|
|
void squareroot(double *input, double *output) {
|
2011-03-24 20:20:49 -05:00
|
|
|
*output = sqrt(*input);
|
|
|
|
}
|
|
|
|
|
2011-08-11 12:46:57 -05:00
|
|
|
extern "C" CDECL void
|
2011-10-20 19:23:21 -05:00
|
|
|
leak(void *thing) {
|
2011-08-11 12:46:57 -05:00
|
|
|
// 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
|
2011-10-21 13:06:33 -05:00
|
|
|
refcount(intptr_t *v) {
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
do_gc() {
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
unsupervise() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2010-07-05 16:43:40 -05:00
|
|
|
task->unsupervise();
|
|
|
|
}
|
|
|
|
|
2011-08-25 03:18:02 -05:00
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
vec_reserve_shared(type_desc* ty, rust_vec** vp,
|
2011-10-03 20:04:43 -05:00
|
|
|
size_t n_elts) {
|
|
|
|
rust_task *task = rust_scheduler::get_task();
|
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*
|
2011-10-20 05:32:43 -05:00
|
|
|
vec_from_buf_shared(type_desc *ty, void *ptr, size_t count) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-08-25 03:18:02 -05:00
|
|
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
rust_str_push(rust_vec** sp, uint8_t byte) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-08-29 15:46:25 -05:00
|
|
|
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 *
|
2011-10-20 05:32:43 -05:00
|
|
|
rand_new() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_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
|
2011-10-20 05:32:43 -05:00
|
|
|
rand_next(randctx *rctx) {
|
2011-09-20 17:34:47 -05:00
|
|
|
return isaac_rand(rctx);
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
rand_free(randctx *rctx) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2010-07-25 23:45:09 -05:00
|
|
|
task->free(rctx);
|
|
|
|
}
|
|
|
|
|
2011-10-03 20:04:43 -05:00
|
|
|
/* Debug builtins for std::dbg. */
|
2010-08-24 20:37:42 -05:00
|
|
|
|
|
|
|
static void
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(type_desc *t)
|
|
|
|
{
|
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " size %" PRIdPTR ", align %" PRIdPTR
|
2011-09-23 00:15:25 -05:00
|
|
|
", first_param 0x%" PRIxPTR,
|
|
|
|
t->size, t->align, t->first_param);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_tydesc(type_desc *t) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_tydesc");
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(t);
|
2010-08-24 20:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_opaque(type_desc *t, uint8_t *front) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_opaque");
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(t);
|
2010-08-24 20:37:42 -05:00
|
|
|
// 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-09-20 15:57:04 -05:00
|
|
|
struct rust_box {
|
|
|
|
RUST_REFCOUNTED(rust_box)
|
2011-07-12 17:14:57 -05:00
|
|
|
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_box(type_desc *t, rust_box *box) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_box(0x%" PRIxPTR ")", box);
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(t);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, " refcount %" PRIdPTR,
|
2011-09-02 14:21:01 -05:00
|
|
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_tag(type_desc *t, rust_tag *tag) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_tag");
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(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;
|
2011-09-20 15:57:04 -05:00
|
|
|
rust_box *body;
|
2010-08-24 20:37:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_obj(type_desc *t, rust_obj *obj, size_t nmethods, size_t nbytes) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_obj with %" PRIdPTR " methods", nmethods);
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(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;
|
2011-09-20 15:57:04 -05:00
|
|
|
rust_box *closure;
|
2010-08-24 20:37:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_fn(type_desc *t, rust_fn *fn) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_fn");
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(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 *
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_ptrcast(type_desc *from_ty,
|
2010-09-07 01:24:01 -05:00
|
|
|
type_desc *to_ty,
|
2011-10-20 05:32:43 -05:00
|
|
|
void *ptr) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "debug_ptrcast from");
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(from_ty);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(task, stdlib, "to");
|
2011-10-20 19:06:52 -05:00
|
|
|
debug_tydesc_helper(to_ty);
|
2010-09-07 01:24:01 -05:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2011-12-05 19:20:59 -06:00
|
|
|
extern "C" CDECL void *
|
|
|
|
debug_get_stk_seg() {
|
|
|
|
rust_task *task = rust_scheduler::get_task();
|
|
|
|
return task->stk;
|
|
|
|
}
|
|
|
|
|
2011-09-01 17:51:27 -05:00
|
|
|
extern "C" CDECL rust_vec*
|
2011-10-19 23:53:02 -05:00
|
|
|
rust_list_files(rust_str *path) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-09-02 19:00:40 -05:00
|
|
|
array_list<rust_str*> strings;
|
2011-07-12 17:14:57 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
WIN32_FIND_DATA FindFileData;
|
2011-10-19 23:53:02 -05:00
|
|
|
HANDLE hFind = FindFirstFile((char*)path->data, &FindFileData);
|
2011-07-12 17:14:57 -05:00
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
2011-09-02 19:00:40 -05:00
|
|
|
rust_str *str = make_str(task->kernel, FindFileData.cFileName,
|
|
|
|
strlen(FindFileData.cFileName),
|
|
|
|
"list_files_str");
|
2011-09-01 17:51:27 -05:00
|
|
|
strings.push(str);
|
2011-07-12 17:14:57 -05:00
|
|
|
} while (FindNextFile(hFind, &FindFileData));
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
#else
|
2011-10-19 23:53:02 -05:00
|
|
|
DIR *dirp = opendir((char*)path->data);
|
2011-07-12 17:14:57 -05:00
|
|
|
if (dirp) {
|
|
|
|
struct dirent *dp;
|
2011-09-01 17:51:27 -05:00
|
|
|
while ((dp = readdir(dirp))) {
|
2011-09-02 18:56:07 -05:00
|
|
|
rust_vec *str = make_str(task->kernel, dp->d_name,
|
2011-09-01 17:51:27 -05:00
|
|
|
strlen(dp->d_name),
|
|
|
|
"list_files_str");
|
|
|
|
strings.push(str);
|
|
|
|
}
|
2011-07-12 17:14:57 -05:00
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-01 17:51:27 -05:00
|
|
|
|
|
|
|
rust_vec *vec = (rust_vec *)
|
|
|
|
task->kernel->malloc(vec_size<rust_vec*>(strings.size()),
|
|
|
|
"list_files_vec");
|
|
|
|
size_t alloc_sz = sizeof(rust_vec*) * strings.size();
|
2011-08-25 03:18:02 -05:00
|
|
|
vec->fill = vec->alloc = alloc_sz;
|
|
|
|
memcpy(&vec->data[0], strings.data(), alloc_sz);
|
2011-09-01 17:51:27 -05:00
|
|
|
return vec;
|
2011-07-12 17:14:57 -05:00
|
|
|
}
|
|
|
|
|
2011-03-10 08:56:51 -06:00
|
|
|
extern "C" CDECL int
|
2011-12-16 16:32:09 -06:00
|
|
|
rust_path_is_dir(char *path) {
|
2011-03-10 08:56:51 -06:00
|
|
|
struct stat buf;
|
2011-09-01 14:58:24 -05:00
|
|
|
if (stat(path, &buf)) {
|
2011-09-01 14:38:36 -05:00
|
|
|
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-12-16 16:32:09 -06:00
|
|
|
extern "C" CDECL int
|
|
|
|
rust_path_exists(char *path) {
|
|
|
|
struct stat buf;
|
|
|
|
if (stat(path, &buf)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
rust_ptr_eq(type_desc *t, rust_box *a, rust_box *b) {
|
2011-04-21 18:44:17 -05:00
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
get_time(uint32_t *sec, uint32_t *usec) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-04-29 13:54:06 -05:00
|
|
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
get_time(uint32_t *sec, uint32_t *usec) {
|
2011-04-29 13:54:06 -05:00
|
|
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
nano_time(uint64_t *ns) {
|
2011-06-28 19:58:44 -05:00
|
|
|
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-10-20 05:32:43 -05:00
|
|
|
pin_task() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-06-29 20:47:47 -05:00
|
|
|
task->pin();
|
|
|
|
}
|
|
|
|
|
2011-07-05 16:28:50 -05:00
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
unpin_task() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-06-29 20:47:47 -05:00
|
|
|
task->unpin();
|
|
|
|
}
|
2011-06-16 12:32:52 -05:00
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
extern "C" CDECL rust_task_id
|
2011-10-20 05:32:43 -05:00
|
|
|
get_task_id() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_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
|
2011-10-20 05:32:43 -05:00
|
|
|
new_task() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-08-10 20:48:57 -05:00
|
|
|
return task->kernel->create_task(task, NULL);
|
|
|
|
}
|
|
|
|
|
2011-08-13 20:15:56 -05:00
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
drop_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 *
|
2011-10-20 05:32:43 -05:00
|
|
|
get_task_pointer(rust_task_id id) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-08-17 16:42:28 -05:00
|
|
|
return task->kernel->get_task_by_id(id);
|
2011-08-10 20:48:57 -05:00
|
|
|
}
|
|
|
|
|
2011-11-30 19:54:11 -06:00
|
|
|
extern "C" rust_task *
|
|
|
|
rust_get_task() {
|
|
|
|
return rust_scheduler::get_task();
|
|
|
|
}
|
|
|
|
|
2011-09-06 16:03:20 -05:00
|
|
|
struct fn_env_pair {
|
|
|
|
intptr_t f;
|
|
|
|
intptr_t env;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
start_task(rust_task_id id, fn_env_pair *f) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-09-06 16:03:20 -05:00
|
|
|
rust_task *target = task->kernel->get_task_by_id(id);
|
2011-12-30 22:46:08 -06:00
|
|
|
target->start((spawn_fn)f->f, f->env);
|
2011-09-06 16:03:20 -05:00
|
|
|
target->deref();
|
2011-08-10 20:48:57 -05:00
|
|
|
}
|
|
|
|
|
2011-08-12 18:36:17 -05:00
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
migrate_alloc(void *alloc, rust_task_id tid) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-08-12 18:36:17 -05:00
|
|
|
if(!alloc) return;
|
|
|
|
rust_task *target = task->kernel->get_task_by_id(tid);
|
|
|
|
if(target) {
|
2011-09-26 17:06:26 -05:00
|
|
|
const type_desc *tydesc = task->release_alloc(alloc);
|
|
|
|
target->claim_alloc(alloc, tydesc);
|
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
|
2011-10-20 05:32:43 -05:00
|
|
|
set_min_stack(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
|
2011-10-20 05:32:43 -05:00
|
|
|
sched_threads() {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-07-25 20:07:20 -05:00
|
|
|
return task->kernel->num_threads;
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
extern "C" CDECL rust_port*
|
2011-10-20 05:32:43 -05:00
|
|
|
new_port(size_t unit_sz) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-07-29 19:53:02 -05:00
|
|
|
LOG(task, comm, "new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
|
|
|
|
(uintptr_t) task, task->name, unit_sz);
|
2011-11-11 13:46:07 -06:00
|
|
|
// port starts with refcount == 1
|
2011-07-29 19:53:02 -05:00
|
|
|
return new (task->kernel, "rust_port") rust_port(task, unit_sz);
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:44:08 -06:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_port_detach(rust_port *port) {
|
|
|
|
rust_task *task = rust_scheduler::get_task();
|
|
|
|
LOG(task, comm, "rust_port_detach(0x%" PRIxPTR ")", (uintptr_t) port);
|
|
|
|
port->detach();
|
|
|
|
// FIXME: Busy waiting until we're the only ref
|
|
|
|
bool done = false;
|
|
|
|
while (!done) {
|
|
|
|
scoped_lock with(port->lock);
|
|
|
|
done = port->ref_count == 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:53:02 -05:00
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
del_port(rust_port *port) {
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-07-29 19:53:02 -05:00
|
|
|
LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
|
2011-11-16 18:44:08 -06:00
|
|
|
A(task->sched, port->ref_count == 1, "Expected port ref_count == 1");
|
2011-11-11 13:46:07 -06:00
|
|
|
port->deref();
|
2011-07-29 19:53:02 -05:00
|
|
|
}
|
|
|
|
|
2011-11-13 17:28:59 -06:00
|
|
|
extern "C" CDECL size_t
|
|
|
|
rust_port_size(rust_port *port) {
|
|
|
|
return port->size();
|
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
extern "C" CDECL rust_port_id
|
2011-10-20 05:32:43 -05:00
|
|
|
get_port_id(rust_port *port) {
|
2011-08-09 18:07:49 -05:00
|
|
|
return port->id;
|
|
|
|
}
|
|
|
|
|
2011-11-14 15:49:56 -06:00
|
|
|
extern "C" CDECL uintptr_t
|
2011-10-20 05:32:43 -05:00
|
|
|
chan_id_send(type_desc *t, rust_task_id target_task_id,
|
2011-08-09 18:07:49 -05:00
|
|
|
rust_port_id target_port_id, void *sptr) {
|
|
|
|
// FIXME: make sure this is thread-safe
|
2011-11-14 15:49:56 -06:00
|
|
|
bool sent = false;
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-08-09 18:07:49 -05:00
|
|
|
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) {
|
2011-11-10 19:53:19 -06:00
|
|
|
port->send(sptr);
|
2011-11-11 17:34:35 -06:00
|
|
|
scoped_lock with(target_task->lock);
|
2011-11-11 13:46:07 -06:00
|
|
|
port->deref();
|
2011-11-14 15:49:56 -06:00
|
|
|
sent = true;
|
2011-08-09 18:07:49 -05:00
|
|
|
}
|
2011-11-11 17:34:35 -06:00
|
|
|
target_task->deref();
|
2011-08-09 18:07:49 -05:00
|
|
|
}
|
2011-11-14 15:49:56 -06:00
|
|
|
return (uintptr_t)sent;
|
2011-08-09 18:07:49 -05:00
|
|
|
}
|
|
|
|
|
2011-11-18 18:02:48 -06:00
|
|
|
// This is called by an intrinsic on the Rust stack and must run
|
|
|
|
// entirely in the red zone. Do not call on the C stack.
|
2011-11-08 17:38:56 -06:00
|
|
|
extern "C" CDECL void
|
2011-11-18 17:36:48 -06:00
|
|
|
rust_task_sleep(rust_task *task, size_t time_in_us, bool *killed) {
|
|
|
|
task->yield(time_in_us, killed);
|
2011-11-08 17:38:56 -06:00
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
extern "C" CDECL void
|
2011-11-18 16:45:48 -06:00
|
|
|
port_recv(uintptr_t *dptr, rust_port *port,
|
|
|
|
uintptr_t *yield, uintptr_t *killed) {
|
|
|
|
*yield = false;
|
|
|
|
*killed = false;
|
2011-10-03 20:04:43 -05:00
|
|
|
rust_task *task = rust_scheduler::get_task();
|
2011-08-01 16:57:17 -05:00
|
|
|
{
|
|
|
|
scoped_lock with(port->lock);
|
|
|
|
|
|
|
|
LOG(task, comm, "port: 0x%" PRIxPTR ", dptr: 0x%" PRIxPTR
|
2011-11-10 18:10:57 -06:00
|
|
|
", size: 0x%" PRIxPTR,
|
|
|
|
(uintptr_t) port, (uintptr_t) dptr, port->unit_sz);
|
2011-08-01 16:57:17 -05:00
|
|
|
|
|
|
|
if (port->receive(dptr)) {
|
2011-11-18 16:45:48 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this task has been killed then we're not going to bother
|
|
|
|
// blocking, we have to unwind.
|
|
|
|
if (task->killed) {
|
|
|
|
*killed = true;
|
2011-08-01 16:57:17 -05:00
|
|
|
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");
|
|
|
|
}
|
2011-11-18 14:39:13 -06:00
|
|
|
*yield = true;
|
|
|
|
return;
|
2011-08-01 16:57:17 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// End:
|
|
|
|
//
|