1 | /* $Id: atexit-vcc.cpp 96070 2022-08-05 23:08:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Visual C++ Compiler - Simple atexit implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/nocrt/stdlib.h>
|
---|
36 |
|
---|
37 | #include "internal/compiler-vcc.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Structures and Typedefs *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | typedef struct RTNOCRTATEXITCHUNK
|
---|
44 | {
|
---|
45 | PFNRTNOCRTATEXITCALLBACK apfnCallbacks[256];
|
---|
46 | } RTNOCRTATEXITCHUNK;
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Global Variables *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | /** The first atexit() registration chunk. */
|
---|
53 | static RTNOCRTATEXITCHUNK g_aAtExitPrealloc;
|
---|
54 | /** Array of atexit() callback chunk pointers. */
|
---|
55 | static RTNOCRTATEXITCHUNK *g_apAtExit[8192 / 256] = { &g_aAtExitPrealloc, };
|
---|
56 | /** Chunk and callback index in one. */
|
---|
57 | static volatile uint32_t g_idxNextAtExit = 0;
|
---|
58 |
|
---|
59 |
|
---|
60 | /* Note! not using atexit here because it'll clash with built-in prototype. */
|
---|
61 | extern "C" int nocrt_atexit(PFNRTNOCRTATEXITCALLBACK pfnCallback) RT_NOEXCEPT
|
---|
62 | {
|
---|
63 | AssertPtr(pfnCallback);
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Allocate a table index.
|
---|
67 | */
|
---|
68 | uint32_t idx = ASMAtomicIncU32(&g_idxNextAtExit) - 1;
|
---|
69 | AssertReturnStmt(idx < RT_ELEMENTS(g_apAtExit) * RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks),
|
---|
70 | ASMAtomicDecU32(&g_idxNextAtExit), -1);
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Make sure the table chunk is there.
|
---|
74 | */
|
---|
75 | uint32_t idxChunk = idx / RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
|
---|
76 | RTNOCRTATEXITCHUNK *pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
|
---|
77 | if (!pChunk)
|
---|
78 | {
|
---|
79 | pChunk = (RTNOCRTATEXITCHUNK *)RTMemAllocZ(sizeof(*pChunk)); /* ASSUMES that the allocator works w/o initialization! */
|
---|
80 | AssertReturn(pChunk, -1); /* don't try decrement, someone could be racing us... */
|
---|
81 |
|
---|
82 | if (!ASMAtomicCmpXchgPtr(&g_apAtExit[idxChunk], pChunk, NULL))
|
---|
83 | {
|
---|
84 | RTMemFree(pChunk);
|
---|
85 |
|
---|
86 | pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
|
---|
87 | Assert(pChunk);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Add our callback.
|
---|
93 | */
|
---|
94 | pChunk->apfnCallbacks[idxChunk % RT_ELEMENTS(pChunk->apfnCallbacks)] = pfnCallback;
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 | RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(atexit);
|
---|
98 |
|
---|
99 |
|
---|
100 | void rtVccTermRunAtExit(void) RT_NOEXCEPT
|
---|
101 | {
|
---|
102 | uint32_t idxAtExit = ASMAtomicReadU32(&g_idxNextAtExit);
|
---|
103 | if (idxAtExit-- > 0)
|
---|
104 | {
|
---|
105 | uint32_t idxChunk = idxAtExit / RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
|
---|
106 | uint32_t idxCallback = idxAtExit % RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
|
---|
107 | for (;;)
|
---|
108 | {
|
---|
109 | RTNOCRTATEXITCHUNK *pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
|
---|
110 | if (pChunk)
|
---|
111 | {
|
---|
112 | do
|
---|
113 | {
|
---|
114 | g_idxNextAtExit = idxAtExit--; /* Make sure we don't try problematic atexit callbacks! */
|
---|
115 |
|
---|
116 | PFNRTNOCRTATEXITCALLBACK pfnCallback = pChunk->apfnCallbacks[idxCallback];
|
---|
117 | if (pfnCallback) /* Can be NULL see registration code */
|
---|
118 | {
|
---|
119 | pfnCallback();
|
---|
120 | pChunk->apfnCallbacks[idxCallback] = NULL;
|
---|
121 | }
|
---|
122 | } while (idxCallback-- > 0);
|
---|
123 | }
|
---|
124 | else
|
---|
125 | idxAtExit -= RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
|
---|
126 | if (idxChunk == 0)
|
---|
127 | break;
|
---|
128 | idxChunk--;
|
---|
129 | idxCallback = RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks) - 1;
|
---|
130 | }
|
---|
131 |
|
---|
132 | g_idxNextAtExit = 0;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|