2012-06-27 15:14:04 -05:00
|
|
|
/* Foreign builtins. */
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-03-29 18:31:30 -05:00
|
|
|
#include "rust_sched_loop.h"
|
2011-12-30 22:46:08 -06:00
|
|
|
#include "rust_task.h"
|
2012-01-06 14:06:35 -06:00
|
|
|
#include "rust_util.h"
|
2012-02-03 17:12:18 -06:00
|
|
|
#include "rust_scheduler.h"
|
2012-02-02 18:24:32 -06:00
|
|
|
#include "sync/timer.h"
|
2012-02-28 13:58:50 -06:00
|
|
|
#include "rust_abi.h"
|
2012-04-02 22:18:01 -05:00
|
|
|
#include "rust_port.h"
|
2012-07-10 12:59:58 -05:00
|
|
|
#include "rust_box_annihilator.h"
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-04-03 12:32:26 -05:00
|
|
|
#include <time.h>
|
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-29 13:54:06 -05:00
|
|
|
#if !defined(__WIN32__)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2012-02-09 00:08:24 -06:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
extern char **environ;
|
|
|
|
#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() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2011-10-03 20:04:43 -05:00
|
|
|
|
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() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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;
|
|
|
|
}
|
|
|
|
|
2012-03-23 14:49:53 -05:00
|
|
|
return make_str(task->kernel, cbuf, strlen(cbuf), "rust_str(getcwd)");
|
2011-06-17 13:12:51 -05:00
|
|
|
}
|
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#if defined(__WIN32__)
|
2012-05-21 20:36:52 -05:00
|
|
|
extern "C" CDECL rust_vec_box *
|
2012-02-07 20:55:02 -06:00
|
|
|
rust_env_pairs() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-07 20:55:02 -06:00
|
|
|
size_t envc = 0;
|
|
|
|
LPTCH ch = GetEnvironmentStringsA();
|
|
|
|
LPTCH c;
|
|
|
|
for (c = ch; *c; c += strlen(c) + 1) {
|
|
|
|
++envc;
|
|
|
|
}
|
|
|
|
c = ch;
|
2012-05-23 19:12:02 -05:00
|
|
|
rust_vec_box *v = (rust_vec_box *)
|
|
|
|
task->kernel->malloc(vec_size<rust_vec_box*>(envc),
|
2012-02-07 20:55:02 -06:00
|
|
|
"str vec interior");
|
2012-05-23 19:12:02 -05:00
|
|
|
v->body.fill = v->body.alloc = sizeof(rust_vec*) * envc;
|
2012-02-07 20:55:02 -06:00
|
|
|
for (size_t i = 0; i < envc; ++i) {
|
|
|
|
size_t n = strlen(c);
|
|
|
|
rust_str *str = make_str(task->kernel, c, n, "str");
|
2012-05-21 20:36:52 -05:00
|
|
|
((rust_str**)&v->body.data)[i] = str;
|
2012-02-07 20:55:02 -06:00
|
|
|
c += n + 1;
|
|
|
|
}
|
|
|
|
if (ch) {
|
|
|
|
FreeEnvironmentStrings(ch);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
#else
|
2012-05-21 20:36:52 -05:00
|
|
|
extern "C" CDECL rust_vec_box *
|
2012-02-07 20:55:02 -06:00
|
|
|
rust_env_pairs() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-07 20:55:02 -06:00
|
|
|
#ifdef __APPLE__
|
|
|
|
char **environ = *_NSGetEnviron();
|
|
|
|
#endif
|
|
|
|
char **e = environ;
|
|
|
|
size_t envc = 0;
|
|
|
|
while (*e) {
|
|
|
|
++envc; ++e;
|
|
|
|
}
|
|
|
|
return make_str_vec(task->kernel, envc, environ);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-17 18:31:19 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
vec_reserve_shared_actual(type_desc* ty, rust_vec_box** vp,
|
|
|
|
size_t n_elts) {
|
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
reserve_vec_exact_shared(task, vp, n_elts * ty->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is completely misnamed.
|
2011-08-25 03:18:02 -05:00
|
|
|
extern "C" CDECL void
|
2012-05-21 20:36:52 -05:00
|
|
|
vec_reserve_shared(type_desc* ty, rust_vec_box** vp,
|
2011-10-03 20:04:43 -05:00
|
|
|
size_t n_elts) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-04 05:16:59 -06:00
|
|
|
reserve_vec_exact(task, vp, n_elts * ty->size);
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
extern "C" CDECL rust_vec*
|
|
|
|
rand_seed() {
|
|
|
|
size_t size = sizeof(ub4) * RANDSIZ;
|
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
rust_vec *v = (rust_vec *) task->kernel->malloc(vec_size<uint8_t>(size),
|
|
|
|
"rand_seed");
|
|
|
|
v->fill = v->alloc = size;
|
2012-07-25 17:01:48 -05:00
|
|
|
isaac_seed(task->kernel, (uint8_t*) &v->data, size);
|
2012-05-20 08:06:54 -05:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-07-25 23:45:09 -05:00
|
|
|
extern "C" CDECL void *
|
2011-10-20 05:32:43 -05:00
|
|
|
rand_new() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-03-29 18:31:30 -05:00
|
|
|
rust_sched_loop *thread = task->sched_loop;
|
2012-05-20 08:06:54 -05:00
|
|
|
randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "rand_new");
|
|
|
|
if (!rctx) {
|
|
|
|
task->fail();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
isaac_init(thread->kernel, rctx, NULL);
|
|
|
|
return rctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void *
|
2012-06-05 01:26:06 -05:00
|
|
|
rand_new_seeded(rust_vec_box* seed) {
|
2012-05-20 08:06:54 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
rust_sched_loop *thread = task->sched_loop;
|
|
|
|
randctx *rctx = (randctx *) task->malloc(sizeof(randctx),
|
|
|
|
"rand_new_seeded");
|
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;
|
|
|
|
}
|
2012-05-20 08:06:54 -05:00
|
|
|
isaac_init(thread->kernel, rctx, seed);
|
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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2010-07-25 23:45:09 -05:00
|
|
|
task->free(rctx);
|
|
|
|
}
|
|
|
|
|
2012-03-20 18:32:25 -05:00
|
|
|
|
|
|
|
/* Debug helpers strictly to verify ABI conformance.
|
|
|
|
*
|
2012-06-21 18:44:10 -05:00
|
|
|
* FIXME (#2665): move these into a testcase when the testsuite
|
|
|
|
* understands how to have explicit C files included.
|
2012-03-20 18:32:25 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct quad {
|
|
|
|
uint64_t a;
|
|
|
|
uint64_t b;
|
|
|
|
uint64_t c;
|
|
|
|
uint64_t d;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct floats {
|
|
|
|
double a;
|
|
|
|
uint8_t b;
|
|
|
|
double c;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" quad
|
|
|
|
debug_abi_1(quad q) {
|
|
|
|
quad qq = { q.c + 1,
|
|
|
|
q.d - 1,
|
|
|
|
q.a + 1,
|
|
|
|
q.b - 1 };
|
|
|
|
return qq;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" floats
|
|
|
|
debug_abi_2(floats f) {
|
|
|
|
floats ff = { f.c + 1.0,
|
|
|
|
0xff,
|
|
|
|
f.a - 1.0 };
|
|
|
|
return ff;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-06-18 14:48:47 -05:00
|
|
|
LOG(task, stdlib, " size %" PRIdPTR ", align %" PRIdPTR,
|
|
|
|
t->size, t->align);
|
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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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);
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2667) may want to actually account for alignment.
|
|
|
|
// `front` may not indeed be the front byte of the passed-in
|
|
|
|
// argument.
|
2010-08-24 20:37:42 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2667) this no longer reflects the actual structure of boxes!
|
2011-09-20 15:57:04 -05:00
|
|
|
struct rust_box {
|
|
|
|
RUST_REFCOUNTED(rust_box)
|
2011-07-12 17:14:57 -05:00
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2667) `data` could be aligned differently from the actual
|
|
|
|
// box body data
|
2010-08-24 20:37:42 -05:00
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2011-10-20 05:32:43 -05:00
|
|
|
debug_box(type_desc *t, rust_box *box) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2011-10-03 20:04:43 -05:00
|
|
|
|
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_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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2011-12-05 19:20:59 -06:00
|
|
|
return task->stk;
|
|
|
|
}
|
|
|
|
|
2012-05-22 20:00:56 -05:00
|
|
|
extern "C" CDECL rust_vec_box*
|
2011-10-19 23:53:02 -05:00
|
|
|
rust_list_files(rust_str *path) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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;
|
2012-05-21 20:36:52 -05:00
|
|
|
HANDLE hFind = FindFirstFile((char*)path->body.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
|
2012-05-21 20:36:52 -05:00
|
|
|
DIR *dirp = opendir((char*)path->body.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))) {
|
2012-05-21 20:36:52 -05:00
|
|
|
rust_vec_box *str = make_str(task->kernel, dp->d_name,
|
|
|
|
strlen(dp->d_name),
|
|
|
|
"list_files_str");
|
2011-09-01 17:51:27 -05:00
|
|
|
strings.push(str);
|
|
|
|
}
|
2011-07-12 17:14:57 -05:00
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-01 17:51:27 -05:00
|
|
|
|
2012-05-22 20:00:56 -05:00
|
|
|
rust_vec_box *vec = (rust_vec_box *)
|
|
|
|
task->kernel->malloc(vec_size<rust_vec_box*>(strings.size()),
|
2011-09-01 17:51:27 -05:00
|
|
|
"list_files_vec");
|
|
|
|
size_t alloc_sz = sizeof(rust_vec*) * strings.size();
|
2012-05-22 20:00:56 -05:00
|
|
|
vec->body.fill = vec->body.alloc = alloc_sz;
|
|
|
|
memcpy(&vec->body.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
|
2012-04-02 23:41:24 -05:00
|
|
|
get_time(int64_t *sec, int32_t *nsec) {
|
2011-04-29 13:54:06 -05:00
|
|
|
FILETIME fileTime;
|
2012-02-18 03:21:26 -06:00
|
|
|
GetSystemTimeAsFileTime(&fileTime);
|
|
|
|
|
|
|
|
// A FILETIME contains a 64-bit value representing the number of
|
|
|
|
// hectonanosecond (100-nanosecond) intervals since 1601-01-01T00:00:00Z.
|
|
|
|
// http://support.microsoft.com/kb/167296/en-us
|
|
|
|
ULARGE_INTEGER ul;
|
|
|
|
ul.LowPart = fileTime.dwLowDateTime;
|
|
|
|
ul.HighPart = fileTime.dwHighDateTime;
|
|
|
|
uint64_t ns_since_1601 = ul.QuadPart / 10;
|
|
|
|
|
|
|
|
const uint64_t NANOSECONDS_FROM_1601_TO_1970 = 11644473600000000u;
|
|
|
|
uint64_t ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
|
|
|
|
*sec = ns_since_1970 / 1000000;
|
2012-04-02 23:41:24 -05:00
|
|
|
*nsec = (ns_since_1970 % 1000000) * 1000;
|
2011-04-29 13:54:06 -05:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
extern "C" CDECL void
|
2012-04-02 23:41:24 -05:00
|
|
|
get_time(int64_t *sec, int32_t *nsec) {
|
|
|
|
#ifdef __APPLE__
|
2011-04-29 13:54:06 -05:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
*sec = tv.tv_sec;
|
2012-04-02 23:41:24 -05:00
|
|
|
*nsec = tv.tv_usec * 1000;
|
|
|
|
#else
|
|
|
|
timespec ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
*sec = ts.tv_sec;
|
|
|
|
*nsec = ts.tv_nsec;
|
|
|
|
#endif
|
2011-04-29 13:54:06 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-28 19:58:44 -05:00
|
|
|
extern "C" CDECL void
|
2012-02-18 03:39:42 -06:00
|
|
|
precise_time_ns(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
|
|
|
}
|
|
|
|
|
2012-04-03 12:32:26 -05:00
|
|
|
struct rust_tm {
|
|
|
|
int32_t tm_sec;
|
|
|
|
int32_t tm_min;
|
|
|
|
int32_t tm_hour;
|
|
|
|
int32_t tm_mday;
|
|
|
|
int32_t tm_mon;
|
|
|
|
int32_t tm_year;
|
|
|
|
int32_t tm_wday;
|
|
|
|
int32_t tm_yday;
|
|
|
|
int32_t tm_isdst;
|
|
|
|
int32_t tm_gmtoff;
|
|
|
|
rust_str *tm_zone;
|
|
|
|
int32_t tm_nsec;
|
|
|
|
};
|
|
|
|
|
|
|
|
void rust_tm_to_tm(rust_tm* in_tm, tm* out_tm) {
|
|
|
|
memset(out_tm, 0, sizeof(tm));
|
|
|
|
out_tm->tm_sec = in_tm->tm_sec;
|
|
|
|
out_tm->tm_min = in_tm->tm_min;
|
|
|
|
out_tm->tm_hour = in_tm->tm_hour;
|
|
|
|
out_tm->tm_mday = in_tm->tm_mday;
|
|
|
|
out_tm->tm_mon = in_tm->tm_mon;
|
|
|
|
out_tm->tm_year = in_tm->tm_year;
|
|
|
|
out_tm->tm_wday = in_tm->tm_wday;
|
|
|
|
out_tm->tm_yday = in_tm->tm_yday;
|
|
|
|
out_tm->tm_isdst = in_tm->tm_isdst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tm_to_rust_tm(tm* in_tm, rust_tm* out_tm, int32_t gmtoff,
|
|
|
|
const char *zone, int32_t nsec) {
|
|
|
|
out_tm->tm_sec = in_tm->tm_sec;
|
|
|
|
out_tm->tm_min = in_tm->tm_min;
|
|
|
|
out_tm->tm_hour = in_tm->tm_hour;
|
|
|
|
out_tm->tm_mday = in_tm->tm_mday;
|
|
|
|
out_tm->tm_mon = in_tm->tm_mon;
|
|
|
|
out_tm->tm_year = in_tm->tm_year;
|
|
|
|
out_tm->tm_wday = in_tm->tm_wday;
|
|
|
|
out_tm->tm_yday = in_tm->tm_yday;
|
|
|
|
out_tm->tm_isdst = in_tm->tm_isdst;
|
|
|
|
out_tm->tm_gmtoff = gmtoff;
|
|
|
|
out_tm->tm_nsec = nsec;
|
|
|
|
|
|
|
|
if (zone != NULL) {
|
2012-08-25 18:53:44 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-04-03 12:32:26 -05:00
|
|
|
size_t size = strlen(zone);
|
2012-08-25 18:53:44 -05:00
|
|
|
reserve_vec_exact(task, &out_tm->tm_zone, size + 1);
|
2012-05-21 20:36:52 -05:00
|
|
|
memcpy(out_tm->tm_zone->body.data, zone, size);
|
|
|
|
out_tm->tm_zone->body.fill = size + 1;
|
|
|
|
out_tm->tm_zone->body.data[size] = '\0';
|
2012-04-03 12:32:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
#define TZSET() _tzset()
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
|
|
|
#define GMTIME(clock, result) gmtime_s((result), (clock))
|
|
|
|
#define LOCALTIME(clock, result) localtime_s((result), (clock))
|
|
|
|
#define TIMEGM(result) _mkgmtime64(result)
|
|
|
|
#else
|
|
|
|
struct tm* GMTIME(const time_t *clock, tm *result) {
|
|
|
|
struct tm* t = gmtime(clock);
|
|
|
|
if (t == NULL || result == NULL) { return NULL; }
|
|
|
|
*result = *t;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
struct tm* LOCALTIME(const time_t *clock, tm *result) {
|
|
|
|
struct tm* t = localtime(clock);
|
|
|
|
if (t == NULL || result == NULL) { return NULL; }
|
|
|
|
*result = *t;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#define TIMEGM(result) mktime((result)) - _timezone
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define TZSET() tzset()
|
|
|
|
#define GMTIME(clock, result) gmtime_r((clock), (result))
|
|
|
|
#define LOCALTIME(clock, result) localtime_r((clock), (result))
|
|
|
|
#define TIMEGM(result) timegm(result)
|
|
|
|
#endif
|
|
|
|
|
2012-04-13 19:33:02 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_tzset() {
|
|
|
|
TZSET();
|
|
|
|
}
|
|
|
|
|
2012-04-03 12:32:26 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_gmtime(int64_t *sec, int32_t *nsec, rust_tm *timeptr) {
|
|
|
|
tm tm;
|
|
|
|
time_t s = *sec;
|
|
|
|
GMTIME(&s, &tm);
|
|
|
|
|
|
|
|
tm_to_rust_tm(&tm, timeptr, 0, "UTC", *nsec);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_localtime(int64_t *sec, int32_t *nsec, rust_tm *timeptr) {
|
|
|
|
tm tm;
|
|
|
|
time_t s = *sec;
|
|
|
|
LOCALTIME(&s, &tm);
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
int32_t gmtoff = -timezone;
|
|
|
|
char zone[64];
|
|
|
|
strftime(zone, sizeof(zone), "%Z", &tm);
|
|
|
|
#else
|
|
|
|
int32_t gmtoff = tm.tm_gmtoff;
|
|
|
|
const char *zone = tm.tm_zone;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tm_to_rust_tm(&tm, timeptr, gmtoff, zone, *nsec);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_timegm(rust_tm* timeptr, int64_t *out) {
|
|
|
|
tm t;
|
|
|
|
rust_tm_to_tm(timeptr, &t);
|
|
|
|
*out = TIMEGM(&t);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_mktime(rust_tm* timeptr, int64_t *out) {
|
|
|
|
tm t;
|
|
|
|
rust_tm_to_tm(timeptr, &t);
|
|
|
|
*out = mktime(&t);
|
|
|
|
}
|
|
|
|
|
2012-02-07 19:43:54 -06:00
|
|
|
extern "C" CDECL rust_sched_id
|
|
|
|
rust_get_sched_id() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-07 19:43:54 -06:00
|
|
|
return task->sched->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_sched_id
|
2012-02-07 20:34:04 -06:00
|
|
|
rust_new_sched(uintptr_t threads) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(threads > 0 && "Can't create a scheduler with no threads, silly!");
|
2012-02-07 19:43:54 -06:00
|
|
|
return task->kernel->create_scheduler(threads);
|
|
|
|
}
|
|
|
|
|
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() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-08 19:46:12 -06:00
|
|
|
return task->id;
|
2011-08-09 18:07:49 -05:00
|
|
|
}
|
|
|
|
|
2012-03-14 22:22:34 -05:00
|
|
|
static rust_task*
|
2012-02-07 19:43:54 -06:00
|
|
|
new_task_common(rust_scheduler *sched, rust_task *parent) {
|
|
|
|
return sched->create_task(parent, NULL);
|
|
|
|
}
|
|
|
|
|
2012-03-14 22:22:34 -05:00
|
|
|
extern "C" CDECL rust_task*
|
2011-10-20 05:32:43 -05:00
|
|
|
new_task() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-07 19:43:54 -06:00
|
|
|
return new_task_common(task->sched, task);
|
|
|
|
}
|
|
|
|
|
2012-03-14 22:22:34 -05:00
|
|
|
extern "C" CDECL rust_task*
|
2012-02-07 19:43:54 -06:00
|
|
|
rust_new_task_in_sched(rust_sched_id id) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-07 19:43:54 -06:00
|
|
|
rust_scheduler *sched = task->kernel->get_scheduler_by_id(id);
|
2012-06-28 18:01:55 -05:00
|
|
|
if (sched == NULL)
|
|
|
|
return NULL;
|
2012-02-07 19:43:54 -06:00
|
|
|
return new_task_common(sched, task);
|
2011-08-10 20:48:57 -05:00
|
|
|
}
|
|
|
|
|
2011-11-30 19:54:11 -06:00
|
|
|
extern "C" rust_task *
|
|
|
|
rust_get_task() {
|
2012-04-02 00:13:39 -05:00
|
|
|
return rust_get_current_task();
|
2011-11-30 19:54:11 -06:00
|
|
|
}
|
|
|
|
|
2011-09-06 16:03:20 -05:00
|
|
|
extern "C" CDECL void
|
2012-03-14 22:22:34 -05:00
|
|
|
start_task(rust_task *target, fn_env_pair *f) {
|
2012-01-04 22:11:39 -06:00
|
|
|
target->start(f->f, f->env, NULL);
|
2011-08-10 20:48:57 -05:00
|
|
|
}
|
|
|
|
|
2011-07-25 20:07:20 -05:00
|
|
|
extern "C" CDECL int
|
2011-10-20 05:32:43 -05:00
|
|
|
sched_threads() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-03 17:18:58 -06:00
|
|
|
return task->sched->number_of_threads();
|
2011-07-25 20:07:20 -05:00
|
|
|
}
|
|
|
|
|
2012-05-01 19:39:56 -05:00
|
|
|
extern "C" CDECL rust_port*
|
|
|
|
rust_port_take(rust_port_id id) {
|
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
return task->kernel->get_port_by_id(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_port_drop(rust_port *p) {
|
|
|
|
assert(p != NULL);
|
|
|
|
p->deref();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_task_id
|
|
|
|
rust_port_task(rust_port *p) {
|
|
|
|
assert(p != NULL);
|
|
|
|
return p->task->id;
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_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
|
2012-03-04 18:52:19 -06:00
|
|
|
rust_port_begin_detach(rust_port *port, uintptr_t *yield) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2011-11-16 18:44:08 -06:00
|
|
|
LOG(task, comm, "rust_port_detach(0x%" PRIxPTR ")", (uintptr_t) port);
|
2012-03-04 18:52:19 -06:00
|
|
|
port->begin_detach(yield);
|
|
|
|
}
|
|
|
|
|
2012-03-05 22:02:25 -06:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_port_end_detach(rust_port *port) {
|
|
|
|
port->end_detach();
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2011-07-29 19:53:02 -05:00
|
|
|
LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
|
2012-03-04 18:52:19 -06:00
|
|
|
delete port;
|
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
|
2012-04-06 19:03:00 -05:00
|
|
|
rust_port_id_send(rust_port_id target_port_id, void *sptr) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-04-06 19:03:00 -05:00
|
|
|
return (uintptr_t)task->kernel->send_to_port(target_port_id, sptr);
|
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.
|
2012-07-24 17:22:44 -05:00
|
|
|
extern "C" CDECL MUST_CHECK bool
|
2012-02-02 17:48:08 -06:00
|
|
|
rust_task_yield(rust_task *task, bool *killed) {
|
2012-07-24 17:22:44 -05:00
|
|
|
return task->yield();
|
2011-11-08 17:38:56 -06:00
|
|
|
}
|
|
|
|
|
2011-08-09 18:07:49 -05:00
|
|
|
extern "C" CDECL void
|
2012-03-02 02:31:14 -06:00
|
|
|
port_recv(uintptr_t *dptr, rust_port *port, uintptr_t *yield) {
|
2012-03-02 02:55:27 -06:00
|
|
|
port->receive(dptr, yield);
|
2011-08-01 16:57:17 -05:00
|
|
|
}
|
|
|
|
|
2012-02-15 00:23:16 -06:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_port_select(rust_port **dptr, rust_port **ports,
|
|
|
|
size_t n_ports, uintptr_t *yield) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-02-15 00:23:16 -06:00
|
|
|
rust_port_selector *selector = task->get_port_selector();
|
|
|
|
selector->select(task, dptr, ports, n_ports, yield);
|
|
|
|
}
|
|
|
|
|
2012-01-13 00:17:21 -06:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_set_exit_status(intptr_t code) {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-01-13 00:17:21 -06:00
|
|
|
task->kernel->set_exit_status((int)code);
|
|
|
|
}
|
|
|
|
|
2012-07-10 12:59:58 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_annihilate_box(rust_opaque_box *ptr) {
|
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
annihilate_box(task, ptr);
|
|
|
|
}
|
|
|
|
|
2012-01-13 15:19:26 -06:00
|
|
|
extern void log_console_on();
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_log_console_on() {
|
|
|
|
log_console_on();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void log_console_off(rust_env *env);
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_log_console_off() {
|
2012-04-02 00:13:39 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-01-13 15:19:26 -06:00
|
|
|
log_console_off(task->kernel->env);
|
|
|
|
}
|
|
|
|
|
2012-02-08 00:24:56 -06:00
|
|
|
extern "C" CDECL lock_and_signal *
|
|
|
|
rust_dbg_lock_create() {
|
|
|
|
return new lock_and_signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_dbg_lock_destroy(lock_and_signal *lock) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(lock);
|
2012-02-08 00:24:56 -06:00
|
|
|
delete lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_dbg_lock_lock(lock_and_signal *lock) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(lock);
|
2012-02-08 00:24:56 -06:00
|
|
|
lock->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_dbg_lock_unlock(lock_and_signal *lock) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(lock);
|
2012-02-08 00:24:56 -06:00
|
|
|
lock->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_dbg_lock_wait(lock_and_signal *lock) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(lock);
|
2012-02-08 00:24:56 -06:00
|
|
|
lock->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_dbg_lock_signal(lock_and_signal *lock) {
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(lock);
|
2012-02-08 00:24:56 -06:00
|
|
|
lock->signal();
|
|
|
|
}
|
|
|
|
|
2012-02-13 15:54:14 -06:00
|
|
|
typedef void *(*dbg_callback)(void*);
|
|
|
|
|
|
|
|
extern "C" CDECL void *
|
2012-02-13 18:06:56 -06:00
|
|
|
rust_dbg_call(dbg_callback cb, void *data) {
|
|
|
|
return cb(data);
|
2012-02-13 15:54:14 -06:00
|
|
|
}
|
|
|
|
|
2012-08-16 17:06:06 -05:00
|
|
|
extern "C" CDECL void rust_dbg_do_nothing() { }
|
|
|
|
|
2012-06-01 02:11:51 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_dbg_breakpoint() {
|
|
|
|
BREAKPOINT_AWESOME;
|
|
|
|
}
|
|
|
|
|
2012-04-03 16:03:27 -05:00
|
|
|
extern "C" CDECL rust_sched_id
|
|
|
|
rust_osmain_sched_id() {
|
2012-04-03 22:29:12 -05:00
|
|
|
rust_task *task = rust_get_current_task();
|
2012-04-03 16:03:27 -05:00
|
|
|
return task->kernel->osmain_sched_id();
|
|
|
|
}
|
|
|
|
|
2012-04-04 23:17:50 -05:00
|
|
|
extern "C" CDECL bool
|
|
|
|
rust_compare_and_swap_ptr(intptr_t *address,
|
|
|
|
intptr_t oldval, intptr_t newval) {
|
|
|
|
return sync::compare_and_swap(address, oldval, newval);
|
|
|
|
}
|
|
|
|
|
2012-05-23 16:53:44 -05:00
|
|
|
extern "C" CDECL intptr_t
|
|
|
|
rust_atomic_increment(intptr_t *address) {
|
|
|
|
return sync::increment(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL intptr_t
|
|
|
|
rust_atomic_decrement(intptr_t *address) {
|
|
|
|
return sync::decrement(address);
|
|
|
|
}
|
|
|
|
|
2012-04-06 19:03:00 -05:00
|
|
|
extern "C" CDECL void
|
|
|
|
rust_task_weaken(rust_port_id chan) {
|
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
task->kernel->weaken_task(chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_task_unweaken(rust_port_id chan) {
|
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
task->kernel->unweaken_task(chan);
|
|
|
|
}
|
|
|
|
|
2012-04-30 19:28:14 -05:00
|
|
|
extern "C" CDECL uintptr_t*
|
|
|
|
rust_global_env_chan_ptr() {
|
|
|
|
rust_task *task = rust_get_current_task();
|
|
|
|
return task->kernel->get_global_env_chan();
|
|
|
|
}
|
|
|
|
|
2012-05-15 13:34:52 -05:00
|
|
|
extern "C" void
|
2012-07-24 14:27:45 -05:00
|
|
|
rust_task_inhibit_kill(rust_task *task) {
|
2012-05-15 13:34:52 -05:00
|
|
|
task->inhibit_kill();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2012-07-24 14:27:45 -05:00
|
|
|
rust_task_allow_kill(rust_task *task) {
|
2012-05-15 13:34:52 -05:00
|
|
|
task->allow_kill();
|
|
|
|
}
|
|
|
|
|
2012-07-24 14:27:45 -05:00
|
|
|
extern "C" void
|
|
|
|
rust_task_inhibit_yield(rust_task *task) {
|
|
|
|
task->inhibit_yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
rust_task_allow_yield(rust_task *task) {
|
|
|
|
task->allow_yield();
|
|
|
|
}
|
|
|
|
|
2012-07-10 17:55:16 -05:00
|
|
|
extern "C" void
|
|
|
|
rust_task_kill_other(rust_task *task) { /* Used for linked failure */
|
|
|
|
task->kill();
|
|
|
|
}
|
|
|
|
|
2012-07-12 18:52:32 -05:00
|
|
|
extern "C" void
|
2012-07-13 18:13:11 -05:00
|
|
|
rust_task_kill_all(rust_task *task) { /* Used for linked failure */
|
2012-07-12 18:52:32 -05:00
|
|
|
task->fail_sched_loop();
|
2012-07-13 18:13:11 -05:00
|
|
|
// This must not happen twice.
|
|
|
|
static bool main_taskgroup_failed = false;
|
|
|
|
assert(!main_taskgroup_failed);
|
|
|
|
main_taskgroup_failed = true;
|
2012-07-12 18:52:32 -05:00
|
|
|
}
|
|
|
|
|
2012-07-24 16:35:08 -05:00
|
|
|
extern "C" CDECL
|
|
|
|
bool rust_task_is_unwinding(rust_task *rt) {
|
|
|
|
return rt->unwinding;
|
|
|
|
}
|
|
|
|
|
2012-08-07 13:42:30 -05:00
|
|
|
extern "C" lock_and_signal*
|
|
|
|
rust_create_little_lock() {
|
|
|
|
return new lock_and_signal();
|
2012-06-06 17:06:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2012-08-07 13:42:30 -05:00
|
|
|
rust_destroy_little_lock(lock_and_signal *lock) {
|
2012-06-06 17:06:24 -05:00
|
|
|
delete lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2012-08-07 13:42:30 -05:00
|
|
|
rust_lock_little_lock(lock_and_signal *lock) {
|
|
|
|
lock->lock();
|
2012-06-06 17:06:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2012-08-07 13:42:30 -05:00
|
|
|
rust_unlock_little_lock(lock_and_signal *lock) {
|
|
|
|
lock->unlock();
|
2012-06-06 17:06:24 -05:00
|
|
|
}
|
|
|
|
|
2012-06-27 12:07:00 -05:00
|
|
|
// set/get/atexit task_local_data can run on the rust stack for speed.
|
|
|
|
extern "C" void *
|
|
|
|
rust_get_task_local_data(rust_task *task) {
|
|
|
|
return task->task_local_data;
|
|
|
|
}
|
|
|
|
extern "C" void
|
|
|
|
rust_set_task_local_data(rust_task *task, void *data) {
|
|
|
|
task->task_local_data = data;
|
|
|
|
}
|
|
|
|
extern "C" void
|
|
|
|
rust_task_local_data_atexit(rust_task *task, void (*cleanup_fn)(void *data)) {
|
|
|
|
task->task_local_data_cleanup = cleanup_fn;
|
|
|
|
}
|
2012-06-06 17:06:24 -05:00
|
|
|
|
2012-07-02 19:42:58 -05:00
|
|
|
extern "C" void
|
|
|
|
task_clear_event_reject(rust_task *task) {
|
|
|
|
task->clear_event_reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Waits on an event, returning the pointer to the event that unblocked this
|
|
|
|
// task.
|
2012-07-24 17:22:44 -05:00
|
|
|
extern "C" MUST_CHECK bool
|
|
|
|
task_wait_event(rust_task *task, void **result) {
|
|
|
|
// Maybe (if not too slow) assert that the passed in task is the currently
|
2012-07-12 20:20:39 -05:00
|
|
|
// running task. We wouldn't want to wait some other task.
|
2012-07-02 19:42:58 -05:00
|
|
|
|
2012-07-24 17:22:44 -05:00
|
|
|
return task->wait_event(result);
|
2012-07-02 19:42:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
task_signal_event(rust_task *target, void *event) {
|
|
|
|
target->signal_event(event);
|
|
|
|
}
|
|
|
|
|
2012-08-03 20:00:23 -05:00
|
|
|
// Can safely run on the rust stack.
|
|
|
|
extern "C" void
|
|
|
|
rust_task_ref(rust_task *task) {
|
|
|
|
task->ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't run on the rust stack!
|
|
|
|
extern "C" void
|
|
|
|
rust_task_deref(rust_task *task) {
|
|
|
|
task->deref();
|
|
|
|
}
|
|
|
|
|
2012-08-21 17:32:30 -05:00
|
|
|
// Must call on rust stack.
|
|
|
|
extern "C" CDECL void
|
|
|
|
rust_call_tydesc_glue(void *root, size_t *tydesc, size_t glue_index) {
|
|
|
|
void (*glue_fn)(void *, void *, void *, void *) =
|
|
|
|
(void (*)(void *, void *, void *, void *))tydesc[glue_index];
|
|
|
|
if (glue_fn)
|
|
|
|
glue_fn(0, 0, 0, root);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
//
|