1 | /* $Id: term.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Common Termination Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.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 <iprt/initterm.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/once.h>
|
---|
39 | #include <iprt/semaphore.h>
|
---|
40 | #include <iprt/thread.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | /** Pointer to a termination callback record. */
|
---|
47 | typedef struct RTTERMCALLBACKREC *PRTTERMCALLBACKREC;
|
---|
48 | /**
|
---|
49 | * Termination callback record.
|
---|
50 | */
|
---|
51 | typedef struct RTTERMCALLBACKREC
|
---|
52 | {
|
---|
53 | /** Pointer to the next record. */
|
---|
54 | PRTTERMCALLBACKREC pNext;
|
---|
55 | /** Pointer to the callback. */
|
---|
56 | PFNRTTERMCALLBACK pfnCallback;
|
---|
57 | /** The user argument. */
|
---|
58 | void *pvUser;
|
---|
59 | } RTTERMCALLBACKREC;
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Global Variables *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 | /** Execute once construct protecting lazy callback initialization. */
|
---|
66 | static RTONCE g_InitTermCallbacksOnce = RTONCE_INITIALIZER;
|
---|
67 | /** Mutex protecting the callback globals. */
|
---|
68 | static RTSEMFASTMUTEX g_hFastMutex = NIL_RTSEMFASTMUTEX;
|
---|
69 | /** Number of registered callbacks. */
|
---|
70 | static uint32_t g_cCallbacks = 0;
|
---|
71 | /** The callback head. */
|
---|
72 | static PRTTERMCALLBACKREC g_pCallbackHead = NULL;
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Initializes the globals.
|
---|
78 | *
|
---|
79 | * @returns IPRT status code
|
---|
80 | * @param pvUser Ignored.
|
---|
81 | */
|
---|
82 | static DECLCALLBACK(int32_t) rtTermInitOnce(void *pvUser)
|
---|
83 | {
|
---|
84 | RTSEMFASTMUTEX hFastMutex;
|
---|
85 | int rc;
|
---|
86 |
|
---|
87 | Assert(!g_cCallbacks);
|
---|
88 | Assert(!g_pCallbackHead);
|
---|
89 | Assert(g_hFastMutex == NIL_RTSEMFASTMUTEX);
|
---|
90 |
|
---|
91 | rc = RTSemFastMutexCreate(&hFastMutex);
|
---|
92 | if (RT_SUCCESS(rc))
|
---|
93 | g_hFastMutex = hFastMutex;
|
---|
94 |
|
---|
95 | NOREF(pvUser);
|
---|
96 |
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(int) RTTermRegisterCallback(PFNRTTERMCALLBACK pfnCallback, void *pvUser)
|
---|
103 | {
|
---|
104 | int rc;
|
---|
105 | PRTTERMCALLBACKREC pNew;
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Validation and lazy init.
|
---|
109 | */
|
---|
110 | AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
|
---|
111 |
|
---|
112 | rc = RTOnce(&g_InitTermCallbacksOnce, rtTermInitOnce, NULL);
|
---|
113 | if (RT_FAILURE(rc))
|
---|
114 | return rc;
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Allocate and initialize a new callback record.
|
---|
118 | */
|
---|
119 | pNew = (PRTTERMCALLBACKREC)RTMemAlloc(sizeof(*pNew));
|
---|
120 | if (!pNew)
|
---|
121 | return VERR_NO_MEMORY;
|
---|
122 | pNew->pfnCallback = pfnCallback;
|
---|
123 | pNew->pvUser = pvUser;
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Insert into the list.
|
---|
127 | */
|
---|
128 | rc = RTSemFastMutexRequest(g_hFastMutex);
|
---|
129 | if (RT_SUCCESS(rc))
|
---|
130 | {
|
---|
131 | g_cCallbacks++;
|
---|
132 | pNew->pNext = g_pCallbackHead;
|
---|
133 | g_pCallbackHead = pNew;
|
---|
134 |
|
---|
135 | RTSemFastMutexRelease(g_hFastMutex);
|
---|
136 | }
|
---|
137 | else
|
---|
138 | RTMemFree(pNew);
|
---|
139 |
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 | RT_EXPORT_SYMBOL(RTTermRegisterCallback);
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(int) RTTermDeregisterCallback(PFNRTTERMCALLBACK pfnCallback, void *pvUser)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * g_hFastMutex will be NIL if we're not initialized.
|
---|
149 | */
|
---|
150 | int rc;
|
---|
151 | RTSEMFASTMUTEX hFastMutex = g_hFastMutex;
|
---|
152 | if (hFastMutex == NIL_RTSEMFASTMUTEX)
|
---|
153 | return VERR_NOT_FOUND;
|
---|
154 |
|
---|
155 | rc = RTSemFastMutexRequest(hFastMutex);
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | {
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Search for the specified pfnCallback/pvUser pair.
|
---|
161 | */
|
---|
162 | PRTTERMCALLBACKREC pPrev = NULL;
|
---|
163 | PRTTERMCALLBACKREC pCur = g_pCallbackHead;
|
---|
164 | while (pCur)
|
---|
165 | {
|
---|
166 | if ( pCur->pfnCallback == pfnCallback
|
---|
167 | && pCur->pvUser == pvUser)
|
---|
168 | {
|
---|
169 | if (pPrev)
|
---|
170 | pPrev->pNext = pCur->pNext;
|
---|
171 | else
|
---|
172 | g_pCallbackHead = pCur->pNext;
|
---|
173 | g_cCallbacks--;
|
---|
174 | RTSemFastMutexRelease(hFastMutex);
|
---|
175 |
|
---|
176 | pCur->pfnCallback = NULL;
|
---|
177 | RTMemFree(pCur);
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* next */
|
---|
182 | pPrev = pCur;
|
---|
183 | pCur = pCur->pNext;
|
---|
184 | }
|
---|
185 |
|
---|
186 | RTSemFastMutexRelease(hFastMutex);
|
---|
187 | rc = VERR_NOT_FOUND;
|
---|
188 | }
|
---|
189 |
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 | RT_EXPORT_SYMBOL(RTTermDeregisterCallback);
|
---|
193 |
|
---|
194 |
|
---|
195 | RTDECL(void) RTTermRunCallbacks(RTTERMREASON enmReason, int32_t iStatus)
|
---|
196 | {
|
---|
197 | RTSEMFASTMUTEX hFastMutex;
|
---|
198 | Assert( enmReason == RTTERMREASON_EXIT
|
---|
199 | || enmReason == RTTERMREASON_ABEND
|
---|
200 | || enmReason == RTTERMREASON_SIGNAL
|
---|
201 | || enmReason == RTTERMREASON_UNLOAD);
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * Run the callback list. This is a bit paranoid in order to guard against
|
---|
205 | * recursive calls to RTTermRunCallbacks.
|
---|
206 | */
|
---|
207 | while (g_hFastMutex != NIL_RTSEMFASTMUTEX)
|
---|
208 | {
|
---|
209 | PRTTERMCALLBACKREC pCur;
|
---|
210 | RTTERMCALLBACKREC CurCopy;
|
---|
211 | int rc;
|
---|
212 |
|
---|
213 | /* Unlink the head of the chain. */
|
---|
214 | rc = RTSemFastMutexRequest(g_hFastMutex);
|
---|
215 | AssertRCReturnVoid(rc);
|
---|
216 | pCur = g_pCallbackHead;
|
---|
217 | if (pCur)
|
---|
218 | {
|
---|
219 | g_pCallbackHead = pCur->pNext;
|
---|
220 | g_cCallbacks--;
|
---|
221 | }
|
---|
222 | RTSemFastMutexRelease(g_hFastMutex);
|
---|
223 | if (!pCur)
|
---|
224 | break;
|
---|
225 |
|
---|
226 | /* Copy and free it. */
|
---|
227 | CurCopy = *pCur;
|
---|
228 | RTMemFree(pCur);
|
---|
229 |
|
---|
230 | /* Make the call. */
|
---|
231 | CurCopy.pfnCallback(enmReason, iStatus, CurCopy.pvUser);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Free the lock.
|
---|
236 | */
|
---|
237 | ASMAtomicXchgHandle(&g_hFastMutex, NIL_RTSEMFASTMUTEX, &hFastMutex);
|
---|
238 | RTSemFastMutexDestroy(hFastMutex);
|
---|
239 | RTOnceReset(&g_InitTermCallbacksOnce); /* for the testcase */
|
---|
240 | }
|
---|
241 | RT_EXPORT_SYMBOL(RTTermRunCallbacks);
|
---|
242 |
|
---|