VirtualBox

source: vbox/trunk/include/iprt/once.h@ 55747

Last change on this file since 55747 was 43879, checked in by vboxsync, 12 years ago

Extended RTOnce with termination cleanups. (Changes existing structures and functions.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/** @file
2 * IPRT - Execute Once.
3 */
4
5/*
6 * Copyright (C) 2006-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_once_h
27#define ___iprt_once_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/asm.h>
32#include <iprt/err.h>
33#include <iprt/list.h>
34
35RT_C_DECLS_BEGIN
36
37/** @defgroup grp_rt_once RTOnce - Execute Once
38 * @ingroup grp_rt
39 * @{
40 */
41
42/**
43 * Callback that gets executed once.
44 *
45 * @returns IPRT style status code, RTOnce returns this.
46 *
47 * @param pvUser The user parameter.
48 */
49typedef DECLCALLBACK(int32_t) FNRTONCE(void *pvUser);
50/** Pointer to a FNRTONCE. */
51typedef FNRTONCE *PFNRTONCE;
52
53/**
54 * Callback that gets executed on IPRT/process termination.
55 *
56 * @param pvUser The user parameter.
57 * @param fLazyCleanUpOk Indicates whether lazy clean-up is OK (see
58 * initterm.h).
59 */
60typedef DECLCALLBACK(void) FNRTONCECLEANUP(void *pvUser, bool fLazyCleanUpOk);
61/** Pointer to a FNRTONCE. */
62typedef FNRTONCECLEANUP *PFNRTONCECLEANUP;
63
64/**
65 * Execute once structure.
66 *
67 * This is typically a global variable that is statically initialized
68 * by RTONCE_INITIALIZER.
69 */
70typedef struct RTONCE
71{
72 /** Event semaphore that the other guys are blocking on. */
73 RTSEMEVENTMULTI volatile hEventMulti;
74 /** Reference counter for hEventMulti. */
75 int32_t volatile cEventRefs;
76 /** See RTONCESTATE. */
77 int32_t volatile iState;
78 /** The return code of pfnOnce. */
79 int32_t volatile rc;
80
81 /** Pointer to the clean-up function. */
82 PFNRTONCECLEANUP pfnCleanUp;
83 /** Argument to hand to the clean-up function. */
84 void *pvUser;
85 /** Clean-up list entry. */
86 RTLISTNODE CleanUpNode;
87} RTONCE;
88/** Pointer to a execute once struct. */
89typedef RTONCE *PRTONCE;
90
91/**
92 * The execute once statemachine.
93 */
94typedef enum RTONCESTATE
95{
96 /** RTOnce() has not been called.
97 * Next: NO_SEM */
98 RTONCESTATE_UNINITIALIZED = 1,
99 /** RTOnce() is busy, no race.
100 * Next: CREATING_SEM, DONE */
101 RTONCESTATE_BUSY_NO_SEM,
102 /** More than one RTOnce() caller is busy.
103 * Next: BUSY_HAVE_SEM, BUSY_SPIN, DONE_CREATING_SEM, DONE */
104 RTONCESTATE_BUSY_CREATING_SEM,
105 /** More than one RTOnce() caller, the first is busy, the others are
106 * waiting.
107 * Next: DONE */
108 RTONCESTATE_BUSY_HAVE_SEM,
109 /** More than one RTOnce() caller, the first is busy, the others failed to
110 * create a semaphore and are spinning.
111 * Next: DONE */
112 RTONCESTATE_BUSY_SPIN,
113 /** More than one RTOnce() caller, the first has completed, the others
114 * are busy creating the semaphore.
115 * Next: DONE_HAVE_SEM */
116 RTONCESTATE_DONE_CREATING_SEM,
117 /** More than one RTOnce() caller, the first is busy grabbing the
118 * semaphore, while the others are waiting.
119 * Next: DONE */
120 RTONCESTATE_DONE_HAVE_SEM,
121 /** The execute once stuff has completed. */
122 RTONCESTATE_DONE = 16
123} RTONCESTATE;
124
125/** Static initializer for RTONCE variables. */
126#define RTONCE_INITIALIZER \
127 { NIL_RTSEMEVENTMULTI, 0, RTONCESTATE_UNINITIALIZED, VERR_INTERNAL_ERROR, NULL, NULL, { NULL, NULL } }
128
129
130/**
131 * Serializes execution of the pfnOnce function, making sure it's
132 * executed exactly once and that nobody returns from RTOnce before
133 * it has executed successfully.
134 *
135 * @returns IPRT like status code returned by pfnOnce.
136 *
137 * @param pOnce Pointer to the execute once variable.
138 * @param pfnOnce The function to executed once.
139 * @param pfnCleanUp The function that will be doing the cleaning up.
140 * Optional.
141 * @param pvUser The user parameter for pfnOnce.
142 */
143RTDECL(int) RTOnceSlow(PRTONCE pOnce, PFNRTONCE pfnOnce, FNRTONCECLEANUP pfnCleanUp, void *pvUser);
144
145/**
146 * Serializes execution of the pfnOnce function, making sure it's
147 * executed exactly once and that nobody returns from RTOnce before
148 * it has executed successfully.
149 *
150 * @returns IPRT like status code returned by pfnOnce.
151 *
152 * @param pOnce Pointer to the execute once variable.
153 * @param pfnOnce The function to executed once.
154 * @param pvUser The user parameter for pfnOnce.
155 */
156DECLINLINE(int) RTOnce(PRTONCE pOnce, PFNRTONCE pfnOnce, void *pvUser)
157{
158 int32_t iState = ASMAtomicUoReadS32(&pOnce->iState);
159 if (RT_LIKELY( iState == RTONCESTATE_DONE
160 || iState == RTONCESTATE_DONE_CREATING_SEM
161 || iState == RTONCESTATE_DONE_HAVE_SEM ))
162 return ASMAtomicUoReadS32(&pOnce->rc);
163 return RTOnceSlow(pOnce, pfnOnce, NULL, pvUser);
164}
165
166/**
167 * Execute pfnOnce once and register a termination clean-up callback.
168 *
169 * Serializes execution of the pfnOnce function, making sure it's
170 * executed exactly once and that nobody returns from RTOnce before
171 * it has executed successfully.
172 *
173 * @returns IPRT like status code returned by pfnOnce.
174 *
175 * @param pOnce Pointer to the execute once variable.
176 * @param pfnOnce The function to executed once.
177 * @param pfnCleanUp The function that will be doing the cleaning up.
178 * @param pvUser The user parameter for pfnOnce.
179 */
180DECLINLINE(int) RTOnceEx(PRTONCE pOnce, PFNRTONCE pfnOnce, PFNRTONCECLEANUP pfnCleanUp, void *pvUser)
181{
182 int32_t iState = ASMAtomicUoReadS32(&pOnce->iState);
183 if (RT_LIKELY( iState == RTONCESTATE_DONE
184 || iState == RTONCESTATE_DONE_CREATING_SEM
185 || iState == RTONCESTATE_DONE_HAVE_SEM ))
186 return ASMAtomicUoReadS32(&pOnce->rc);
187 return RTOnceSlow(pOnce, pfnOnce, pfnCleanUp, pvUser);
188}
189
190
191/**
192 * Resets an execute once variable.
193 *
194 * The caller is responsible for making sure there are no concurrent accesses to
195 * the execute once variable.
196 *
197 * @param pOnce Pointer to the execute once variable.
198 */
199RTDECL(void) RTOnceReset(PRTONCE pOnce);
200
201/** @} */
202
203RT_C_DECLS_END
204
205#endif
206
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