2 Copyright (C) 2001-2009, Parrot Foundation.
7 src/exit.c - Exit Handling
11 Parrot's version of C<exit()>, C<on_exit()>, and friends.
13 C<Parrot_on_exit()> allows you register exit handlers which will be
14 called by C<Parrot_exit()> when the interpreter exits.
25 #include "parrot/parrot.h"
27 /* HEADERIZER HFILE: include/parrot/exit.h */
31 =item C<void Parrot_on_exit(PARROT_INTERP, exit_handler_f function, void *arg)>
33 Register the specified function to be called on exit.
41 Parrot_on_exit(PARROT_INTERP
, NOTNULL(exit_handler_f function
), ARGIN_NULLOK(void *arg
))
43 ASSERT_ARGS(Parrot_on_exit
)
44 /* RT#46403 we might want locking around the list access. I'm sure this
45 * will be the least of the threading issues. */
47 handler_node_t
* const new_node
= mem_allocate_typed(handler_node_t
);
49 new_node
->function
= function
;
51 new_node
->next
= interp
->exit_handler_list
;
52 interp
->exit_handler_list
= new_node
;
57 =item C<void Parrot_exit(PARROT_INTERP, int status)>
59 Exit, calling any registered exit handlers.
66 PARROT_DOES_NOT_RETURN
68 Parrot_exit(PARROT_INTERP
, int status
)
70 ASSERT_ARGS(Parrot_exit
)
71 /* call all the exit handlers */
72 /* we are well "below" the runloop now, where lo_var_ptr
73 * is set usually - exit handlers may run some resource-hungry
74 * stuff like printing profile stats - a GC run would kill
75 * resources - RT#46405 reset stacktop or better disable GC
78 * we don't allow new exit_handlers being installed inside exit handlers
80 * and: interp->exit_handler_list is gone, after the last exit handler
81 * (Parrot_really_destroy) has run
83 handler_node_t
*node
= interp
->exit_handler_list
;
85 Parrot_block_GC_mark(interp
);
86 Parrot_block_GC_sweep(interp
);
89 handler_node_t
* const next
= node
->next
;
91 (node
->function
)(interp
, status
, node
->arg
);
105 F<include/parrot/exit.h> and F<t/src/exit.t>.
109 Initial version by Josh Wilmes.
118 * c-file-style: "parrot"
120 * vim: expandtab shiftwidth=4: