Starting release 0.7.0
[parrot.git] / src / exit.c
blob94d6510b5762d3f644c5c198b3dc8ce5f1e9e0cb
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
18 =over 4
20 =cut
24 #include <stdlib.h>
25 #include "parrot/parrot.h"
27 /* HEADERIZER HFILE: include/parrot/exit.h */
31 =item C<void Parrot_on_exit>
33 Register the specified function to be called on exit.
35 =cut
39 PARROT_API
40 void
41 Parrot_on_exit(PARROT_INTERP, NOTNULL(exit_handler_f function), ARGIN_NULLOK(void *arg))
43 /* RT#46403 we might want locking around the list access. I'm sure this
44 * will be the least of the threading issues. */
46 handler_node_t * const new_node = mem_allocate_typed(handler_node_t);
48 new_node->function = function;
49 new_node->arg = arg;
50 new_node->next = interp->exit_handler_list;
51 interp->exit_handler_list = new_node;
56 =item C<void Parrot_exit>
58 Exit, calling any registered exit handlers.
60 =cut
64 PARROT_API
65 PARROT_DOES_NOT_RETURN
66 void
67 Parrot_exit(PARROT_INTERP, int status)
69 /* call all the exit handlers */
70 /* we are well "below" the runloop now, where lo_var_ptr
71 * is set usually - exit handlers may run some resource-hungry
72 * stuff like printing profile stats - a DOD run would kill
73 * resources - RT#46405 reset stacktop or better disable GC
76 * we don't allow new exit_handlers being installed inside exit handlers
77 * - do we?
78 * and: interp->exit_handler_list is gone, after the last exit handler
79 * (Parrot_really_destroy) has run
81 handler_node_t *node = interp->exit_handler_list;
83 Parrot_block_GC_mark(interp);
84 Parrot_block_GC_sweep(interp);
86 while (node) {
87 handler_node_t * const next = node->next;
89 (node->function)(interp, status, node->arg);
90 mem_sys_free(node);
91 node = next;
94 exit(status);
99 =back
101 =head1 SEE ALSO
103 F<include/parrot/exit.h> and F<t/src/exit.t>.
105 =head1 HISTORY
107 Initial version by Josh Wilmes.
109 =cut
115 * Local variables:
116 * c-file-style: "parrot"
117 * End:
118 * vim: expandtab shiftwidth=4: