2010-06-23 23:03:09 -05:00
|
|
|
#ifndef RUST_INTERNAL_H
|
|
|
|
#define RUST_INTERNAL_H
|
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
#ifndef GLOBALS_H
|
|
|
|
// these are defined in two files, and GCC complains.
|
2010-06-23 23:03:09 -05:00
|
|
|
#define __STDC_LIMIT_MACROS 1
|
|
|
|
#define __STDC_CONSTANT_MACROS 1
|
|
|
|
#define __STDC_FORMAT_MACROS 1
|
2011-05-31 19:44:54 -05:00
|
|
|
#endif
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
#define ERROR 0
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
2010-08-16 16:13:08 -05:00
|
|
|
#include <stdarg.h>
|
2011-03-11 06:30:18 -06:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2010-06-23 23:03:09 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2011-03-11 06:30:18 -06:00
|
|
|
#include <fcntl.h>
|
2011-03-24 20:20:49 -05:00
|
|
|
#include <math.h>
|
2011-03-11 06:30:18 -06:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
#include "rust.h"
|
|
|
|
#include "rand.h"
|
|
|
|
#include "uthash.h"
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
extern "C" {
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <wincrypt.h>
|
|
|
|
}
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
2011-03-11 06:30:18 -06:00
|
|
|
#include <dirent.h>
|
2010-06-23 23:03:09 -05:00
|
|
|
#else
|
|
|
|
#error "Platform not supported."
|
|
|
|
#endif
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
#include "util/array_list.h"
|
|
|
|
#include "util/indexed_list.h"
|
|
|
|
#include "util/synchronized_indexed_list.h"
|
|
|
|
#include "util/hash_map.h"
|
2010-08-09 10:06:08 -05:00
|
|
|
#include "sync/sync.h"
|
2010-08-11 23:23:34 -05:00
|
|
|
#include "sync/timer.h"
|
2010-09-08 21:13:49 -05:00
|
|
|
#include "sync/lock_and_signal.h"
|
2010-08-27 20:26:36 -05:00
|
|
|
#include "sync/lock_free_queue.h"
|
|
|
|
|
2011-05-08 23:08:43 -05:00
|
|
|
struct rust_dom;
|
|
|
|
struct rust_task;
|
2011-04-19 05:21:57 -05:00
|
|
|
class rust_log;
|
2010-08-27 20:26:36 -05:00
|
|
|
class rust_port;
|
|
|
|
class rust_chan;
|
|
|
|
struct rust_token;
|
|
|
|
class rust_kernel;
|
|
|
|
class rust_crate_cache;
|
|
|
|
|
|
|
|
struct stk_seg;
|
|
|
|
struct type_desc;
|
|
|
|
struct frame_glue_fns;
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
#ifndef __i386__
|
|
|
|
#error "Target CPU not supported."
|
|
|
|
#endif
|
|
|
|
|
2010-08-16 16:13:08 -05:00
|
|
|
#define I(dom, e) ((e) ? (void)0 : \
|
|
|
|
(dom)->srv->fatal(#e, __FILE__, __LINE__, ""))
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2010-08-16 16:13:08 -05:00
|
|
|
#define W(dom, e, s, ...) ((e) ? (void)0 : \
|
|
|
|
(dom)->srv->warning(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))
|
|
|
|
|
|
|
|
#define A(dom, e, s, ...) ((e) ? (void)0 : \
|
|
|
|
(dom)->srv->fatal(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
#define K(srv, e, s, ...) ((e) ? (void)0 : \
|
|
|
|
srv->fatal(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))
|
|
|
|
|
|
|
|
#define PTR "0x%" PRIxPTR
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// This drives our preemption scheme.
|
|
|
|
|
|
|
|
static size_t const TIME_SLICE_IN_MS = 10;
|
|
|
|
|
2010-11-09 16:15:07 -06:00
|
|
|
// Since every refcounted object is > 4 bytes, any refcount with any of the
|
|
|
|
// top two bits set is invalid. We reserve a particular bit pattern in this
|
|
|
|
// set for indicating objects that are "constant" as far as the memory model
|
|
|
|
// knows.
|
|
|
|
|
|
|
|
static intptr_t const CONST_REFCOUNT = 0x7badface;
|
|
|
|
|
2011-01-14 15:41:39 -06:00
|
|
|
// This accounts for logging buffers.
|
|
|
|
|
2011-01-14 18:01:43 -06:00
|
|
|
static size_t const BUF_BYTES = 2048;
|
2011-01-14 15:41:39 -06:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Every reference counted object should derive from this base class.
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
template <typename T> struct rc_base {
|
2010-08-11 16:05:50 -05:00
|
|
|
intptr_t ref_count;
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
void ref() {
|
2010-07-19 16:05:18 -05:00
|
|
|
++ref_count;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void deref() {
|
2010-07-19 16:05:18 -05:00
|
|
|
if (--ref_count == 0) {
|
2010-06-23 23:03:09 -05:00
|
|
|
delete (T*)this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
rc_base();
|
|
|
|
~rc_base();
|
2010-06-23 23:03:09 -05:00
|
|
|
};
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
template <typename T> struct dom_owned {
|
2010-06-23 23:03:09 -05:00
|
|
|
void operator delete(void *ptr) {
|
|
|
|
((T *)ptr)->dom->free(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
template <typename T> struct task_owned {
|
2010-09-07 20:39:07 -05:00
|
|
|
void operator delete(void *ptr) {
|
|
|
|
((T *)ptr)->task->dom->free(ptr);
|
2010-07-28 02:40:45 -05:00
|
|
|
}
|
2010-09-07 20:39:07 -05:00
|
|
|
};
|
2010-08-27 20:26:36 -05:00
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
template <typename T> struct kernel_owned {
|
2010-06-23 23:03:09 -05:00
|
|
|
void operator delete(void *ptr) {
|
2010-09-07 20:39:07 -05:00
|
|
|
((T *)ptr)->kernel->free(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> struct region_owned {
|
|
|
|
void operator delete(void *ptr) {
|
|
|
|
((T *)ptr)->region->free(ptr);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-09-10 03:21:29 -05:00
|
|
|
#include "rust_task_list.h"
|
|
|
|
|
2010-07-28 02:34:28 -05:00
|
|
|
// A cond(ition) is something we can block on. This can be a channel
|
|
|
|
// (writing), a port (reading) or a task (waiting).
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
struct rust_cond { };
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
// Helper class used regularly elsewhere.
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
template <typename T> class ptr_vec : public dom_owned<ptr_vec<T> > {
|
2010-06-23 23:03:09 -05:00
|
|
|
static const size_t INIT_SIZE = 8;
|
|
|
|
rust_dom *dom;
|
|
|
|
size_t alloc;
|
|
|
|
size_t fill;
|
|
|
|
T **data;
|
|
|
|
public:
|
|
|
|
ptr_vec(rust_dom *dom);
|
|
|
|
~ptr_vec();
|
|
|
|
|
|
|
|
size_t length() {
|
|
|
|
return fill;
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
bool is_empty() {
|
|
|
|
return fill == 0;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
T *& operator[](size_t offset);
|
|
|
|
void push(T *p);
|
|
|
|
T *pop();
|
2010-08-09 10:01:40 -05:00
|
|
|
T *peek();
|
2010-06-23 23:03:09 -05:00
|
|
|
void trim(size_t fill);
|
2010-07-19 16:05:18 -05:00
|
|
|
void swap_delete(T* p);
|
2010-06-23 23:03:09 -05:00
|
|
|
};
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
#include "memory_region.h"
|
|
|
|
#include "rust_srv.h"
|
|
|
|
#include "rust_log.h"
|
|
|
|
#include "rust_proxy.h"
|
|
|
|
#include "rust_kernel.h"
|
2010-09-07 20:39:07 -05:00
|
|
|
#include "rust_message.h"
|
2010-07-19 16:05:18 -05:00
|
|
|
#include "rust_dom.h"
|
2010-08-18 01:40:07 -05:00
|
|
|
#include "memory.h"
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
struct rust_timer {
|
2010-06-23 23:03:09 -05:00
|
|
|
// FIXME: This will probably eventually need replacement
|
|
|
|
// with something more sophisticated and integrated with
|
|
|
|
// an IO event-handling library, when we have such a thing.
|
|
|
|
// For now it's just the most basic "thread that can interrupt
|
|
|
|
// its associated domain-thread" device, so that we have
|
|
|
|
// *some* form of task-preemption.
|
2010-07-19 16:05:18 -05:00
|
|
|
rust_dom *dom;
|
2010-06-23 23:03:09 -05:00
|
|
|
uintptr_t exit_flag;
|
|
|
|
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
HANDLE thread;
|
|
|
|
#else
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_t thread;
|
|
|
|
#endif
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
rust_timer(rust_dom *dom);
|
2010-06-23 23:03:09 -05:00
|
|
|
~rust_timer();
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "rust_util.h"
|
|
|
|
|
2011-06-14 17:54:58 -05:00
|
|
|
typedef void CDECL (glue_fn)(void *, rust_task *, void *,
|
|
|
|
const type_desc **, void *);
|
|
|
|
typedef void CDECL (cmp_glue_fn)(void *, rust_task *, void *,
|
|
|
|
const type_desc **,
|
|
|
|
void *, void *, int8_t);
|
2011-06-10 16:11:22 -05:00
|
|
|
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
struct type_desc {
|
|
|
|
// First part of type_desc is known to compiler.
|
|
|
|
// first_param = &descs[1] if dynamic, null if static.
|
|
|
|
const type_desc **first_param;
|
|
|
|
size_t size;
|
|
|
|
size_t align;
|
2011-06-10 16:11:22 -05:00
|
|
|
glue_fn *take_glue;
|
|
|
|
glue_fn *drop_glue;
|
|
|
|
glue_fn *free_glue;
|
|
|
|
glue_fn *sever_glue; // For GC.
|
|
|
|
glue_fn *mark_glue; // For GC.
|
|
|
|
glue_fn *obj_drop_glue; // For custom destructors.
|
2010-07-22 19:47:32 -05:00
|
|
|
uintptr_t is_stateful;
|
2011-06-10 16:11:22 -05:00
|
|
|
cmp_glue_fn *cmp_glue;
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
// Residual fields past here are known only to runtime.
|
|
|
|
UT_hash_handle hh;
|
|
|
|
size_t n_descs;
|
|
|
|
const type_desc *descs[];
|
|
|
|
};
|
|
|
|
|
|
|
|
// An alarm can be put into a wait queue and the task will be notified
|
|
|
|
// when the wait queue is flushed.
|
|
|
|
|
|
|
|
struct
|
|
|
|
rust_alarm
|
|
|
|
{
|
|
|
|
rust_task *receiver;
|
|
|
|
size_t idx;
|
|
|
|
|
|
|
|
rust_alarm(rust_task *receiver);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef ptr_vec<rust_alarm> rust_wait_queue;
|
|
|
|
|
|
|
|
|
|
|
|
struct stk_seg {
|
|
|
|
unsigned int valgrind_id;
|
|
|
|
uintptr_t limit;
|
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct frame_glue_fns {
|
|
|
|
uintptr_t mark_glue_off;
|
|
|
|
uintptr_t drop_glue_off;
|
|
|
|
uintptr_t reloc_glue_off;
|
|
|
|
};
|
|
|
|
|
2010-06-28 20:53:16 -05:00
|
|
|
struct gc_alloc {
|
|
|
|
gc_alloc *prev;
|
|
|
|
gc_alloc *next;
|
|
|
|
uintptr_t ctrl_word;
|
|
|
|
uint8_t data[];
|
|
|
|
bool mark() {
|
|
|
|
if (ctrl_word & 1)
|
|
|
|
return false;
|
|
|
|
ctrl_word |= 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-28 18:46:13 -05:00
|
|
|
#include "circular_buffer.h"
|
2010-07-19 16:05:18 -05:00
|
|
|
#include "rust_task.h"
|
2010-06-23 23:03:09 -05:00
|
|
|
#include "rust_chan.h"
|
2010-07-28 18:46:13 -05:00
|
|
|
#include "rust_port.h"
|
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
|
|
|
|
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|
|
|
|
|
|
|
|
#endif
|