[CRT]
[reactos.git] / reactos / lib / sdk / crt / startup / atonexit.c
1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6
7 #undef CRTDLL
8 #ifndef _DLL
9 #define _DLL
10 #endif
11
12 #include <oscalls.h>
13 #include <internal.h>
14 #include <stdlib.h>
15 #include <crtdefs.h>
16 #include <limits.h>
17 #include <windows.h>
18
19 #define _EXIT_LOCK1 8
20
21 void __cdecl _lock (int _File);
22 void __cdecl _unlock (int _File);
23
24 _PVFV *__onexitbegin;
25 _PVFV *__onexitend;
26
27 extern _CRTIMP _onexit_t __dllonexit (_onexit_t, _PVFV**, _PVFV**);
28 extern _onexit_t (__cdecl * __MINGW_IMP_SYMBOL(_onexit)) (_onexit_t func);
29
30 /* Choose a different name to prevent name conflicts. The CRT one works fine. */
31 _onexit_t __cdecl mingw_onexit(_onexit_t func);
32
33 _onexit_t __cdecl mingw_onexit(_onexit_t func)
34 {
35 _PVFV *onexitbegin;
36 _PVFV *onexitend;
37 _onexit_t retval;
38
39 onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin);
40
41 if (onexitbegin == (_PVFV *) -1)
42 return (* __MINGW_IMP_SYMBOL(_onexit)) (func);
43 _lock (_EXIT_LOCK1);
44 onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin);
45 onexitend = (_PVFV *) _decode_pointer (__onexitend);
46
47 retval = __dllonexit (func, &onexitbegin, &onexitend);
48
49 __onexitbegin = (_PVFV *) _encode_pointer (onexitbegin);
50 __onexitend = (_PVFV *) _encode_pointer (onexitend);
51 _unlock (_EXIT_LOCK1);
52 return retval;
53 }
54
55 int __cdecl
56 atexit (_PVFV func)
57 {
58 return (mingw_onexit((_onexit_t)func) == NULL) ? -1 : 0;
59 }