VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/once.cpp@ 30468

Last change on this file since 30468 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: 5.8 KB
Line 
1/* $Id: once.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Execute Once.
4 */
5
6/*
7 * Copyright (C) 2007 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/once.h>
32#include "internal/iprt.h"
33
34#include <iprt/semaphore.h>
35#include <iprt/thread.h>
36#include <iprt/err.h>
37#include <iprt/assert.h>
38#include <iprt/asm.h>
39
40
41
42RTDECL(int) RTOnce(PRTONCE pOnce, PFNRTONCE pfnOnce, void *pvUser1, void *pvUser2)
43{
44 /*
45 * Validate input (strict builds only).
46 */
47 AssertPtr(pOnce);
48 AssertPtr(pfnOnce);
49
50 /*
51 * Deal with the 'initialized' case first
52 */
53 int32_t iState = ASMAtomicUoReadS32(&pOnce->iState);
54 if (RT_LIKELY(iState == 2))
55 return ASMAtomicUoReadS32(&pOnce->rc);
56 AssertReturn(iState == -1 || iState == 1, VERR_INTERNAL_ERROR);
57
58 /*
59 * Do we initialize it?
60 */
61 if (iState == -1)
62 {
63 RTSEMEVENTMULTI hEventMulti;
64 int rc = RTSemEventMultiCreate(&hEventMulti);
65 if (RT_FAILURE(rc))
66 hEventMulti = NIL_RTSEMEVENTMULTI;
67
68 if (ASMAtomicCmpXchgS32(&pOnce->iState, 1, -1))
69 {
70 ASMAtomicWriteHandle(&pOnce->hEventMulti, hEventMulti);
71 ASMAtomicIncS32(&pOnce->cEventRefs);
72
73 /* do the execute once stuff. */
74 int32_t rcOnce = pfnOnce(pvUser1, pvUser2);
75
76 /* set the return code, change the state and signal any waiters. */
77 ASMAtomicWriteS32(&pOnce->rc, rcOnce);
78 ASMAtomicWriteS32(&pOnce->iState, 2);
79 if (hEventMulti != NIL_RTSEMEVENTMULTI)
80 RTSemEventMultiSignal(hEventMulti);
81
82 /* last guy destroys the semaphore. */
83 if (ASMAtomicDecS32(&pOnce->cEventRefs) == 0)
84 ASMAtomicWriteSize(&pOnce->hEventMulti, NIL_RTSEMEVENTMULTI);
85 else
86 hEventMulti = NIL_RTSEMEVENTMULTI;
87 }
88 if (hEventMulti != NIL_RTSEMEVENTMULTI)
89 RTSemEventMultiDestroy(hEventMulti);
90 }
91
92 /*
93 * Wait for it to finish initializing.
94 */
95 if (ASMAtomicReadS32(&pOnce->iState) == 1)
96 {
97 int i = 0;
98 while (ASMAtomicReadS32(&pOnce->iState) == 1)
99 {
100 bool fYieldSleep = true;
101
102 /*
103 * Take care not to increment the counter if it's 0, that indicates
104 * that RTONCE::hEventMulti isn't valid either because it's not set
105 * yet, or because it's being destroyed.
106 */
107 int32_t cEventRefs = ASMAtomicUoReadS32(&pOnce->cEventRefs);
108 while ( cEventRefs > 0
109 && !ASMAtomicCmpXchgS32(&pOnce->cEventRefs, cEventRefs + 1, cEventRefs))
110 cEventRefs = ASMAtomicUoReadS32(&pOnce->cEventRefs);
111 if (cEventRefs > 1)
112 {
113 /*
114 * The hEventMulti might be NIL for two reasons, see above in
115 * the init code, if it isn't valid just do the yield/sleep thing.
116 */
117 RTSEMEVENTMULTI hEventMulti;
118 ASMAtomicUoReadHandle(&pOnce->hEventMulti, &hEventMulti);
119 if (hEventMulti != NIL_RTSEMEVENTMULTI)
120 {
121 fYieldSleep = false;
122 RTSemEventMultiWait(hEventMulti, RT_INDEFINITE_WAIT);
123 }
124
125 /*
126 * Last thread cleans up.
127 */
128 if (ASMAtomicDecS32(&pOnce->cEventRefs) == 0)
129 {
130 ASMAtomicXchgHandle(&pOnce->hEventMulti, NIL_RTSEMEVENTMULTI, &hEventMulti);
131 if (hEventMulti != NIL_RTSEMEVENTMULTI)
132 RTSemEventMultiDestroy(hEventMulti);
133 }
134 }
135
136 /*
137 * If we didn't block, yield or sleep for a bit.
138 *
139 * The sleep is essential to prevent higher priority threads from spinning wildly
140 * and preventing a lower priority thread from completing the pfnOnce operation
141 * in a timely manner.
142 */
143 if (fYieldSleep)
144 {
145 if (ASMAtomicReadS32(&pOnce->iState) != 1)
146 break;
147 if (!(++i % 8) )
148 RTThreadSleep(1);
149 else
150 RTThreadYield();
151 }
152 }
153 }
154
155 /*
156 * Finally, return the status code from the execute once function.
157 */
158 return ASMAtomicUoReadS32(&pOnce->rc);
159}
160RT_EXPORT_SYMBOL(RTOnce);
161
162
163RTDECL(void) RTOnceReset(PRTONCE pOnce)
164{
165 /* Cannot be done while busy! */
166 AssertPtr(pOnce);
167 Assert(pOnce->hEventMulti == NIL_RTSEMEVENTMULTI);
168 Assert(pOnce->iState != 1);
169
170 /* Do the same as RTONCE_INITIALIZER does. */
171 ASMAtomicWriteS32(&pOnce->rc, VERR_INTERNAL_ERROR);
172 ASMAtomicWriteS32(&pOnce->iState, -1);
173}
174RT_EXPORT_SYMBOL(RTOnceReset);
175
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