VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/term.cpp@ 25660

Last change on this file since 25660 was 21337, checked in by vboxsync, 15 years ago

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette