1 | /** @file
|
---|
2 | * IPRT - Execute Once.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef ___iprt_once_h
|
---|
31 | #define ___iprt_once_h
|
---|
32 |
|
---|
33 | #include <iprt/cdefs.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 |
|
---|
37 | RT_C_DECLS_BEGIN
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_once RTOnce - Execute Once
|
---|
40 | * @ingroup grp_rt
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Execute once structure.
|
---|
46 | *
|
---|
47 | * This is typically a global variable that is statically initialized
|
---|
48 | * by RTONCE_INITIALIZER.
|
---|
49 | */
|
---|
50 | typedef struct RTONCE
|
---|
51 | {
|
---|
52 | /** Event semaphore that the other guys are blocking on. */
|
---|
53 | RTSEMEVENTMULTI volatile hEventMulti;
|
---|
54 | /** Reference counter for hEventMulti. */
|
---|
55 | int32_t volatile cEventRefs;
|
---|
56 | /** -1 when uninitialized, 1 when initializing (busy) and 2 when done. */
|
---|
57 | int32_t volatile iState;
|
---|
58 | /** The return code of pfnOnce. */
|
---|
59 | int32_t volatile rc;
|
---|
60 | } RTONCE;
|
---|
61 | /** Pointer to a execute once struct. */
|
---|
62 | typedef RTONCE *PRTONCE;
|
---|
63 |
|
---|
64 | /** Static initializer for RTONCE variables. */
|
---|
65 | #define RTONCE_INITIALIZER { NIL_RTSEMEVENTMULTI, 0, -1, VERR_INTERNAL_ERROR }
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Callback that gets executed once.
|
---|
70 | *
|
---|
71 | * @returns IPRT style status code, RTOnce returns this.
|
---|
72 | *
|
---|
73 | * @param pvUser1 The first user parameter.
|
---|
74 | * @param pvUser2 The second user parameter.
|
---|
75 | */
|
---|
76 | typedef DECLCALLBACK(int32_t) FNRTONCE(void *pvUser1, void *pvUser2);
|
---|
77 | /** Pointer to a FNRTONCE. */
|
---|
78 | typedef FNRTONCE *PFNRTONCE;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Serializes execution of the pfnOnce function, making sure it's
|
---|
82 | * executed exactly once and that nobody returns from RTOnce before
|
---|
83 | * it has executed successfully.
|
---|
84 | *
|
---|
85 | * @returns IPRT like status code returned by pfnOnce.
|
---|
86 | *
|
---|
87 | * @param pOnce Pointer to the execute once variable.
|
---|
88 | * @param pfnOnce The function to executed once.
|
---|
89 | * @param pvUser1 The first user parameter for pfnOnce.
|
---|
90 | * @param pvUser2 The second user parameter for pfnOnce.
|
---|
91 | */
|
---|
92 | RTDECL(int) RTOnce(PRTONCE pOnce, PFNRTONCE pfnOnce, void *pvUser1, void *pvUser2);
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Resets an execute once variable.
|
---|
96 | *
|
---|
97 | * The caller is responsible for making sure there are no concurrent accesses to
|
---|
98 | * the execute once variable.
|
---|
99 | *
|
---|
100 | * @param pOnce Pointer to the execute once variable.
|
---|
101 | */
|
---|
102 | RTDECL(void) RTOnceReset(PRTONCE pOnce);
|
---|
103 |
|
---|
104 | /** @} */
|
---|
105 |
|
---|
106 | RT_C_DECLS_END
|
---|
107 |
|
---|
108 | #endif
|
---|
109 |
|
---|