builtin: implement an at_exit(cb) wrapper for C.atexit (part 1) (#21254)

This commit is contained in:
Delyan Angelov 2024-04-12 09:15:13 +03:00 committed by GitHub
parent 27cd1b106e
commit a222d7beb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,6 +32,23 @@ pub fn exit(code int) {
C.exit(code)
}
// at_exit registers a fn callback, that will be called at normal process termination.
// It returns an error, if the registration was not successful.
// The registered callback functions, will be called either via exit/1,
// or via return from the main program, in the reverse order of their registration.
// The same fn may be registered multiple times.
// Each callback fn will called once for each registration.
pub fn at_exit(cb FnExitCb) ! {
$if freestanding {
return error('at_exit not implemented with -freestanding')
} $else {
res := C.atexit(cb)
if res != 0 {
return error_with_code('at_exit failed', res)
}
}
}
// panic_debug private function that V uses for panics, -cg/-g is passed
// recent versions of tcc print nicer backtraces automatically
// Note: the duplication here is because tcc_backtrace should be called directly