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