Skip to content

Commit f8ff013

Browse files
committed
Added a few utility classes, cleaned up the include order of .h files, and started to make the Rust kernel own domain message queues rather than the Rust domains themselves.
1 parent ffdb5fc commit f8ff013

16 files changed

+250
-111
lines changed

src/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ RUNTIME_CS := rt/sync/timer.cpp \
267267
rt/circular_buffer.cpp \
268268
rt/isaac/randport.cpp \
269269
rt/rust_srv.cpp \
270+
rt/rust_kernel.cpp \
270271
rt/memory_region.cpp
271272

272273
RUNTIME_HDR := rt/globals.h \
@@ -283,11 +284,14 @@ RUNTIME_HDR := rt/globals.h \
283284
rt/rust_message.h \
284285
rt/circular_buffer.h \
285286
rt/util/array_list.h \
287+
rt/util/indexed_list.h \
288+
rt/util/synchronized_indexed_list.h \
286289
rt/util/hash_map.h \
287290
rt/sync/sync.h \
288291
rt/sync/timer.h \
289292
rt/sync/lock_free_queue.h \
290293
rt/rust_srv.h \
294+
rt/rust_kernel.h \
291295
rt/memory_region.h \
292296
rt/memory.h
293297

src/rt/rust.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "rust_internal.h"
22

3-
43
struct
54
command_line_args
65
{
@@ -80,6 +79,7 @@ rust_start(uintptr_t main_fn, rust_crate const *crate, int argc, char **argv)
8079
{
8180
rust_srv srv;
8281
rust_dom dom(&srv, crate, "main");
82+
srv.kernel->register_domain(&dom);
8383
command_line_args args(dom, argc, argv);
8484

8585
dom.log(rust_log::DOM, "startup: %d args", args.argc);
@@ -99,6 +99,7 @@ rust_start(uintptr_t main_fn, rust_crate const *crate, int argc, char **argv)
9999
sizeof(main_args));
100100

101101
ret = dom.start_main_loop();
102+
srv.kernel->deregister_domain(&dom);
102103
}
103104

104105
#if !defined(__WIN32__)

src/rt/rust.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#define CDECL
1818
#endif
1919

