Skip to content

Commit

Permalink
builtin: implement an at_exit(cb) wrapper for C.atexit (part 1) (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Apr 12, 2024
1 parent 27cd1b1 commit a222d7b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions vlib/builtin/builtin.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a222d7b

Please sign in to comment.