about summary refs log tree commit diff
path: root/src/rt/rust_kernel.cpp
blob: aaf19f240c8f650b9ae0b70de932111308eeadf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "rust_internal.h"
#include "rust_util.h"

#define KLOG_(...)                              \
    KLOG(this, kern, __VA_ARGS__)
#define KLOG_ERR_(field, ...)                   \
    KLOG_LVL(this, field, log_err, __VA_ARGS__)

rust_kernel::rust_kernel(rust_srv *srv, size_t num_threads) :
    _region(srv, true),
    _log(srv, NULL),
    srv(srv),
    max_id(0),
    rval(0),
    num_threads(num_threads),
    live_tasks(0),
    env(srv->env)
{
    isaac_init(this, &rctx);
    create_schedulers();
}

rust_scheduler *
rust_kernel::create_scheduler(int id) {
    _kernel_lock.lock();
    rust_srv *srv = this->srv->clone();
    rust_scheduler *sched =
        new (this, "rust_scheduler") rust_scheduler(this, srv, id);
    KLOG_("created scheduler: " PTR ", id: %d, index: %d",
          sched, id, sched->list_index);
    _kernel_lock.unlock();
    return sched;
}

void
rust_kernel::destroy_scheduler(rust_scheduler *sched) {
    _kernel_lock.lock();
    KLOG_("deleting scheduler: " PTR ", name: %s, index: %d",
        sched, sched->name, sched->list_index);
    rust_srv *srv = sched->srv;
    delete sched;
    delete srv;
    _kernel_lock.unlock();
}

void rust_kernel::create_schedulers() {
    KLOG_("Using %d scheduler threads.", num_threads);

    for(size_t i = 0; i < num_threads; ++i) {
        threads.push(create_scheduler(i));
    }
}

void rust_kernel::destroy_schedulers() {
    for(size_t i = 0; i < num_threads; ++i) {
        destroy_scheduler(threads[i]);
    }
}

void
rust_kernel::log_all_scheduler_state() {
    for(size_t i = 0; i < num_threads; ++i) {
        threads[i]->log_state();
    }
}

/**
 * Checks for simple deadlocks.
 */
bool
rust_kernel::is_deadlocked() {
    return false;
}

void
rust_kernel::log(uint32_t level, char const *fmt, ...) {
    char buf[BUF_BYTES];
    va_list args;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    _log.trace_ln(NULL, level, buf);
    va_end(args);
}

void
rust_kernel::fatal(char const *fmt, ...) {
    char buf[BUF_BYTES];
    va_list args;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    _log.trace_ln(NULL, (uint32_t)0, buf);
    exit(1);
    va_end(args);
}

rust_kernel::~rust_kernel() {
    destroy_schedulers();
}

void *
rust_kernel::malloc(size_t size, const char *tag) {
    return _region.malloc(size, tag);
}

void *
rust_kernel::realloc(void *mem, size_t size) {
    return _region.realloc(mem, size);
}

void rust_kernel::free(void *mem) {
    _region.free(mem);
}

void
rust_kernel::signal_kernel_lock() {
    _kernel_lock.lock();
    _kernel_lock.unlock();
}

int rust_kernel::start_task_threads()
{
    for(size_t i = 0; i < num_threads; ++i) {
        rust_scheduler *thread = threads[i];
        thread->start();
    }

    for(size_t i = 0; i < num_threads; ++i) {
        rust_scheduler *thread = threads[i];
        thread->join();
    }

    return rval;
}

void
rust_kernel::fail() {
    // FIXME: On windows we're getting "Application has requested the
    // Runtime to terminate it in an unusual way" when trying to shutdown
    // cleanly.
    set_exit_status(PROC_FAIL_CODE);
#if defined(__WIN32__)
    exit(rval);
#endif
    for(size_t i = 0; i < num_threads; ++i) {
        rust_scheduler *thread = threads[i];
        thread->kill_all_tasks();
    }
}

rust_task_id
rust_kernel::create_task(rust_task *spawner, const char *name,
                         size_t init_stack_sz) {
    scoped_lock with(_kernel_lock);
    rust_scheduler *thread = threads[isaac_rand(&rctx) % num_threads];
    rust_task *t = thread->create_task(spawner, name, init_stack_sz);
    t->user.id = max_id++;
    task_table.put(t->user.id, t);
    return t->user.id;
}

rust_task_id
rust_kernel::create_task(rust_task *spawner, const char *name) {
    return create_task(spawner, name, env->min_stack_size);
}

rust_task *
rust_kernel::get_task_by_id(rust_task_id id) {
    scoped_lock with(_kernel_lock);
    rust_task *task = NULL;
    // get leaves task unchanged if not found.
    task_table.get(id, &task);
    if(task) {
        if(task->get_ref_count() == 0) {
            // this means the destructor is running, since the destructor
            // grabs the kernel lock to unregister the task. Pretend this
            // doesn't actually exist.
            return NULL;
        }
        else {
            task->ref();
        }
    }
    return task;
}

void
rust_kernel::release_task_id(rust_task_id id) {
    scoped_lock with(_kernel_lock);
    task_table.remove(id);
}

void rust_kernel::exit_schedulers() {
    for(size_t i = 0; i < num_threads; ++i) {
        threads[i]->exit();
    }
}

#ifdef __WIN32__
void
rust_kernel::win32_require(LPCTSTR fn, BOOL ok) {
    if (!ok) {
        LPTSTR buf;
        DWORD err = GetLastError();
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                      FORMAT_MESSAGE_FROM_SYSTEM |
                      FORMAT_MESSAGE_IGNORE_INSERTS,
                      NULL, err,
                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                      (LPTSTR) &buf, 0, NULL );
        KLOG_ERR_(dom, "%s failed with error %ld: %s", fn, err, buf);
        LocalFree((HLOCAL)buf);
        I(this, ok);
    }
}
#endif

void
rust_kernel::set_exit_status(int code) {
    scoped_lock with(_kernel_lock);
    // If we've already failed then that's the code we're going to use
    if (rval != PROC_FAIL_CODE) {
        rval = code;
    }
}

//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//