3 #include "rust_internal.h"
4 #include "rust_scheduler.h"
6 #if !defined(__WIN32__)
10 extern "C" CDECL rust_str*
12 rust_task *task = rust_scheduler::get_task();
14 LOG(task, task, "last_os_error()");
16 #if defined(__WIN32__)
18 DWORD err = GetLastError();
19 DWORD res = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
20 FORMAT_MESSAGE_FROM_SYSTEM |
21 FORMAT_MESSAGE_IGNORE_INSERTS,
23 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
24 (LPTSTR) &buf, 0, NULL);
29 #elif defined(_GNU_SOURCE)
31 char *buf = strerror_r(errno, cbuf, sizeof(cbuf));
38 int err = strerror_r(errno, buf, sizeof(buf));
45 rust_str * st = make_str(task->kernel, buf, strlen(buf),
48 LocalFree((HLOCAL)buf);
53 extern "C" CDECL rust_str *
55 rust_task *task = rust_scheduler::get_task();
56 LOG(task, task, "rust_getcwd()");
60 #if defined(__WIN32__)
61 if (!_getcwd(cbuf, sizeof(cbuf))) {
63 if (!getcwd(cbuf, sizeof(cbuf))) {
69 return make_str(task->kernel, cbuf, strlen(cbuf), "rust_str(getcwd");
72 // TODO: Allow calling native functions that return double results.
74 void squareroot(double *input, double *output) {
75 *output = sqrt(*input);
80 // Do nothing. Call this with move-mode in order to say "Don't worry rust,
81 // I'll take care of this."
84 extern "C" CDECL intptr_t
85 refcount(intptr_t *v) {
86 // Passed-in value has refcount 1 too high
87 // because it was ref'ed while making the call.
98 rust_task *task = rust_scheduler::get_task();
102 extern "C" CDECL void
103 vec_reserve_shared(type_desc* ty, rust_vec** vp,
105 rust_task *task = rust_scheduler::get_task();
106 reserve_vec(task, vp, n_elts * ty->size);
110 * Copies elements in an unsafe buffer to the given interior vector. The
111 * vector must have size zero.
113 extern "C" CDECL rust_vec*
114 vec_from_buf_shared(type_desc *ty, void *ptr, size_t count) {
115 rust_task *task = rust_scheduler::get_task();
116 size_t fill = ty->size * count;
117 rust_vec* v = (rust_vec*)task->kernel->malloc(fill + sizeof(rust_vec),
119 v->fill = v->alloc = fill;
120 memmove(&v->data[0], ptr, fill);
124 extern "C" CDECL void
125 rust_str_push(rust_vec** sp, uint8_t byte) {
126 rust_task *task = rust_scheduler::get_task();
127 size_t fill = (*sp)->fill;
128 reserve_vec(task, sp, fill + 1);
129 (*sp)->data[fill-1] = byte;
130 (*sp)->data[fill] = 0;
131 (*sp)->fill = fill + 1;
134 extern "C" CDECL void *
136 rust_task *task = rust_scheduler::get_task();
137 rust_scheduler *sched = task->sched;
138 randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "randctx");
143 isaac_init(sched, rctx);
147 extern "C" CDECL size_t
148 rand_next(randctx *rctx) {
149 return isaac_rand(rctx);
152 extern "C" CDECL void
153 rand_free(randctx *rctx) {
154 rust_task *task = rust_scheduler::get_task();
158 /* Debug builtins for std::dbg. */
161 debug_tydesc_helper(type_desc *t)
163 rust_task *task = rust_scheduler::get_task();
164 LOG(task, stdlib, " size %" PRIdPTR ", align %" PRIdPTR
165 ", first_param 0x%" PRIxPTR,
166 t->size, t->align, t->first_param);
169 extern "C" CDECL void
170 debug_tydesc(type_desc *t) {
171 rust_task *task = rust_scheduler::get_task();
172 LOG(task, stdlib, "debug_tydesc");
173 debug_tydesc_helper(t);
176 extern "C" CDECL void
177 debug_opaque(type_desc *t, uint8_t *front) {
178 rust_task *task = rust_scheduler::get_task();
179 LOG(task, stdlib, "debug_opaque");
180 debug_tydesc_helper(t);
181 // FIXME may want to actually account for alignment. `front` may not
182 // indeed be the front byte of the passed-in argument.
183 for (uintptr_t i = 0; i < t->size; ++front, ++i) {
184 LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i, *front);
189 RUST_REFCOUNTED(rust_box)
191 // FIXME `data` could be aligned differently from the actual box body data
195 extern "C" CDECL void
196 debug_box(type_desc *t, rust_box *box) {
197 rust_task *task = rust_scheduler::get_task();
198 LOG(task, stdlib, "debug_box(0x%" PRIxPTR ")", box);
199 debug_tydesc_helper(t);
200 LOG(task, stdlib, " refcount %" PRIdPTR,
201 box->ref_count - 1); // -1 because we ref'ed for this call
202 for (uintptr_t i = 0; i < t->size; ++i) {
203 LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i, box->data[i]);
208 uintptr_t discriminant;
212 extern "C" CDECL void
213 debug_tag(type_desc *t, rust_tag *tag) {
214 rust_task *task = rust_scheduler::get_task();
216 LOG(task, stdlib, "debug_tag");
217 debug_tydesc_helper(t);
218 LOG(task, stdlib, " discriminant %" PRIdPTR, tag->discriminant);
220 for (uintptr_t i = 0; i < t->size - sizeof(tag->discriminant); ++i)
221 LOG(task, stdlib, " byte %" PRIdPTR ": 0x%" PRIx8, i,
230 extern "C" CDECL void
231 debug_obj(type_desc *t, rust_obj *obj, size_t nmethods, size_t nbytes) {
232 rust_task *task = rust_scheduler::get_task();
234 LOG(task, stdlib, "debug_obj with %" PRIdPTR " methods", nmethods);
235 debug_tydesc_helper(t);
236 LOG(task, stdlib, " vtbl at 0x%" PRIxPTR, obj->vtbl);
237 LOG(task, stdlib, " body at 0x%" PRIxPTR, obj->body);
239 for (uintptr_t *p = obj->vtbl; p < obj->vtbl + nmethods; ++p)
240 LOG(task, stdlib, " vtbl word: 0x%" PRIxPTR, *p);
242 for (uintptr_t i = 0; i < nbytes; ++i)
243 LOG(task, stdlib, " body byte %" PRIdPTR ": 0x%" PRIxPTR,
244 i, obj->body->data[i]);
252 extern "C" CDECL void
253 debug_fn(type_desc *t, rust_fn *fn) {
254 rust_task *task = rust_scheduler::get_task();
255 LOG(task, stdlib, "debug_fn");
256 debug_tydesc_helper(t);
257 LOG(task, stdlib, " thunk at 0x%" PRIxPTR, fn->thunk);
258 LOG(task, stdlib, " closure at 0x%" PRIxPTR, fn->closure);
260 LOG(task, stdlib, " refcount %" PRIdPTR, fn->closure->ref_count);
264 extern "C" CDECL void *
265 debug_ptrcast(type_desc *from_ty,
268 rust_task *task = rust_scheduler::get_task();
269 LOG(task, stdlib, "debug_ptrcast from");
270 debug_tydesc_helper(from_ty);
271 LOG(task, stdlib, "to");
272 debug_tydesc_helper(to_ty);
276 extern "C" CDECL void *
277 debug_get_stk_seg() {
278 rust_task *task = rust_scheduler::get_task();
282 extern "C" CDECL rust_vec*
283 rust_list_files(rust_str *path) {
284 rust_task *task = rust_scheduler::get_task();
285 array_list<rust_str*> strings;
286 #if defined(__WIN32__)
287 WIN32_FIND_DATA FindFileData;
288 HANDLE hFind = FindFirstFile((char*)path->data, &FindFileData);
289 if (hFind != INVALID_HANDLE_VALUE) {
291 rust_str *str = make_str(task->kernel, FindFileData.cFileName,
292 strlen(FindFileData.cFileName),
295 } while (FindNextFile(hFind, &FindFileData));
299 DIR *dirp = opendir((char*)path->data);
302 while ((dp = readdir(dirp))) {
303 rust_vec *str = make_str(task->kernel, dp->d_name,
312 rust_vec *vec = (rust_vec *)
313 task->kernel->malloc(vec_size<rust_vec*>(strings.size()),
315 size_t alloc_sz = sizeof(rust_vec*) * strings.size();
316 vec->fill = vec->alloc = alloc_sz;
317 memcpy(&vec->data[0], strings.data(), alloc_sz);
322 rust_path_is_dir(char *path) {
324 if (stat(path, &buf)) {
327 return S_ISDIR(buf.st_mode);
331 rust_path_exists(char *path) {
333 if (stat(path, &buf)) {
339 extern "C" CDECL FILE* rust_get_stdin() {return stdin;}
340 extern "C" CDECL FILE* rust_get_stdout() {return stdout;}
341 extern "C" CDECL FILE* rust_get_stderr() {return stderr;}
344 rust_ptr_eq(type_desc *t, rust_box *a, rust_box *b) {
348 #if defined(__WIN32__)
349 extern "C" CDECL void
350 get_time(uint32_t *sec, uint32_t *usec) {
351 rust_task *task = rust_scheduler::get_task();
352 SYSTEMTIME systemTime;
354 GetSystemTime(&systemTime);
355 if (!SystemTimeToFileTime(&systemTime, &fileTime)) {
360 // FIXME: This is probably completely wrong.
361 *sec = fileTime.dwHighDateTime;
362 *usec = fileTime.dwLowDateTime;
365 extern "C" CDECL void
366 get_time(uint32_t *sec, uint32_t *usec) {
368 gettimeofday(&tv, NULL);
374 extern "C" CDECL void
375 nano_time(uint64_t *ns) {
380 extern "C" CDECL void
382 rust_task *task = rust_scheduler::get_task();
386 extern "C" CDECL void
388 rust_task *task = rust_scheduler::get_task();
392 extern "C" CDECL rust_task_id
394 rust_task *task = rust_scheduler::get_task();
395 return task->user.id;
398 extern "C" CDECL rust_task_id
400 rust_task *task = rust_scheduler::get_task();
401 return task->kernel->create_task(task, NULL);
404 extern "C" CDECL void
405 drop_task(rust_task *target) {
411 extern "C" CDECL rust_task *
412 get_task_pointer(rust_task_id id) {
413 rust_task *task = rust_scheduler::get_task();
414 return task->kernel->get_task_by_id(id);
417 extern "C" rust_task *
419 return rust_scheduler::get_task();
427 // FIXME This is probably not needed at all anymore. Have to rearrange some
428 // argument passing to remove it.
429 void rust_spawn_wrapper(void* retptr, void* envptr,
430 void(*func)(void*, void*)) {
431 func(retptr, envptr);
434 extern "C" CDECL void
435 start_task(rust_task_id id, fn_env_pair *f) {
436 rust_task *task = rust_scheduler::get_task();
437 rust_task *target = task->kernel->get_task_by_id(id);
438 target->start((uintptr_t)rust_spawn_wrapper, f->f, f->env);
442 extern "C" CDECL void
443 migrate_alloc(void *alloc, rust_task_id tid) {
444 rust_task *task = rust_scheduler::get_task();
446 rust_task *target = task->kernel->get_task_by_id(tid);
448 const type_desc *tydesc = task->release_alloc(alloc);
449 target->claim_alloc(alloc, tydesc);
453 // We couldn't find the target. Maybe we should just free?
458 // defined in rust_task.cpp
459 extern size_t g_custom_min_stack_size;
460 extern "C" CDECL void
461 set_min_stack(uintptr_t stack_size) {
462 g_custom_min_stack_size = stack_size;
467 rust_task *task = rust_scheduler::get_task();
468 return task->kernel->num_threads;
471 extern "C" CDECL rust_port*
472 new_port(size_t unit_sz) {
473 rust_task *task = rust_scheduler::get_task();
474 LOG(task, comm, "new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
475 (uintptr_t) task, task->name, unit_sz);
476 // port starts with refcount == 1
477 return new (task->kernel, "rust_port") rust_port(task, unit_sz);
480 extern "C" CDECL void
481 rust_port_detach(rust_port *port) {
482 rust_task *task = rust_scheduler::get_task();
483 LOG(task, comm, "rust_port_detach(0x%" PRIxPTR ")", (uintptr_t) port);
485 // FIXME: Busy waiting until we're the only ref
488 scoped_lock with(port->lock);
489 done = port->ref_count == 1;
493 extern "C" CDECL void
494 del_port(rust_port *port) {
495 rust_task *task = rust_scheduler::get_task();
496 LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
497 A(task->sched, port->ref_count == 1, "Expected port ref_count == 1");
501 extern "C" CDECL size_t
502 rust_port_size(rust_port *port) {
506 extern "C" CDECL rust_port_id
507 get_port_id(rust_port *port) {
511 extern "C" CDECL uintptr_t
512 chan_id_send(type_desc *t, rust_task_id target_task_id,
513 rust_port_id target_port_id, void *sptr) {
514 // FIXME: make sure this is thread-safe
516 rust_task *task = rust_scheduler::get_task();
517 rust_task *target_task = task->kernel->get_task_by_id(target_task_id);
519 rust_port *port = target_task->get_port_by_id(target_port_id);
522 scoped_lock with(target_task->lock);
526 target_task->deref();
528 return (uintptr_t)sent;
531 // This is called by an intrinsic on the Rust stack and must run
532 // entirely in the red zone. Do not call on the C stack.
533 extern "C" CDECL void
534 rust_task_sleep(rust_task *task, size_t time_in_us, bool *killed) {
535 task->yield(time_in_us, killed);
538 extern "C" CDECL void
539 port_recv(uintptr_t *dptr, rust_port *port,
540 uintptr_t *yield, uintptr_t *killed) {
543 rust_task *task = rust_scheduler::get_task();
545 scoped_lock with(port->lock);
547 LOG(task, comm, "port: 0x%" PRIxPTR ", dptr: 0x%" PRIxPTR
548 ", size: 0x%" PRIxPTR,
549 (uintptr_t) port, (uintptr_t) dptr, port->unit_sz);
551 if (port->receive(dptr)) {
555 // If this task has been killed then we're not going to bother
556 // blocking, we have to unwind.
562 // No data was buffered on any incoming channel, so block this task on
563 // the port. Remember the rendezvous location so that any sender task
564 // can write to it before waking up this task.
566 LOG(task, comm, "<=== waiting for rendezvous data ===");
567 task->rendezvous_ptr = dptr;
568 task->block(port, "waiting for rendezvous data");
578 // indent-tabs-mode: nil
580 // buffer-file-coding-system: utf-8-unix
581 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";