This PR implements zero cost `BaseIO` by erasing the `IO.RealWorld` parameter from argument lists and structures. This is a **major breaking change for FFI**. Concretely: - `BaseIO` is defined in terms of `ST IO.RealWorld` - `EIO` (and thus `IO`) is defined in terms of `EST IO.RealWorld` - The opaque `Void` type is introduced and the trivial structure optimization updated to account for it. Furthermore, arguments of type `Void s` are removed from the argument lists of the C functions. - `ST` is redefined as `Void s -> ST.Out s a` where `ST.Out` is a pair of `Void s` and `a` This together has the following major effects on our generated code: - Functions that return `BaseIO`/`ST`/`EIO`/`IO`/`EST` now do not take the dummy world parameter anymore. To account for this FFI code needs to delete the dummy world parameter from the argument lists. - Functions that return `BaseIO`/`ST` now return their wrapped value directly. In particular `BaseIO UInt32` now returns a `uint32_t` instead of a `lean_object*`. To account for this FFI code might have to change the return type and does not need to call `lean_io_result_mk_ok` anymore but can instead just `return` values right away (same with extracting values from `BaseIO` computations. - Functions that return `EIO`/`IO`/`EST` now only return the equivalent of an `Except` node which reduces the allocation size. The `lean_io_result_mk_ok`/`lean_io_result_mk_error` functions were updated to account for this already so no change is required. Besides improving performance by dropping allocation (sizes) we can now also do fun new things such as: ```lean @[extern "malloc"] opaque malloc (size : USize) : BaseIO USize ```
95 lines
2.6 KiB
C++
95 lines
2.6 KiB
C++
/*
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Author: Leonardo de Moura
|
|
*/
|
|
#include <limits>
|
|
#include "runtime/thread.h"
|
|
#include "runtime/interrupt.h"
|
|
#include "runtime/exception.h"
|
|
#include "runtime/memory.h"
|
|
#include "lean/lean.h"
|
|
#include "util/io.h"
|
|
|
|
namespace lean {
|
|
LEAN_THREAD_VALUE(size_t, g_max_heartbeat, 0);
|
|
LEAN_THREAD_VALUE(size_t, g_heartbeat, 0);
|
|
|
|
extern "C" LEAN_EXPORT obj_res lean_internal_get_default_max_heartbeat() {
|
|
#ifdef LEAN_DEFAULT_MAX_HEARTBEAT
|
|
return lean_box(LEAN_DEFAULT_MAX_HEARTBEAT);
|
|
#else
|
|
return lean_box(0);
|
|
#endif
|
|
}
|
|
|
|
void inc_heartbeat() { g_heartbeat++; }
|
|
|
|
void reset_heartbeat() { g_heartbeat = 0; }
|
|
|
|
void set_max_heartbeat(size_t max) { g_max_heartbeat = max; }
|
|
|
|
extern "C" LEAN_EXPORT obj_res lean_internal_set_max_heartbeat(usize max) {
|
|
set_max_heartbeat(max);
|
|
return lean_box(0);
|
|
}
|
|
|
|
size_t get_max_heartbeat() { return g_max_heartbeat; }
|
|
|
|
void set_max_heartbeat_thousands(unsigned max) { g_max_heartbeat = static_cast<size_t>(max) * 1000; }
|
|
|
|
scope_heartbeat::scope_heartbeat(size_t max):flet<size_t>(g_heartbeat, max) {}
|
|
LEAN_EXPORT scope_max_heartbeat::scope_max_heartbeat(size_t max):flet<size_t>(g_max_heartbeat, max) {}
|
|
|
|
// separate definition to allow breakpoint in debugger
|
|
void throw_heartbeat_exception() {
|
|
throw heartbeat_exception();
|
|
}
|
|
|
|
void check_heartbeat() {
|
|
inc_heartbeat();
|
|
if (g_max_heartbeat > 0 && g_heartbeat > g_max_heartbeat)
|
|
throw_heartbeat_exception();
|
|
}
|
|
|
|
LEAN_THREAD_VALUE(lean_object *, g_cancel_tk, nullptr);
|
|
|
|
LEAN_EXPORT scope_cancel_tk::scope_cancel_tk(lean_object * o):flet<lean_object *>(g_cancel_tk, o) {}
|
|
|
|
/* CancelToken.isSet : @& IO.CancelToken → BaseIO Bool */
|
|
extern "C" lean_obj_res lean_io_cancel_token_is_set(b_lean_obj_arg cancel_tk);
|
|
|
|
void check_interrupted() {
|
|
if (g_cancel_tk) {
|
|
inc_ref(g_cancel_tk);
|
|
if (lean_io_cancel_token_is_set(g_cancel_tk) &&
|
|
!std::uncaught_exception()) {
|
|
throw interrupted();
|
|
}
|
|
}
|
|
}
|
|
|
|
void check_system(char const * component_name, bool do_check_interrupted) {
|
|
check_stack(component_name);
|
|
check_memory(component_name);
|
|
if (do_check_interrupted) {
|
|
check_interrupted();
|
|
check_heartbeat();
|
|
}
|
|
}
|
|
|
|
void sleep_for(unsigned ms, unsigned step_ms) {
|
|
if (step_ms == 0)
|
|
step_ms = 1;
|
|
unsigned rounds = ms / step_ms;
|
|
chrono::milliseconds c(step_ms);
|
|
chrono::milliseconds r(ms % step_ms);
|
|
for (unsigned i = 0; i < rounds; i++) {
|
|
this_thread::sleep_for(c);
|
|
check_interrupted();
|
|
}
|
|
this_thread::sleep_for(r);
|
|
check_interrupted();
|
|
}
|
|
}
|