1 | /* $Id: term.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Common Termination Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/initterm.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/errcore.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #include <iprt/once.h>
|
---|
49 | #include <iprt/semaphore.h>
|
---|
50 | #include <iprt/thread.h>
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Structures and Typedefs *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | /** Pointer to a termination callback record. */
|
---|
57 | typedef struct RTTERMCALLBACKREC *PRTTERMCALLBACKREC;
|
---|
58 | /**
|
---|
59 | * Termination callback record.
|
---|
60 | */
|
---|
61 | typedef struct RTTERMCALLBACKREC
|
---|
62 | {
|
---|
63 | /** Pointer to the next record. */
|
---|
64 | PRTTERMCALLBACKREC pNext;
|
---|
65 | /** Pointer to the callback. */
|
---|
66 | PFNRTTERMCALLBACK pfnCallback;
|
---|
67 | /** The user argument. */
|
---|
68 | void *pvUser;
|
---|
69 | } RTTERMCALLBACKREC;
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Global Variables *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 | /** Execute once construct protecting lazy callback initialization. */
|
---|
76 | static RTONCE g_InitTermCallbacksOnce = RTONCE_INITIALIZER;
|
---|
77 | /** Mutex protecting the callback globals. */
|
---|
78 | static RTSEMFASTMUTEX g_hFastMutex = NIL_RTSEMFASTMUTEX;
|
---|
79 | /** Number of registered callbacks. */
|
---|
80 | static uint32_t g_cCallbacks = 0;
|
---|
81 | /** The callback head. */
|
---|
82 | static PRTTERMCALLBACKREC g_pCallbackHead = NULL;
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Initializes the globals.
|
---|
88 | *
|
---|
89 | * @returns IPRT status code
|
---|
90 | * @param pvUser Ignored.
|
---|
91 | */
|
---|
92 | static DECLCALLBACK(int32_t) rtTermInitOnce(void *pvUser)
|
---|
93 | {
|
---|
94 | RTSEMFASTMUTEX hFastMutex;
|
---|
95 | int rc;
|
---|
96 |
|
---|
97 | Assert(!g_cCallbacks);
|
---|
98 | Assert(!g_pCallbackHead);
|
---|
99 | Assert(g_hFastMutex == NIL_RTSEMFASTMUTEX);
|
---|
100 |
|
---|
101 | rc = RTSemFastMutexCreate(&hFastMutex);
|
---|
102 | if (RT_SUCCESS(rc))
|
---|
103 | g_hFastMutex = hFastMutex;
|
---|
104 |
|
---|
105 | NOREF(pvUser);
|
---|
106 |
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 | RTDECL(int) RTTermRegisterCallback(PFNRTTERMCALLBACK pfnCallback, void *pvUser)
|
---|
113 | {
|
---|
114 | int rc;
|
---|
115 | PRTTERMCALLBACKREC pNew;
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Validation and lazy init.
|
---|
119 | */
|
---|
120 | AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
|
---|
121 |
|
---|
122 | rc = RTOnce(&g_InitTermCallbacksOnce, rtTermInitOnce, NULL);
|
---|
123 | if (RT_FAILURE(rc))
|
---|
124 | return rc;
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Allocate and initialize a new callback record.
|
---|
128 | */
|
---|
129 | pNew = (PRTTERMCALLBACKREC)RTMemAlloc(sizeof(*pNew));
|
---|
130 | if (!pNew)
|
---|
131 | return VERR_NO_MEMORY;
|
---|
132 | pNew->pfnCallback = pfnCallback;
|
---|
133 | pNew->pvUser = pvUser;
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * Insert into the list.
|
---|
137 | */
|
---|
138 | rc = RTSemFastMutexRequest(g_hFastMutex);
|
---|
139 | if (RT_SUCCESS(rc))
|
---|
140 | {
|
---|
141 | g_cCallbacks++;
|
---|
142 | pNew->pNext = g_pCallbackHead;
|
---|
143 | g_pCallbackHead = pNew;
|
---|
144 |
|
---|
145 | RTSemFastMutexRelease(g_hFastMutex);
|
---|
146 | }
|
---|
147 | else
|
---|
148 | RTMemFree(pNew);
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 | RT_EXPORT_SYMBOL(RTTermRegisterCallback);
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(int) RTTermDeregisterCallback(PFNRTTERMCALLBACK pfnCallback, void *pvUser)
|
---|
156 | {
|
---|
157 | /*
|
---|
158 | * g_hFastMutex will be NIL if we're not initialized.
|
---|
159 | */
|
---|
160 | int rc;
|
---|
161 | RTSEMFASTMUTEX hFastMutex = g_hFastMutex;
|
---|
162 | if (hFastMutex == NIL_RTSEMFASTMUTEX)
|
---|
163 | return VERR_NOT_FOUND;
|
---|
164 |
|
---|
165 | rc = RTSemFastMutexRequest(hFastMutex);
|
---|
166 | if (RT_SUCCESS(rc))
|
---|
167 | {
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Search for the specified pfnCallback/pvUser pair.
|
---|
171 | */
|
---|
172 | PRTTERMCALLBACKREC pPrev = NULL;
|
---|
173 | PRTTERMCALLBACKREC pCur = g_pCallbackHead;
|
---|
174 | while (pCur)
|
---|
175 | {
|
---|
176 | if ( pCur->pfnCallback == pfnCallback
|
---|
177 | && pCur->pvUser == pvUser)
|
---|
178 | {
|
---|
179 | if (pPrev)
|
---|
180 | pPrev->pNext = pCur->pNext;
|
---|
181 | else
|
---|
182 | g_pCallbackHead = pCur->pNext;
|
---|
183 | g_cCallbacks--;
|
---|
184 | RTSemFastMutexRelease(hFastMutex);
|
---|
185 |
|
---|
186 | pCur->pfnCallback = NULL;
|
---|
187 | RTMemFree(pCur);
|
---|
188 | return VINF_SUCCESS;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* next */
|
---|
192 | pPrev = pCur;
|
---|
193 | pCur = pCur->pNext;
|
---|
194 | }
|
---|
195 |
|
---|
196 | RTSemFastMutexRelease(hFastMutex);
|
---|
197 | rc = VERR_NOT_FOUND;
|
---|
198 | }
|
---|
199 |
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 | RT_EXPORT_SYMBOL(RTTermDeregisterCallback);
|
---|
203 |
|
---|
204 |
|
---|
205 | RTDECL(void) RTTermRunCallbacks(RTTERMREASON enmReason, int32_t iStatus)
|
---|
206 | {
|
---|
207 | RTSEMFASTMUTEX hFastMutex;
|
---|
208 | Assert( enmReason == RTTERMREASON_EXIT
|
---|
209 | || enmReason == RTTERMREASON_ABEND
|
---|
210 | || enmReason == RTTERMREASON_SIGNAL
|
---|
211 | || enmReason == RTTERMREASON_UNLOAD);
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * Run the callback list. This is a bit paranoid in order to guard against
|
---|
215 | * recursive calls to RTTermRunCallbacks.
|
---|
216 | */
|
---|
217 | while (g_hFastMutex != NIL_RTSEMFASTMUTEX)
|
---|
218 | {
|
---|
219 | PRTTERMCALLBACKREC pCur;
|
---|
220 | RTTERMCALLBACKREC CurCopy;
|
---|
221 | int rc;
|
---|
222 |
|
---|
223 | /* Unlink the head of the chain. */
|
---|
224 | rc = RTSemFastMutexRequest(g_hFastMutex);
|
---|
225 | AssertRCReturnVoid(rc);
|
---|
226 | pCur = g_pCallbackHead;
|
---|
227 | if (pCur)
|
---|
228 | {
|
---|
229 | g_pCallbackHead = pCur->pNext;
|
---|
230 | g_cCallbacks--;
|
---|
231 | }
|
---|
232 | RTSemFastMutexRelease(g_hFastMutex);
|
---|
233 | if (!pCur)
|
---|
234 | break;
|
---|
235 |
|
---|
236 | /* Copy and free it. */
|
---|
237 | CurCopy = *pCur;
|
---|
238 | RTMemFree(pCur);
|
---|
239 |
|
---|
240 | /* Make the call. */
|
---|
241 | CurCopy.pfnCallback(enmReason, iStatus, CurCopy.pvUser);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Free the lock.
|
---|
246 | */
|
---|
247 | ASMAtomicXchgHandle(&g_hFastMutex, NIL_RTSEMFASTMUTEX, &hFastMutex);
|
---|
248 | RTSemFastMutexDestroy(hFastMutex);
|
---|
249 | RTOnceReset(&g_InitTermCallbacksOnce); /* for the testcase */
|
---|
250 | }
|
---|
251 | RT_EXPORT_SYMBOL(RTTermRunCallbacks);
|
---|
252 |
|
---|