20-
#include "util/array_list.h"
21-
22-
#include "rust_srv.h"
23-
2420
/*
2521
* Local Variables:
2622
* fill-column: 78;

src/rt/rust_dom.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
template class ptr_vec<rust_task>;
66

7-
// Keeps track of all live domains, for debugging purposes.
8-
array_list<rust_dom*> _live_domains;
9-
107
rust_dom::rust_dom(rust_srv *srv, rust_crate const *root_crate,
118
const char *name) :
129
interrupt_flag(0),
@@ -22,7 +19,8 @@ rust_dom::rust_dom(rust_srv *srv, rust_crate const *root_crate,
2219
caches(this),
2320
root_task(NULL),
2421
curr_task(NULL),
25-
rval(0)
22+
rval(0),
23+
_kernel(srv->kernel)
2624
{
2725
logptr("new dom", (uintptr_t)this);
2826
isaac_init(this, &rctx);
@@ -32,10 +30,6 @@ rust_dom::rust_dom(rust_srv *srv, rust_crate const *root_crate,
3230
pthread_attr_setdetachstate(&attr, true);
3331
#endif
3432
root_task = new (this) rust_task(this, NULL, name);
35-
36-
if (_live_domains.replace(NULL, this) == false) {
37-
_live_domains.append(this);
38-
}
3933
}
4034

4135
static void
@@ -86,8 +80,6 @@ rust_dom::~rust_dom() {
8680
#endif
8781
while (caches.length())
8882
delete caches.pop();
89-
90-
_live_domains.replace(this, NULL);
9183
}
9284

9385
void
@@ -375,7 +367,7 @@ rust_dom::schedule_task() {
375367
*/
376368
bool
377369
rust_dom::is_deadlocked() {
378-
if (_live_domains.size() != 1) {
370+
if (_kernel->domains.length() != 1) {
379371
// We cannot tell if we are deadlocked if other domains exists.
380372
return false;
381373
}
@@ -388,20 +380,13 @@ rust_dom::is_deadlocked() {
388380
if (_incoming_message_queue.is_empty() && blocked_tasks.length() > 0) {
389381
// We have no messages to process, no running tasks to schedule
390382
// and some blocked tasks therefore we are likely in a deadlock.
391-
log_state();
383+
_kernel->log_all_domain_state();
392384
return true;
393385
}
394386

395387
return false;
396388
}
397389

398-
void
399-
rust_dom::log_all_state() {
400-
for (uint32_t i = 0; i < _live_domains.size(); i++) {
401-
_live_domains[i]->log_state();
402-
}
403-
}
404-
405390
void
406391
rust_dom::log_state() {
407392
if (!running_tasks.is_empty()) {

src/rt/rust_dom.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
/*
2-
* rust_dom.h
3-
*/
4-
51
#ifndef RUST_DOM_H
62
#define RUST_DOM_H
73

8-
#include "sync/lock_free_queue.h"
9-
#include "util/hash_map.h"
10-
11-
#include "rust_proxy.h"
12-
#include "rust_message.h"
13-
144
struct rust_dom
155
{
166
// Fields known to the compiler:
@@ -37,6 +27,9 @@ struct rust_dom
3727
rust_task *curr_task;
3828
int rval;
3929

30+
rust_kernel *_kernel;
31+
int32_t list_index;
32+
4033
hash_map<rust_task *, rust_proxy<rust_task> *> _task_proxies;
4134
hash_map<rust_port *, rust_proxy<rust_port> *> _port_proxies;
4235

src/rt/rust_internal.h

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414

1515
#include <stdio.h>
1616
#include <string.h>
17-
1817
#include "rust.h"
19-
2018
#include "rand.h"
21-
#include "rust_log.h"
2219
#include "uthash.h"
2320

2421
#if defined(__WIN32__)
@@ -39,9 +36,28 @@ extern "C" {
3936
#error "Platform not supported."
4037
#endif
4138

39+
#include "util/array_list.h"
40+
#include "util/indexed_list.h"
41+
#include "util/synchronized_indexed_list.h"
42+
#include "util/hash_map.h"
4243
#include "sync/sync.h"
4344
#include "sync/timer.h"
4445
#include "sync/condition_variable.h"
46+
#include "sync/lock_free_queue.h"
47+
48+
class rust_dom;
49+
class rust_log;
50+
class rust_task;
51+
class rust_port;
52+
class rust_chan;
53+
struct rust_token;
54+
class rust_kernel;
55+
class rust_crate;
56+
class rust_crate_cache;
57+
58+
struct stk_seg;
59+
struct type_desc;
60+
struct frame_glue_fns;
4561

4662
#ifndef __i386__
4763
#error "Target CPU not supported."
@@ -56,29 +72,13 @@ extern "C" {
5672
#define A(dom, e, s, ...) ((e) ? (void)0 : \
5773
(dom)->srv->fatal(#e, __FILE__, __LINE__, s, ## __VA_ARGS__))
5874

59-
struct rust_task;
60-
struct rust_port;
61-
class rust_chan;
62-
struct rust_token;
63-
struct rust_dom;
64-
class rust_crate;
65-
class rust_crate_cache;
66-
// class lockfree_queue;
67-
68-
struct stk_seg;
69-
struct type_desc;
70-
struct frame_glue_fns;
71-
7275
// This drives our preemption scheme.
7376

7477
static size_t const TIME_SLICE_IN_MS = 10;
7578

7679
// Every reference counted object should derive from this base class.
7780

78-
template <typename T>
79-
struct
80-
rc_base
81-
{
81+
template <typename T> struct rc_base {
8282
intptr_t ref_count;
8383

8484
void ref() {
@@ -91,29 +91,25 @@ rc_base
9191
}
9292
}
9393

94-
rc_base();
95-
~rc_base();
94+
rc_base();
95+
~rc_base();
9696
};
9797

98-
template <typename T>
99-
struct
100-
dom_owned
101-
{
98+
template <typename T> struct dom_owned {
10299
rust_dom *get_dom() const {
103100
return ((T*)this)->dom;
104101
}
102+
105103
void operator delete(void *ptr) {
106104
((T *)ptr)->dom->free(ptr);
107105
}
108106
};
109107

110-
template <typename T>
111-
struct
112-
task_owned
113-
{
108+
template <typename T> struct task_owned {
114109
rust_dom *get_dom() const {
115110
return ((T *)this)->task->dom;
116111
}
112+
117113
void operator delete(void *ptr) {
118114
((T *)ptr)->task->dom->free(ptr);
119115
}
@@ -122,24 +118,16 @@ task_owned
122118
// A cond(ition) is something we can block on. This can be a channel
123119
// (writing), a port (reading) or a task (waiting).
124120

125-
struct
126-
rust_cond
127-
{
128-
};
121+
struct rust_cond { };
129122

130123
// Helper class used regularly elsewhere.
131124

132-
template <typename T>
133-
class
134-
ptr_vec : public dom_owned<ptr_vec<T> >
135-
{
125+
template <typename T> class ptr_vec : public dom_owned<ptr_vec<T> > {
136126
static const size_t INIT_SIZE = 8;
137-
138127
rust_dom *dom;
139128
size_t alloc;
140129
size_t fill;
141130
T **data;
142-
143131
public:
144132
ptr_vec(rust_dom *dom);
145133
~ptr_vec();
@@ -160,24 +148,16 @@ ptr_vec : public dom_owned<ptr_vec<T> >
160148
void swap_delete(T* p);
161149
};
162150

151+
#include "memory_region.h"
152+
#include "rust_srv.h"
153+
#include "rust_log.h"
154+
#include "rust_proxy.h"
155+
#include "rust_message.h"
156+
#include "rust_kernel.h"
163157
#include "rust_dom.h"
164-
165-
template <typename T> inline T
166-
check_null(rust_dom *dom, T value, char const *expr,
167-
char const *file, size_t line) {
168-
if (value == NULL) {
169-
dom->srv->fatal(expr, file, line, "is null");
170-
}
171-
return value;
172-
}
173-
174-
#define CHECK_NULL(dom, e) (check_null(dom, e, #e, __FILE__, __LINE__))
175-
176158
#include "memory.h"
177159

178-
struct
179-
rust_timer
180-
{
160+
struct rust_timer {
181161
// FIXME: This will probably eventually need replacement
182162
// with something more sophisticated and integrated with
183163
// an IO event-handling library, when we have such a thing.
@@ -568,7 +548,6 @@ struct gc_alloc {
568548
};
569549

570550
#include "circular_buffer.h"
571-
#include "rust_proxy.h"
572551
#include "rust_task.h"
573552
#include "rust_chan.h"
574553
#include "rust_port.h"

src/rt/rust_kernel.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "rust_internal.h"
2+
3+
rust_kernel::rust_kernel(rust_srv *srv) :
4+
_region(srv->local_region),
5+
_log(srv, NULL),
6+
domains(srv->local_region),
7+
message_queues(srv->local_region) {
8+
// Nop.
9+
}
10+
11+
rust_kernel::~rust_kernel() {
12+
// Nop.
13+
}
14+
15+
void
16+
rust_kernel::register_domain(rust_dom *dom) {
17+
domains.append(dom);
18+
}
19+
20+
void
21+
rust_kernel::deregister_domain(rust_dom *dom) {
22+
domains.remove(dom);
23+
}
24+
25+
void
26+
rust_kernel::log_all_domain_state() {
27+
log(rust_log::KERN, "log_all_domain_state: %d domains", domains.length());
28+
for (uint32_t i = 0; i < domains.length(); i++) {
29+
domains[i]->log_state();
30+
}
31+
}
32+
33+
void
34+
rust_kernel::log(uint32_t type_bits, char const *fmt, ...) {
35+
char buf[256];
36+
if (_log.is_tracing(type_bits)) {
37+
va_list args;
38+
va_start(args, fmt);
39+
vsnprintf(buf, sizeof(buf), fmt, args);
40+
_log.trace_ln(NULL, type_bits, buf);
41+
va_end(args);
42+
}
43+
}

src/rt/rust_kernel.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef RUST_KERNEL_H
2+
#define RUST_KERNEL_H
3+
4+
/**
5+
* A global object shared by all domains.
6+
*/
7+
class rust_kernel {
8+
memory_region &_region;
9+
rust_log _log;
10+
public:
11+
synchronized_indexed_list<rust_dom> domains;
12+
synchronized_indexed_list<lock_free_queue<rust_message*> > message_queues;
13+
rust_kernel(rust_srv *srv);
14+
void register_domain(rust_dom *dom);
15+
void deregister_domain(rust_dom *dom);
16+
void log_all_domain_state();
17+
void log(uint32_t type_bits, char const *fmt, ...);
18+
virtual ~rust_kernel();
19+
};
20+
21+
#endif /* RUST_KERNEL_H */

0 commit comments

Comments
 (0)