2010-06-23 21:03:09 -07:00
|
|
|
#include "rust_internal.h"
|
|
|
|
|
|
|
|
// Upcalls.
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
2011-04-19 12:21:57 +02:00
|
|
|
#define LOG_UPCALL_ENTRY(task) \
|
|
|
|
LOG(task, upcall, \
|
|
|
|
"> UPCALL %s - task: %s 0x%" PRIxPTR \
|
|
|
|
" retpc: x%" PRIxPTR \
|
|
|
|
" ref_count: %d", \
|
|
|
|
__FUNCTION__, \
|
|
|
|
(task)->name, (task), \
|
|
|
|
__builtin_return_address(0), \
|
|
|
|
(task->ref_count));
|
2010-06-23 21:03:09 -07:00
|
|
|
#else
|
2011-04-19 12:21:57 +02:00
|
|
|
#define LOG_UPCALL_ENTRY(task) \
|
|
|
|
LOG(task, upcall, "> UPCALL task: %s @x%" PRIxPTR, \
|
|
|
|
(task)->name, (task));
|
2010-06-23 21:03:09 -07:00
|
|
|
#endif
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL char const *
|
|
|
|
str_buf(rust_task *task, rust_str *s);
|
2010-06-23 21:03:09 -07:00
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" void
|
|
|
|
upcall_grow_task(rust_task *task, size_t n_frame_bytes) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
task->grow(n_frame_bytes);
|
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL
|
2011-04-19 12:21:57 +02:00
|
|
|
void upcall_log_int(rust_task *task, uint32_t level, int32_t i) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
if (task->dom->log_lvl >= level)
|
|
|
|
task->dom->log(task, level, "rust: %" PRId32 " (0x%" PRIx32 ")",
|
|
|
|
i, i);
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2011-03-22 17:25:40 -07:00
|
|
|
extern "C" CDECL
|
2011-04-19 12:21:57 +02:00
|
|
|
void upcall_log_float(rust_task *task, uint32_t level, float f) {
|
2011-03-21 17:12:05 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
if (task->dom->log_lvl >= level)
|
|
|
|
task->dom->log(task, level, "rust: %12.12f", f);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL
|
|
|
|
void upcall_log_double(rust_task *task, uint32_t level, double *f) {
|
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
if (task->dom->log_lvl >= level)
|
|
|
|
task->dom->log(task, level, "rust: %12.12f", *f);
|
2011-03-21 17:12:05 -07:00
|
|
|
}
|
|
|
|
|
2011-04-19 12:21:57 +02:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_log_str(rust_task *task, uint32_t level, rust_str *str) {
|
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
if (task->dom->log_lvl >= level) {
|
|
|
|
const char *c = str_buf(task, str);
|
|
|
|
task->dom->log(task, level, "rust: %s", c);
|
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_trace_word(rust_task *task, uintptr_t i) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
task->dom->log(task, 2, "trace: 0x%" PRIxPTR "", i, i, (char) i);
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_trace_str(rust_task *task, char const *c) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
task->dom->log(task, 2, "trace: %s", c);
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_port*
|
2010-07-19 14:05:18 -07:00
|
|
|
upcall_new_port(rust_task *task, size_t unit_sz) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
rust_dom *dom = task->dom;
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, comm, "upcall_new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
|
|
|
|
(uintptr_t) task, task->name, unit_sz);
|
2010-06-23 21:03:09 -07:00
|
|
|
return new (dom) rust_port(task, unit_sz);
|
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_del_port(rust_task *task, rust_port *port) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, comm, "upcall del_port(0x%" PRIxPTR ")", (uintptr_t) port);
|
2010-07-19 14:05:18 -07:00
|
|
|
I(task->dom, !port->ref_count);
|
2010-06-23 21:03:09 -07:00
|
|
|
delete port;
|
|
|
|
}
|
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
/**
|
2010-07-28 12:36:59 -07:00
|
|
|
* Creates a new channel pointing to a given port.
|
2010-07-19 14:05:18 -07:00
|
|
|
*/
|
2010-06-23 21:03:09 -07:00
|
|
|
extern "C" CDECL rust_chan*
|
2010-07-19 14:05:18 -07:00
|
|
|
upcall_new_chan(rust_task *task, rust_port *port) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
rust_dom *dom = task->dom;
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, comm, "upcall_new_chan("
|
|
|
|
"task=0x%" PRIxPTR " (%s), port=0x%" PRIxPTR ")",
|
|
|
|
(uintptr_t) task, task->name, port);
|
2010-06-23 21:03:09 -07:00
|
|
|
I(dom, port);
|
2010-09-07 18:39:07 -07:00
|
|
|
return new (dom) rust_chan(task, port, port->unit_sz);
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-08-09 08:15:34 -07:00
|
|
|
/**
|
|
|
|
* Called whenever this channel needs to be flushed. This can happen due to a
|
|
|
|
* flush statement, or automatically whenever a channel's ref count is
|
|
|
|
* about to drop to zero.
|
|
|
|
*/
|
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_flush_chan(rust_task *task, rust_chan *chan) {
|
|
|
|
LOG_UPCALL_ENTRY(task);
|
2010-08-17 23:21:29 -07:00
|
|
|
// Nop.
|
2010-08-09 08:15:34 -07:00
|
|
|
}
|
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
/**
|
|
|
|
* Called whenever the channel's ref count drops to zero.
|
2010-08-09 08:15:34 -07:00
|
|
|
*
|
|
|
|
* Cannot Yield: If the task were to unwind, the dropped ref would still
|
|
|
|
* appear to be live, causing modify-after-free errors.
|
2010-07-19 14:05:18 -07:00
|
|
|
*/
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL
|
|
|
|
void upcall_del_chan(rust_task *task, rust_chan *chan) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2010-08-09 08:15:34 -07:00
|
|
|
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, comm, "upcall del_chan(0x%" PRIxPTR ")", (uintptr_t) chan);
|
2010-07-28 16:46:13 -07:00
|
|
|
|
2010-08-09 08:15:34 -07:00
|
|
|
A(task->dom, chan->ref_count == 0,
|
|
|
|
"Channel's ref count should be zero.");
|
|
|
|
|
|
|
|
if (chan->is_associated()) {
|
2010-09-07 18:39:07 -07:00
|
|
|
if (chan->port->is_proxy()) {
|
|
|
|
// Here is a good place to delete the port proxy we allocated
|
|
|
|
// in upcall_clone_chan.
|
|
|
|
rust_proxy<rust_port> *proxy = chan->port->as_proxy();
|
|
|
|
chan->disassociate();
|
|
|
|
delete proxy;
|
|
|
|
} else {
|
|
|
|
// We're trying to delete a channel that another task may be
|
|
|
|
// reading from. We have two options:
|
|
|
|
//
|
|
|
|
// 1. We can flush the channel by blocking in upcall_flush_chan()
|
|
|
|
// and resuming only when the channel is flushed. The problem
|
|
|
|
// here is that we can get ourselves in a deadlock if the
|
|
|
|
// parent task tries to join us.
|
|
|
|
//
|
|
|
|
// 2. We can leave the channel in a "dormnat" state by not freeing
|
|
|
|
// it and letting the receiver task delete it for us instead.
|
|
|
|
if (chan->buffer.is_empty() == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chan->disassociate();
|
2010-08-17 23:21:29 -07:00
|
|
|
}
|
2010-08-09 08:15:34 -07:00
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
delete chan;
|
|
|
|
}
|
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
/**
|
|
|
|
* Clones a channel and stores it in the spawnee's domain. Each spawned task
|
2010-09-07 18:39:07 -07:00
|
|
|
* has its own copy of the channel.
|
2010-07-19 14:05:18 -07:00
|
|
|
*/
|
2010-06-23 21:03:09 -07:00
|
|
|
extern "C" CDECL rust_chan *
|
2010-09-07 18:39:07 -07:00
|
|
|
upcall_clone_chan(rust_task *task, maybe_proxy<rust_task> *target,
|
2010-07-19 14:05:18 -07:00
|
|
|
rust_chan *chan) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2010-09-07 18:39:07 -07:00
|
|
|
size_t unit_sz = chan->buffer.unit_sz;
|
2010-07-28 16:46:13 -07:00
|
|
|
maybe_proxy<rust_port> *port = chan->port;
|
2010-09-07 18:39:07 -07:00
|
|
|
rust_task *target_task = NULL;
|
|
|
|
if (target->is_proxy() == false) {
|
|
|
|
port = chan->port;
|
|
|
|
target_task = target->referent();
|
|
|
|
} else {
|
|
|
|
rust_handle<rust_port> *handle =
|
|
|
|
task->dom->kernel->get_port_handle(port->as_referent());
|
|
|
|
maybe_proxy<rust_port> *proxy = new rust_proxy<rust_port> (handle);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem, "new proxy: " PTR, proxy);
|
2010-09-07 18:39:07 -07:00
|
|
|
port = proxy;
|
|
|
|
target_task = target->as_proxy()->handle()->referent();
|
2010-07-28 16:46:13 -07:00
|
|
|
}
|
2010-09-07 18:39:07 -07:00
|
|
|
return new (target_task->dom) rust_chan(target_task, port, unit_sz);
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_yield(rust_task *task) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, comm, "upcall yield()");
|
2010-06-23 21:03:09 -07:00
|
|
|
task->yield(1);
|
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_sleep(rust_task *task, size_t time_in_us) {
|
2010-08-11 21:23:34 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, task, "elapsed %d",
|
2010-08-11 21:23:34 -07:00
|
|
|
task->yield_timer.get_elapsed_time());
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, task, "sleep %d us", time_in_us);
|
2010-08-11 21:23:34 -07:00
|
|
|
task->yield(2, time_in_us);
|
|
|
|
}
|
|
|
|
|
2010-07-28 15:17:30 -07:00
|
|
|
extern "C" CDECL void
|
2010-07-28 16:24:50 -07:00
|
|
|
upcall_join(rust_task *task, maybe_proxy<rust_task> *target) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2010-07-28 16:24:50 -07:00
|
|
|
|
|
|
|
if (target->is_proxy()) {
|
2010-09-07 18:39:07 -07:00
|
|
|
rust_handle<rust_task> *task_handle = target->as_proxy()->handle();
|
|
|
|
notify_message::send(notify_message::JOIN, "join",
|
|
|
|
task->get_handle(), task_handle);
|
|
|
|
task->block(task_handle, "joining remote task");
|
2010-06-23 21:03:09 -07:00
|
|
|
task->yield(2);
|
2010-07-28 16:24:50 -07:00
|
|
|
} else {
|
2010-09-07 18:39:07 -07:00
|
|
|
rust_task *target_task = target->referent();
|
2010-07-28 16:24:50 -07:00
|
|
|
// If the other task is already dying, we don't have to wait for it.
|
|
|
|
if (target_task->dead() == false) {
|
|
|
|
target_task->tasks_waiting_to_join.push(task);
|
2010-08-17 23:26:43 -07:00
|
|
|
task->block(target_task, "joining local task");
|
2010-07-28 16:24:50 -07:00
|
|
|
task->yield(2);
|
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
/**
|
2010-07-28 16:46:13 -07:00
|
|
|
* Buffers a chunk of data in the specified channel.
|
2010-07-19 14:05:18 -07:00
|
|
|
*
|
2010-07-28 16:46:13 -07:00
|
|
|
* sptr: pointer to a chunk of data to buffer
|
2010-07-19 14:05:18 -07:00
|
|
|
*/
|
2010-06-23 21:03:09 -07:00
|
|
|
extern "C" CDECL void
|
2010-07-19 14:05:18 -07:00
|
|
|
upcall_send(rust_task *task, rust_chan *chan, void *sptr) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2010-08-09 08:15:34 -07:00
|
|
|
chan->send(sptr);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, comm, "=== sent data ===>");
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL void
|
2010-07-19 14:05:18 -07:00
|
|
|
upcall_recv(rust_task *task, uintptr_t *dptr, rust_port *port) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
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());
|
2010-07-19 14:05:18 -07:00
|
|
|
|
2010-08-09 08:15:34 -07:00
|
|
|
if (port->receive(dptr)) {
|
|
|
|
return;
|
2010-07-19 14:05:18 -07:00
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
// 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.
|
2010-06-23 21:03:09 -07:00
|
|
|
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, comm, "<=== waiting for rendezvous data ===");
|
2010-07-19 14:05:18 -07:00
|
|
|
task->rendezvous_ptr = dptr;
|
2010-08-17 23:26:43 -07:00
|
|
|
task->block(port, "waiting for rendezvous data");
|
2010-07-19 14:05:18 -07:00
|
|
|
task->yield(3);
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:51 -07:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_fail(rust_task *task,
|
|
|
|
char const *expr,
|
|
|
|
char const *file,
|
|
|
|
size_t line) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG_ERR(task, upcall, "upcall fail '%s', %s:%" PRIdPTR, expr, file, line);
|
2010-06-23 21:03:09 -07:00
|
|
|
task->fail(4);
|
|
|
|
}
|
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
/**
|
|
|
|
* Called whenever a task's ref count drops to zero.
|
|
|
|
*/
|
2010-06-23 21:03:09 -07:00
|
|
|
extern "C" CDECL void
|
2010-07-28 16:24:50 -07:00
|
|
|
upcall_kill(rust_task *task, maybe_proxy<rust_task> *target) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2010-07-28 16:24:50 -07:00
|
|
|
if (target->is_proxy()) {
|
|
|
|
notify_message::
|
2010-09-07 18:39:07 -07:00
|
|
|
send(notify_message::KILL, "kill", task->get_handle(),
|
|
|
|
target->as_proxy()->handle());
|
2010-07-28 16:24:50 -07:00
|
|
|
// The proxy ref_count dropped to zero, delete it here.
|
|
|
|
delete target->as_proxy();
|
2010-07-19 14:05:18 -07:00
|
|
|
} else {
|
2010-09-07 18:39:07 -07:00
|
|
|
target->referent()->kill();
|
2010-07-19 14:05:18 -07:00
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
/**
|
|
|
|
* Called by the exit glue when the task terminates.
|
|
|
|
*/
|
2010-06-23 21:03:09 -07:00
|
|
|
extern "C" CDECL void
|
2010-07-19 14:05:18 -07:00
|
|
|
upcall_exit(rust_task *task) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, task, "task ref_count: %d", task->ref_count);
|
2010-08-09 08:15:34 -07:00
|
|
|
A(task->dom, task->ref_count >= 0,
|
|
|
|
"Task ref_count should not be negative on exit!");
|
2010-06-23 21:03:09 -07:00
|
|
|
task->die();
|
2010-07-28 16:24:50 -07:00
|
|
|
task->notify_tasks_waiting_to_join();
|
2010-06-23 21:03:09 -07:00
|
|
|
task->yield(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL uintptr_t
|
2010-08-31 14:36:51 -07:00
|
|
|
upcall_malloc(rust_task *task, size_t nbytes, type_desc *td) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem,
|
2010-07-02 08:17:09 -07:00
|
|
|
"upcall malloc(%" PRIdPTR ", 0x%" PRIxPTR ")"
|
|
|
|
" with gc-chain head = 0x%" PRIxPTR,
|
|
|
|
nbytes, td, task->gc_alloc_chain);
|
2010-06-28 18:53:16 -07:00
|
|
|
void *p = task->malloc(nbytes, td);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem,
|
2010-07-02 08:17:09 -07:00
|
|
|
"upcall malloc(%" PRIdPTR ", 0x%" PRIxPTR
|
|
|
|
") = 0x%" PRIxPTR
|
2010-06-28 18:53:16 -07:00
|
|
|
" with gc-chain head = 0x%" PRIxPTR,
|
2010-07-02 08:17:09 -07:00
|
|
|
nbytes, td, (uintptr_t)p, task->gc_alloc_chain);
|
2010-06-23 21:03:09 -07:00
|
|
|
return (uintptr_t) p;
|
|
|
|
}
|
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
/**
|
|
|
|
* Called whenever an object's ref count drops to zero.
|
|
|
|
*/
|
2010-06-23 21:03:09 -07:00
|
|
|
extern "C" CDECL void
|
2010-08-31 14:36:51 -07:00
|
|
|
upcall_free(rust_task *task, void* ptr, uintptr_t is_gc) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
rust_dom *dom = task->dom;
|
2011-04-19 12:21:57 +02:00
|
|
|
DLOG(dom, mem,
|
2010-09-29 17:22:07 -07:00
|
|
|
"upcall free(0x%" PRIxPTR ", is_gc=%" PRIdPTR ")",
|
|
|
|
(uintptr_t)ptr, is_gc);
|
2010-06-28 18:53:16 -07:00
|
|
|
task->free(ptr, (bool) is_gc);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL uintptr_t
|
2010-08-31 14:36:51 -07:00
|
|
|
upcall_mark(rust_task *task, void* ptr) {
|
2010-06-28 18:53:16 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
|
|
|
|
rust_dom *dom = task->dom;
|
|
|
|
if (ptr) {
|
|
|
|
gc_alloc *gcm = (gc_alloc*) (((char*)ptr) - sizeof(gc_alloc));
|
|
|
|
uintptr_t marked = (uintptr_t) gcm->mark();
|
2011-04-19 12:21:57 +02:00
|
|
|
DLOG(dom, gc, "upcall mark(0x%" PRIxPTR ") = %" PRIdPTR,
|
2010-06-28 18:53:16 -07:00
|
|
|
(uintptr_t)gcm, marked);
|
|
|
|
return marked;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_str *
|
2010-07-19 14:05:18 -07:00
|
|
|
upcall_new_str(rust_task *task, char const *s, size_t fill) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
rust_dom *dom = task->dom;
|
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_str) + fill);
|
2010-09-29 17:22:07 -07:00
|
|
|
void *mem = task->malloc(alloc);
|
2010-06-23 21:03:09 -07:00
|
|
|
if (!mem) {
|
|
|
|
task->fail(3);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-19 14:05:18 -07:00
|
|
|
rust_str *st = new (mem) rust_str(dom, alloc, fill, (uint8_t const *) s);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem,
|
|
|
|
"upcall new_str('%s', %" PRIdPTR ") = 0x%" PRIxPTR,
|
|
|
|
s, fill, st);
|
2010-06-23 21:03:09 -07:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_vec *
|
2010-08-31 14:36:51 -07:00
|
|
|
upcall_new_vec(rust_task *task, size_t fill, type_desc *td) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
rust_dom *dom = task->dom;
|
2011-04-19 12:21:57 +02:00
|
|
|
DLOG(dom, mem, "upcall new_vec(%" PRIdPTR ")", fill);
|
2010-06-23 21:03:09 -07:00
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_vec) + fill);
|
2010-06-28 18:53:16 -07:00
|
|
|
void *mem = task->malloc(alloc, td);
|
2010-06-23 21:03:09 -07:00
|
|
|
if (!mem) {
|
|
|
|
task->fail(3);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rust_vec *v = new (mem) rust_vec(dom, alloc, 0, NULL);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem,
|
2010-07-19 14:05:18 -07:00
|
|
|
"upcall new_vec(%" PRIdPTR ") = 0x%" PRIxPTR, fill, v);
|
2010-06-23 21:03:09 -07:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-09-03 16:18:32 -07:00
|
|
|
extern "C" CDECL rust_vec *
|
2010-08-31 14:36:51 -07:00
|
|
|
upcall_vec_grow(rust_task *task,
|
|
|
|
rust_vec *v,
|
|
|
|
size_t n_bytes,
|
2010-09-29 17:22:07 -07:00
|
|
|
uintptr_t *need_copy,
|
|
|
|
type_desc *td)
|
2010-09-03 16:18:32 -07:00
|
|
|
{
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
rust_dom *dom = task->dom;
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem,
|
2011-04-28 13:14:25 -07:00
|
|
|
"upcall vec_grow(0x%" PRIxPTR ", %" PRIdPTR
|
|
|
|
"), rc=%" PRIdPTR " alloc=%" PRIdPTR ", fill=%" PRIdPTR
|
|
|
|
", need_copy=0x%" PRIxPTR,
|
|
|
|
v, n_bytes, v->ref_count, v->alloc, v->fill, need_copy);
|
2010-09-03 16:18:32 -07:00
|
|
|
|
|
|
|
*need_copy = 0;
|
2010-06-23 21:03:09 -07:00
|
|
|
size_t alloc = next_power_of_two(sizeof(rust_vec) + v->fill + n_bytes);
|
2010-09-03 16:18:32 -07:00
|
|
|
|
2010-07-19 14:05:18 -07:00
|
|
|
if (v->ref_count == 1) {
|
2010-06-23 21:03:09 -07:00
|
|
|
|
|
|
|
// Fastest path: already large enough.
|
|
|
|
if (v->alloc >= alloc) {
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem, "no-growth path");
|
2010-06-23 21:03:09 -07:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second-fastest path: can at least realloc.
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem, "realloc path");
|
2010-09-29 17:22:07 -07:00
|
|
|
v = (rust_vec*) task->realloc(v, alloc, td->is_stateful);
|
2010-06-23 21:03:09 -07:00
|
|
|
if (!v) {
|
2010-07-15 18:59:31 -07:00
|
|
|
task->fail(4);
|
2010-06-23 21:03:09 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
v->alloc = alloc;
|
|
|
|
|
|
|
|
} else {
|
2010-09-03 16:18:32 -07:00
|
|
|
/**
|
|
|
|
* Slowest path: make a new vec.
|
|
|
|
*
|
|
|
|
* 1. Allocate a new rust_vec with desired additional space.
|
|
|
|
* 2. Down-ref the shared rust_vec, point to the new one instead.
|
|
|
|
* 3. Copy existing elements into the new rust_vec.
|
|
|
|
*
|
|
|
|
* Step 3 is a bit tricky. We don't know how to properly copy the
|
|
|
|
* elements in the runtime (all we have are bits in a buffer; no
|
|
|
|
* type infromation and no copy glue). What we do instead is set the
|
|
|
|
* need_copy outparam flag to indicate to our caller (vec-copy glue)
|
|
|
|
* that we need the copies performed for us.
|
|
|
|
*/
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem, "new vec path");
|
2010-09-29 17:22:07 -07:00
|
|
|
void *mem = task->malloc(alloc, td);
|
2010-06-23 21:03:09 -07:00
|
|
|
if (!mem) {
|
2010-07-15 18:59:31 -07:00
|
|
|
task->fail(4);
|
2010-06-23 21:03:09 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-11-09 14:15:07 -08:00
|
|
|
|
|
|
|
if (v->ref_count != CONST_REFCOUNT)
|
|
|
|
v->deref();
|
|
|
|
|
2010-09-03 16:18:32 -07:00
|
|
|
v = new (mem) rust_vec(dom, alloc, 0, NULL);
|
|
|
|
*need_copy = 1;
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
I(dom, sizeof(rust_vec) + v->fill <= v->alloc);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL type_desc *
|
2010-07-28 14:00:44 -07:00
|
|
|
upcall_get_type_desc(rust_task *task,
|
2011-05-26 18:20:48 -07:00
|
|
|
void *curr_crate, // ignored, legacy compat.
|
2010-07-28 14:00:44 -07:00
|
|
|
size_t size,
|
|
|
|
size_t align,
|
|
|
|
size_t n_descs,
|
2010-08-31 14:36:51 -07:00
|
|
|
type_desc const **descs) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, cache, "upcall get_type_desc with size=%" PRIdPTR
|
|
|
|
", align=%" PRIdPTR ", %" PRIdPTR " descs", size, align,
|
|
|
|
n_descs);
|
2011-05-26 18:20:48 -07:00
|
|
|
rust_crate_cache *cache = task->get_crate_cache();
|
2010-06-23 21:03:09 -07:00
|
|
|
type_desc *td = cache->get_type_desc(size, align, n_descs, descs);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, cache, "returning tydesc 0x%" PRIxPTR, td);
|
2010-06-23 21:03:09 -07:00
|
|
|
return td;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL rust_task *
|
2011-05-20 14:29:26 -07:00
|
|
|
upcall_new_task(rust_task *spawner, rust_vec *name) {
|
|
|
|
// name is a rust string structure.
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(spawner);
|
|
|
|
rust_dom *dom = spawner->dom;
|
2011-05-20 14:29:26 -07:00
|
|
|
rust_task *task = dom->create_task(spawner, (const char *)name->data);
|
2010-06-23 21:03:09 -07:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2011-05-31 17:44:54 -07:00
|
|
|
// TODO: This is copied from rust_task.cpp. Both copies should be moved to a
|
|
|
|
// common location.
|
|
|
|
static uintptr_t
|
|
|
|
align_down(uintptr_t sp)
|
|
|
|
{
|
|
|
|
// There is no platform we care about that needs more than a
|
|
|
|
// 16-byte alignment.
|
|
|
|
return sp & ~(16 - 1);
|
|
|
|
}
|
|
|
|
|
2010-06-23 21:03:09 -07:00
|
|
|
extern "C" CDECL rust_task *
|
2010-08-31 14:36:51 -07:00
|
|
|
upcall_start_task(rust_task *spawner,
|
|
|
|
rust_task *task,
|
|
|
|
uintptr_t spawnee_fn,
|
2011-05-31 17:44:54 -07:00
|
|
|
uintptr_t args,
|
|
|
|
size_t args_sz) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(spawner);
|
|
|
|
|
|
|
|
rust_dom *dom = spawner->dom;
|
2011-04-19 12:21:57 +02:00
|
|
|
DLOG(dom, task,
|
2011-05-26 18:00:33 -07:00
|
|
|
"upcall start_task(task %s @0x%" PRIxPTR
|
|
|
|
", spawnee 0x%" PRIxPTR ")",
|
|
|
|
task->name, task,
|
|
|
|
spawnee_fn);
|
|
|
|
|
|
|
|
// we used to be generating this tuple in rustc, but it's easier to do it
|
|
|
|
// here.
|
2011-05-31 17:44:54 -07:00
|
|
|
//
|
|
|
|
// The args tuple is stack-allocated. We need to move it over to the new
|
|
|
|
// stack.
|
|
|
|
task->rust_sp -= args_sz;
|
|
|
|
memcpy((void*)task->rust_sp, (void*)args, args_sz);
|
|
|
|
uintptr_t start_args[] = {0, 0, 0, task->rust_sp};
|
|
|
|
|
|
|
|
task->rust_sp = align_down(task->rust_sp);
|
2011-05-26 18:00:33 -07:00
|
|
|
|
2011-05-27 15:19:38 -07:00
|
|
|
task->start(spawnee_fn, (uintptr_t)start_args, sizeof(start_args));
|
2010-06-23 21:03:09 -07:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2010-09-07 18:39:07 -07:00
|
|
|
/**
|
|
|
|
* Called whenever a new domain is created.
|
|
|
|
*/
|
2010-07-28 15:17:30 -07:00
|
|
|
extern "C" CDECL maybe_proxy<rust_task> *
|
2010-08-08 19:24:35 -07:00
|
|
|
upcall_new_thread(rust_task *task, const char *name) {
|
2010-06-23 21:03:09 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
2010-09-07 18:39:07 -07:00
|
|
|
rust_dom *parent_dom = task->dom;
|
|
|
|
rust_kernel *kernel = parent_dom->kernel;
|
|
|
|
rust_handle<rust_dom> *child_dom_handle =
|
2011-05-24 19:07:30 -04:00
|
|
|
kernel->create_domain(name);
|
2010-09-07 18:39:07 -07:00
|
|
|
rust_handle<rust_task> *child_task_handle =
|
|
|
|
kernel->get_task_handle(child_dom_handle->referent()->root_task);
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, mem, "child name: %s, child_dom_handle: " PTR
|
|
|
|
", child_task_handle: " PTR,
|
|
|
|
name, child_dom_handle, child_task_handle);
|
2010-09-07 18:39:07 -07:00
|
|
|
rust_proxy<rust_task> *child_task_proxy =
|
|
|
|
new rust_proxy<rust_task> (child_task_handle);
|
|
|
|
return child_task_proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
static DWORD WINAPI rust_thread_start(void *ptr)
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
static void *rust_thread_start(void *ptr)
|
|
|
|
#else
|
|
|
|
#error "Platform not supported"
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// We were handed the domain we are supposed to run.
|
|
|
|
rust_dom *dom = (rust_dom *) ptr;
|
|
|
|
|
|
|
|
// Start a new rust main loop for this thread.
|
|
|
|
dom->start_main_loop();
|
|
|
|
|
|
|
|
// Destroy the domain.
|
|
|
|
dom->kernel->destroy_domain(dom);
|
|
|
|
|
|
|
|
return 0;
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-09-07 18:39:07 -07:00
|
|
|
/**
|
|
|
|
* Called after a new domain is created. Here we create a new thread and
|
|
|
|
* and start the domain main loop.
|
|
|
|
*/
|
2010-07-28 15:17:30 -07:00
|
|
|
extern "C" CDECL maybe_proxy<rust_task> *
|
2010-09-07 18:39:07 -07:00
|
|
|
upcall_start_thread(rust_task *task,
|
|
|
|
rust_proxy<rust_task> *child_task_proxy,
|
2010-08-31 14:36:51 -07:00
|
|
|
uintptr_t spawnee_fn,
|
|
|
|
size_t callsz) {
|
2010-09-07 18:39:07 -07:00
|
|
|
LOG_UPCALL_ENTRY(task);
|
|
|
|
rust_dom *parenet_dom = task->dom;
|
|
|
|
rust_handle<rust_task> *child_task_handle = child_task_proxy->handle();
|
2011-04-19 12:21:57 +02:00
|
|
|
LOG(task, task,
|
2011-05-24 15:51:22 -04:00
|
|
|
"spawnee_fn " PTR
|
2011-02-22 16:37:01 -08:00
|
|
|
", callsz %" PRIdPTR ")",
|
2011-05-24 15:51:22 -04:00
|
|
|
spawnee_fn, callsz);
|
2010-09-07 18:39:07 -07:00
|
|
|
rust_task *child_task = child_task_handle->referent();
|
2011-05-24 15:51:22 -04:00
|
|
|
child_task->start(spawnee_fn,
|
2011-02-22 16:37:01 -08:00
|
|
|
task->rust_sp, callsz);
|
2010-06-23 21:03:09 -07:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
HANDLE thread;
|
2010-09-07 18:39:07 -07:00
|
|
|
thread = CreateThread(NULL, 0, rust_thread_start, child_task->dom, 0,
|
|
|
|
NULL);
|
|
|
|
parenet_dom->win32_require("CreateThread", thread != NULL);
|
2010-06-23 21:03:09 -07:00
|
|
|
#else
|
|
|
|
pthread_t thread;
|
2010-09-07 18:39:07 -07:00
|
|
|
pthread_create(&thread, &parenet_dom->attr, rust_thread_start,
|
|
|
|
(void *) child_task->dom);
|
2010-06-23 21:03:09 -07:00
|
|
|
#endif
|
2010-09-07 18:39:07 -07:00
|
|
|
return child_task_proxy;
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
|
2011-06-13 18:52:04 -07:00
|
|
|
/**
|
|
|
|
* Resizes an interior vector that has been spilled to the heap.
|
|
|
|
*/
|
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_ivec_resize(rust_task *task,
|
|
|
|
rust_ivec *v,
|
|
|
|
size_t newsz) {
|
|
|
|
// TODO
|
|
|
|
task->fail(4);
|
|
|
|
}
|
|
|
|
|
2011-06-13 19:27:04 -07:00
|
|
|
/**
|
|
|
|
* Spills an interior vector to the heap.
|
|
|
|
*/
|
2011-06-13 18:52:04 -07:00
|
|
|
extern "C" CDECL void
|
|
|
|
upcall_ivec_spill(rust_task *task,
|
|
|
|
rust_ivec *v,
|
|
|
|
size_t newsz) {
|
2011-06-13 19:27:04 -07:00
|
|
|
size_t new_alloc = next_power_of_two(newsz);
|
|
|
|
|
|
|
|
rust_ivec_heap *heap_part = (rust_ivec_heap *)
|
|
|
|
task->malloc(new_alloc + sizeof(size_t));
|
|
|
|
heap_part->fill = newsz;
|
|
|
|
memcpy(&heap_part->data, v->payload.data, v->fill);
|
|
|
|
|
|
|
|
v->fill = 0;
|
|
|
|
v->alloc = new_alloc;
|
|
|
|
v->payload.ptr = heap_part;
|
2011-06-13 18:52:04 -07:00
|
|
|
}
|
|
|
|
|
2010-06-23 21:03:09 -07:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|