VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/mpnotification-r0drv.c@ 28800

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: mpnotification-r0drv.c 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Ring-0 Driver, Event Notifications.
4 */
5
6/*
7 * Copyright (C) 2008 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/mp.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/spinlock.h>
39#include <iprt/string.h>
40#include <iprt/thread.h>
41#include "r0drv/mp-r0drv.h"
42
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47/**
48 * Notification registration record tracking
49 * RTMpRegisterNotification() calls.
50 */
51typedef struct RTMPNOTIFYREG
52{
53 /** Pointer to the next record. */
54 struct RTMPNOTIFYREG * volatile pNext;
55 /** The callback. */
56 PFNRTMPNOTIFICATION pfnCallback;
57 /** The user argument. */
58 void *pvUser;
59 /** Bit mask indicating whether we've done this callback or not. */
60 uint8_t bmDone[sizeof(void *)];
61} RTMPNOTIFYREG;
62/** Pointer to a registration record. */
63typedef RTMPNOTIFYREG *PRTMPNOTIFYREG;
64
65
66/*******************************************************************************
67* Global Variables *
68*******************************************************************************/
69/** The spinlock protecting the list. */
70static RTSPINLOCK volatile g_hRTMpNotifySpinLock = NIL_RTSPINLOCK;
71/** List of callbacks, in registration order. */
72static PRTMPNOTIFYREG volatile g_pRTMpCallbackHead = NULL;
73/** The current done bit. */
74static uint32_t volatile g_iRTMpDoneBit;
75/** The list generation.
76 * This is increased whenever the list has been modified. The callback routine
77 * make use of this to avoid having restart at the list head after each callback. */
78static uint32_t volatile g_iRTMpGeneration;
79
80
81
82
83/**
84 * This is called by the native code.
85 *
86 * @param idCpu The CPU id the event applies to.
87 * @param enmEvent The event.
88 */
89void rtMpNotificationDoCallbacks(RTMPEVENT enmEvent, RTCPUID idCpu)
90{
91 PRTMPNOTIFYREG pCur;
92 RTSPINLOCK hSpinlock;
93 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
94
95 /*
96 * This is a little bit tricky as we cannot be holding the spinlock
97 * while calling the callback. This means that the list might change
98 * while we're walking it, and that multiple events might be running
99 * concurrently (depending on the OS).
100 *
101 * So, the first measure is to employ a 32-bitmask for each
102 * record where we'll use a bit that rotates for each call to
103 * this function to indicate which records that has been
104 * processed. This will take care of both changes to the list
105 * and a reasonable amount of concurrent events.
106 *
107 * In order to avoid having to restart the list walks for every
108 * callback we make, we'll make use a list generation number that is
109 * incremented everytime the list is changed. So, if it remains
110 * unchanged over a callback we can safely continue the iteration.
111 */
112 uint32_t iDone = ASMAtomicIncU32(&g_iRTMpDoneBit);
113 iDone %= RT_SIZEOFMEMB(RTMPNOTIFYREG, bmDone) * 8;
114
115 hSpinlock = g_hRTMpNotifySpinLock;
116 if (hSpinlock == NIL_RTSPINLOCK)
117 return;
118 RTSpinlockAcquire(hSpinlock, &Tmp);
119
120 /* Clear the bit. */
121 for (pCur = g_pRTMpCallbackHead; pCur; pCur = pCur->pNext)
122 ASMAtomicBitClear(&pCur->bmDone[0], iDone);
123
124 /* Iterate the records and perform the callbacks. */
125 do
126 {
127 uint32_t const iGeneration = ASMAtomicUoReadU32(&g_iRTMpGeneration);
128
129 pCur = g_pRTMpCallbackHead;
130 while (pCur)
131 {
132 if (!ASMAtomicBitTestAndSet(&pCur->bmDone[0], iDone))
133 {
134 PFNRTMPNOTIFICATION pfnCallback = pCur->pfnCallback;
135 void *pvUser = pCur->pvUser;
136 pCur = pCur->pNext;
137 RTSpinlockRelease(g_hRTMpNotifySpinLock, &Tmp);
138
139 pfnCallback(enmEvent, idCpu, pvUser);
140
141 /* carefully require the lock here, see RTR0MpNotificationTerm(). */
142 hSpinlock = g_hRTMpNotifySpinLock;
143 if (hSpinlock == NIL_RTSPINLOCK)
144 return;
145 RTSpinlockAcquire(hSpinlock, &Tmp);
146 if (ASMAtomicUoReadU32(&g_iRTMpGeneration) != iGeneration)
147 break;
148 }
149 else
150 pCur = pCur->pNext;
151 }
152 } while (pCur);
153
154 RTSpinlockRelease(hSpinlock, &Tmp);
155}
156
157
158
159RTDECL(int) RTMpNotificationRegister(PFNRTMPNOTIFICATION pfnCallback, void *pvUser)
160{
161 PRTMPNOTIFYREG pCur;
162 PRTMPNOTIFYREG pNew;
163 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
164
165 /*
166 * Validation.
167 */
168 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
169 AssertReturn(g_hRTMpNotifySpinLock != NIL_RTSPINLOCK, VERR_WRONG_ORDER);
170 RT_ASSERT_PREEMPTIBLE();
171
172 RTSpinlockAcquire(g_hRTMpNotifySpinLock, &Tmp);
173 for (pCur = g_pRTMpCallbackHead; pCur; pCur = pCur->pNext)
174 if ( pCur->pvUser == pvUser
175 && pCur->pfnCallback == pfnCallback)
176 break;
177 RTSpinlockRelease(g_hRTMpNotifySpinLock, &Tmp);
178 AssertMsgReturn(!pCur, ("pCur=%p pfnCallback=%p pvUser=%p\n", pCur, pfnCallback, pvUser), VERR_ALREADY_EXISTS);
179
180 /*
181 * Allocate a new record and attempt to insert it.
182 */
183 pNew = (PRTMPNOTIFYREG)RTMemAlloc(sizeof(*pNew));
184 if (!pNew)
185 return VERR_NO_MEMORY;
186
187 pNew->pNext = NULL;
188 pNew->pfnCallback = pfnCallback;
189 pNew->pvUser = pvUser;
190 memset(&pNew->bmDone[0], 0xff, sizeof(pNew->bmDone));
191
192 RTSpinlockAcquire(g_hRTMpNotifySpinLock, &Tmp);
193
194 pCur = g_pRTMpCallbackHead;
195 if (!pCur)
196 g_pRTMpCallbackHead = pNew;
197 else
198 {
199 for (pCur = g_pRTMpCallbackHead; ; pCur = pCur->pNext)
200 if ( pCur->pvUser == pvUser
201 && pCur->pfnCallback == pfnCallback)
202 break;
203 else if (!pCur->pNext)
204 {
205 pCur->pNext = pNew;
206 pCur = NULL;
207 break;
208 }
209 }
210
211 ASMAtomicIncU32(&g_iRTMpGeneration);
212
213 RTSpinlockRelease(g_hRTMpNotifySpinLock, &Tmp);
214
215 /* duplicate? */
216 if (pCur)
217 {
218 RTMemFree(pCur);
219 AssertMsgFailedReturn(("pCur=%p pfnCallback=%p pvUser=%p\n", pCur, pfnCallback, pvUser), VERR_ALREADY_EXISTS);
220 }
221
222 return VINF_SUCCESS;
223}
224RT_EXPORT_SYMBOL(RTMpNotificationRegister);
225
226
227RTDECL(int) RTMpNotificationDeregister(PFNRTMPNOTIFICATION pfnCallback, void *pvUser)
228{
229 PRTMPNOTIFYREG pPrev;
230 PRTMPNOTIFYREG pCur;
231 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
232
233 /*
234 * Validation.
235 */
236 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
237 AssertReturn(g_hRTMpNotifySpinLock != NIL_RTSPINLOCK, VERR_WRONG_ORDER);
238 RT_ASSERT_INTS_ON();
239
240 /*
241 * Find and unlink the record from the list.
242 */
243 RTSpinlockAcquire(g_hRTMpNotifySpinLock, &Tmp);
244 pPrev = NULL;
245 for (pCur = g_pRTMpCallbackHead; pCur; pCur = pCur->pNext)
246 {
247 if ( pCur->pvUser == pvUser
248 && pCur->pfnCallback == pfnCallback)
249 break;
250 pPrev = pCur;
251 }
252 if (pCur)
253 {
254 if (pPrev)
255 pPrev->pNext = pCur->pNext;
256 else
257 g_pRTMpCallbackHead = pCur->pNext;
258 ASMAtomicIncU32(&g_iRTMpGeneration);
259 }
260 RTSpinlockRelease(g_hRTMpNotifySpinLock, &Tmp);
261
262 if (!pCur)
263 return VERR_NOT_FOUND;
264
265 /*
266 * Invalidate and free the record.
267 */
268 pCur->pNext = NULL;
269 pCur->pfnCallback = NULL;
270 RTMemFree(pCur);
271
272 return VINF_SUCCESS;
273}
274RT_EXPORT_SYMBOL(RTMpNotificationDeregister);
275
276
277int rtR0MpNotificationInit(void)
278{
279 int rc = RTSpinlockCreate((PRTSPINLOCK)&g_hRTMpNotifySpinLock);
280 if (RT_SUCCESS(rc))
281 {
282 rc = rtR0MpNotificationNativeInit();
283 if (RT_SUCCESS(rc))
284 return rc;
285
286 RTSpinlockDestroy(g_hRTMpNotifySpinLock);
287 g_hRTMpNotifySpinLock = NIL_RTSPINLOCK;
288 }
289 return rc;
290}
291
292
293void rtR0MpNotificationTerm(void)
294{
295 PRTMPNOTIFYREG pHead;
296 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
297 RTSPINLOCK hSpinlock = g_hRTMpNotifySpinLock;
298 AssertReturnVoid(hSpinlock != NIL_RTSPINLOCK);
299
300 rtR0MpNotificationNativeTerm();
301
302 /* pick up the list and the spinlock. */
303 RTSpinlockAcquire(hSpinlock, &Tmp);
304 ASMAtomicWriteSize(&g_hRTMpNotifySpinLock, NIL_RTSPINLOCK);
305 pHead = g_pRTMpCallbackHead;
306 g_pRTMpCallbackHead = NULL;
307 ASMAtomicIncU32(&g_iRTMpGeneration);
308 RTSpinlockRelease(hSpinlock, &Tmp);
309
310 /* free the list. */
311 while (pHead)
312 {
313 PRTMPNOTIFYREG pFree = pHead;
314 pHead = pHead->pNext;
315
316 pFree->pNext = NULL;
317 pFree->pfnCallback = NULL;
318 RTMemFree(pFree);
319 }
320
321 RTSpinlockDestroy(hSpinlock);
322}
323
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