2 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #ifndef __DISPATCH_ONCE__
22 #define __DISPATCH_ONCE__
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
29 DISPATCH_ASSUME_NONNULL_BEGIN
34 * @typedef dispatch_once_t
37 * A predicate for use with dispatch_once(). It must be initialized to zero.
38 * Note: static and global variables default to zero.
40 DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
41 typedef long dispatch_once_t
;
43 #if defined(__x86_64__) || defined(__i386__) || defined(__s390x__)
44 #define DISPATCH_ONCE_INLINE_FASTPATH 1
45 #elif defined(__APPLE__)
46 #define DISPATCH_ONCE_INLINE_FASTPATH 1
48 #define DISPATCH_ONCE_INLINE_FASTPATH 0
52 * @function dispatch_once
55 * Execute a block once and only once.
58 * A pointer to a dispatch_once_t that is used to test whether the block has
62 * The block to execute once.
65 * Always call dispatch_once() before using or testing any variables that are
66 * initialized by the block.
69 API_AVAILABLE(macos(10.6), ios(4.0))
70 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
71 DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
73 dispatch_once(dispatch_once_t
*predicate
,
74 DISPATCH_NOESCAPE dispatch_block_t block
);
76 #if DISPATCH_ONCE_INLINE_FASTPATH
77 DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
78 DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
80 _dispatch_once(dispatch_once_t
*predicate
,
81 DISPATCH_NOESCAPE dispatch_block_t block
)
83 if (DISPATCH_EXPECT(*predicate
, ~0l) != ~0l) {
84 dispatch_once(predicate
, block
);
86 dispatch_compiler_barrier();
88 DISPATCH_COMPILER_CAN_ASSUME(*predicate
== ~0l);
91 #define dispatch_once _dispatch_once
93 #endif // DISPATCH_ONCE_INLINE_FASTPATH
95 API_AVAILABLE(macos(10.6), ios(4.0))
96 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
97 DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
99 dispatch_once_f(dispatch_once_t
*predicate
, void *_Nullable context
,
100 dispatch_function_t function
);
102 #if DISPATCH_ONCE_INLINE_FASTPATH
103 DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL1 DISPATCH_NONNULL3
105 DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
107 _dispatch_once_f(dispatch_once_t
*predicate
, void *_Nullable context
,
108 dispatch_function_t function
)
110 if (DISPATCH_EXPECT(*predicate
, ~0l) != ~0l) {
111 dispatch_once_f(predicate
, context
, function
);
113 dispatch_compiler_barrier();
115 DISPATCH_COMPILER_CAN_ASSUME(*predicate
== ~0l);
117 #undef dispatch_once_f
118 #define dispatch_once_f _dispatch_once_f
119 #endif // DISPATCH_ONCE_INLINE_FASTPATH
123 DISPATCH_ASSUME_NONNULL_END