maximum line length in a pod (pdd) is 78 characters
[parrot.git] / src / exit.c
blob7d520dd90995cba64cf56fac5067f8117508b434
1 /*
2 Copyright (C) 2001-2010, Parrot 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(PARROT_INTERP, exit_handler_f function, void *arg)>
33 Register the specified function to be called on exit.
35 =cut
39 PARROT_EXPORT
40 void
41 Parrot_on_exit(PARROT_INTERP, ARGIN(exit_handler_f function), ARGIN_NULLOK(void *arg))
43 ASSERT_ARGS(Parrot_on_exit)
45 handler_node_t * const new_node = mem_internal_allocate_typed(handler_node_t);
47 new_node->function = function;
48 new_node->arg = arg;
49 new_node->next = interp->exit_handler_list;
50 interp->exit_handler_list = new_node;
55 =item C<void Parrot_exit(PARROT_INTERP, int status)>
57 Exit, calling any registered exit handlers.
59 =cut
63 PARROT_EXPORT
64 PARROT_DOES_NOT_RETURN
65 void
66 Parrot_exit(PARROT_INTERP, int status)
68 ASSERT_ARGS(Parrot_exit)
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 GC run would kill
73 * resources
74 * http://rt.perl.org/rt3/Ticket/Display.html?id=46405 (resolved)
77 * we don't allow new exit_handlers being installed inside exit handlers
78 * - do we?
79 * and: interp->exit_handler_list is gone, after the last exit handler
80 * (Parrot_really_destroy) has run
82 handler_node_t *node = interp->exit_handler_list;
84 Parrot_block_GC_mark(interp);
85 Parrot_block_GC_sweep(interp);
87 while (node) {
88 handler_node_t * const next = node->next;
90 (node->function)(interp, status, node->arg);
91 mem_internal_free(node);
92 node = next;
95 exit(status);
100 =back
102 =head1 SEE ALSO
104 F<include/parrot/exit.h> and F<t/src/exit.t>.
106 =head1 HISTORY
108 Initial version by Josh Wilmes.
110 =cut
116 * Local variables:
117 * c-file-style: "parrot"
118 * End:
119 * vim: expandtab shiftwidth=4: