1178354Ssam/* Copyright (C) 1999-2020 Free Software Foundation, Inc.
2178354Ssam
3178354Ssam   NOTE: This source is derived from an old version taken from the GNU C
4178354Ssam   Library (glibc).
5178354Ssam
6178354SsamThis file is part of GCC.
7178354Ssam
8178354SsamGCC is free software; you can redistribute it and/or modify it under
9178354Ssamthe terms of the GNU General Public License as published by the Free
10178354SsamSoftware Foundation; either version 3, or (at your option) any later
11178354Ssamversion.
12178354Ssam
13178354SsamGCC is distributed in the hope that it will be useful, but WITHOUT ANY
14178354SsamWARRANTY; without even the implied warranty of MERCHANTABILITY or
15178354SsamFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16178354Ssamfor more details.
17178354Ssam
18178354SsamUnder Section 7 of GPL version 3, you are granted additional
19178354Ssampermissions described in the GCC Runtime Library Exception, version
20178354Ssam3.1, as published by the Free Software Foundation.
21178354Ssam
22178354SsamYou should have received a copy of the GNU General Public License and
23178354Ssama copy of the GCC Runtime Library Exception along with this program;
24178354Ssamsee the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25178354Ssam<http://www.gnu.org/licenses/>.  */
26178354Ssam
27178354Ssam#include <stdlib.h>
28178354Ssam#include "exit.h"
29178354Ssam
30178354Ssam#define atomic_write_barrier() __asm__ ("eieio" ::: "memory")
31178354Ssam
32178354Ssam/* Register a function to be called by exit.  */
33178354Ssamint
34178354Ssamon_exit (void (*func) (int status, void *arg), void *arg)
35178354Ssam{
36178354Ssam  struct exit_function *new = __new_exitfn (&__exit_funcs);
37178354Ssam
38178354Ssam  if (new == NULL)
39178354Ssam    return -1;
40178354Ssam
41178354Ssam#ifdef PTR_MANGLE
42178354Ssam  PTR_MANGLE (func);
43178354Ssam#endif
44178354Ssam  new->func.on.fn = func;
45178354Ssam  new->func.on.arg = arg;
46178354Ssam  atomic_write_barrier ();
47178354Ssam  new->flavor = ef_on;
48178354Ssam  return 0;
49178354Ssam}
50178354Ssam