* src/pmc/multisub.pmc:
[parrot.git] / src / exit.c
blob463af04bc50f2d790c38d7e5b3c23cb8a1f72fd2
1 /*
2 Copyright (C) 2001-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/exit.c - Exit Handling
9 =head1 DESCRIPTION
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.
16 =head2 Functions
20 #include <stdlib.h>
21 #include "parrot/parrot.h"
23 /* HEADERIZER HFILE: include/parrot/exit.h */
27 FUNCDOC: Parrot_on_exit
29 Register the specified function to be called on exit.
33 PARROT_API
34 void
35 Parrot_on_exit(PARROT_INTERP, NOTNULL(exit_handler_f function), NULLOK(void *arg))
37 /* XXX we might want locking around the list access. I'm sure this
38 * will be the least of the threading issues. */
40 handler_node_t* const new_node = mem_allocate_typed(handler_node_t);
42 new_node->function = function;
43 new_node->arg = arg;
44 new_node->next = interp->exit_handler_list;
45 interp->exit_handler_list = new_node;
50 FUNCDOC: Parrot_exit
52 Exit, calling any registered exit handlers.
56 PARROT_API
57 PARROT_DOES_NOT_RETURN
58 void
59 Parrot_exit(PARROT_INTERP, int status)
61 /* call all the exit handlers */
62 /* we are well "below" the runloop now, where lo_var_ptr
63 * is set usually - exit handlers may run some resource-hungry
64 * stuff like printing profile stats - a DOD run would kill
65 * resources - TODO reset stacktop or better disable GC
68 * we don't allow new exit_handlers being installed inside exit handlers
69 * - do we?
70 * and: interp->exit_handler_list is gone, after the last exit handler
71 * (Parrot_really_destroy) has run
73 handler_node_t *node = interp->exit_handler_list;
75 Parrot_block_DOD(interp);
76 Parrot_block_GC(interp);
78 while (node) {
79 handler_node_t * const next = node->next;
81 (node->function)(interp, status, node->arg);
82 mem_sys_free(node);
83 node = next;
85 exit(status);
90 =head1 SEE ALSO
92 F<include/parrot/exit.h> and F<t/src/exit.t>.
94 =head1 HISTORY
96 Initial version by Josh Wilmes.
102 * Local variables:
103 * c-file-style: "parrot"
104 * End:
105 * vim: expandtab shiftwidth=4: