about summary refs log tree commit diff
path: root/src/rt/rust_kernel.cpp
blob: e74819ab31814cd93a0aae45cc2da3c88f0cdaf5 (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
235
236
237
238
239
240
241
242
243
244
#include "rust_internal.h"
#include "rust_util.h"
#include "rust_scheduler.h"

#include <vector>

#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) :
    _region(srv, true),
    _log(srv, NULL),
    srv(srv),
    max_task_id(0),
    max_port_id(0),
    rval(0),
    max_sched_id(0),
    env(srv->env)
{
}

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);
}

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);
}

rust_sched_id
rust_kernel::create_scheduler(size_t num_threads) {
    rust_sched_id id;
    rust_scheduler *sched;
    {
        scoped_lock with(sched_lock);
        id = max_sched_id++;
        K(srv, id != INTPTR_MAX, "Hit the maximum scheduler id");
        sched = new (this, "rust_scheduler")
            rust_scheduler(this, srv, num_threads, id);
        bool is_new = sched_table
            .insert(std::pair<rust_sched_id, rust_scheduler*>(id, sched)).second;
        A(this, is_new, "Reusing a sched id?");
    }
    sched->start_task_threads();
    return id;
}

rust_scheduler *
rust_kernel::get_scheduler_by_id(rust_sched_id id) {
    scoped_lock with(sched_lock);
    sched_map::iterator iter = sched_table.find(id);
    if (iter != sched_table.end()) {
        return iter->second;
    } else {
        return NULL;
    }
}

void
rust_kernel::release_scheduler_id(rust_sched_id id) {
    scoped_lock with(sched_lock);
    // This list will most likely only ever have a single element in it, but
    // it's an actual list because we could potentially get here multiple
    // times before the main thread ever calls wait_for_schedulers()
    join_list.push_back(id);
    sched_lock.signal();
}

/*
Called on the main thread to wait for the kernel to exit. This function is
also used to join on every terminating scheduler thread, so that we can be
sure they have completely exited before the process exits.  If we don't join
them then we can see valgrind errors due to un-freed pthread memory.
 */
int
rust_kernel::wait_for_schedulers()
{
    scoped_lock with(sched_lock);
    while (!sched_table.empty()) {
        while (!join_list.empty()) {
            rust_sched_id id = join_list.back();
            join_list.pop_back();
            sched_map::iterator iter = sched_table.find(id);
            I(this, iter != sched_table.end());
            rust_scheduler *sched = iter->second;
            sched_table.erase(iter);
            sched->join_task_threads();
            delete sched;
        }
        if (!sched_table.empty()) {
            sched_lock.wait();
        }
    }
    return rval;
}

// FIXME: Fix all these FIXMEs
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
    // Copy the list of schedulers so that we don't hold the lock while
    // running kill_all_tasks.
    // FIXME: There's a lot that happens under kill_all_tasks, and I don't
    // know that holding sched_lock here is ok, but we need to hold the
    // sched lock to prevent the scheduler from being destroyed while
    // we are using it. Probably we need to make rust_scheduler atomicly
    // reference counted.
    std::vector<rust_scheduler*> scheds;
    {
        scoped_lock with(sched_lock);
        for (sched_map::iterator iter = sched_table.begin();
             iter != sched_table.end(); iter++) {
            scheds.push_back(iter->second);
        }
    }

    // FIXME: This is not a foolproof way to kill all tasks while ensuring
    // that no new tasks or schedulers are created in the meantime that
    // keep the scheduler alive.
    for (std::vector<rust_scheduler*>::iterator iter = scheds.begin();
         iter != scheds.end(); iter++) {
        (*iter)->kill_all_tasks();
    }
}

rust_task_id
rust_kernel::generate_task_id() {
    rust_task_id id = sync::increment(max_task_id);
    K(srv, id != INTPTR_MAX, "Hit the maximum task id");
    return id;
}

rust_port_id
rust_kernel::register_port(rust_port *port) {
    uintptr_t new_live_ports;
    rust_port_id new_port_id;
    {
        scoped_lock with(port_lock);
        new_port_id = max_port_id++;
        port_table.put(new_port_id, port);
        new_live_ports = port_table.count();
    }
    K(srv, new_port_id != INTPTR_MAX, "Hit the maximum port id");
    KLOG_("Registered port %" PRIdPTR, new_port_id);
    KLOG_("Total outstanding ports: %d", new_live_ports);
    return new_port_id;
}

void
rust_kernel::release_port_id(rust_port_id id) {
    KLOG_("Releasing port %" PRIdPTR, id);
    uintptr_t new_live_ports;
    {
        scoped_lock with(port_lock);
        port_table.remove(id);
        new_live_ports = port_table.count();
    }
    KLOG_("Total outstanding ports: %d", new_live_ports);
}

rust_port *
rust_kernel::get_port_by_id(rust_port_id id) {
    scoped_lock with(port_lock);
    rust_port *port = NULL;
    // get leaves port unchanged if not found.
    port_table.get(id, &port);
    if(port) {
        port->ref();
    }
    return port;
}

#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(rval_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:
//