]> git.saurik.com Git - apple/libdispatch.git/blame - dispatch/once.h
libdispatch-913.30.4.tar.gz
[apple/libdispatch.git] / dispatch / once.h
CommitLineData
0ab74447 1/*
e85f4437 2 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
0ab74447
A
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
e85f4437 5 *
0ab74447
A
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
e85f4437 9 *
0ab74447 10 * http://www.apache.org/licenses/LICENSE-2.0
e85f4437 11 *
0ab74447
A
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.
e85f4437 17 *
0ab74447
A
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21#ifndef __DISPATCH_ONCE__
22#define __DISPATCH_ONCE__
23
24#ifndef __DISPATCH_INDIRECT__
25#error "Please #include <dispatch/dispatch.h> instead of this file directly."
26#include <dispatch/base.h> // for HeaderDoc
27#endif
28
beb15981
A
29DISPATCH_ASSUME_NONNULL_BEGIN
30
0ab74447
A
31__BEGIN_DECLS
32
33/*!
34 * @typedef dispatch_once_t
35 *
36 * @abstract
37 * A predicate for use with dispatch_once(). It must be initialized to zero.
38 * Note: static and global variables default to zero.
39 */
beb15981 40DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
0ab74447
A
41typedef long dispatch_once_t;
42
6b746eb4
A
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
47#else
48#define DISPATCH_ONCE_INLINE_FASTPATH 0
49#endif
50
0ab74447
A
51/*!
52 * @function dispatch_once
53 *
54 * @abstract
55 * Execute a block once and only once.
56 *
57 * @param predicate
58 * A pointer to a dispatch_once_t that is used to test whether the block has
59 * completed or not.
60 *
61 * @param block
62 * The block to execute once.
63 *
64 * @discussion
65 * Always call dispatch_once() before using or testing any variables that are
66 * initialized by the block.
67 */
68#ifdef __BLOCKS__
6b746eb4 69API_AVAILABLE(macos(10.6), ios(4.0))
e85f4437 70DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
beb15981 71DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
0ab74447 72void
beb15981
A
73dispatch_once(dispatch_once_t *predicate,
74 DISPATCH_NOESCAPE dispatch_block_t block);
0ab74447 75
6b746eb4 76#if DISPATCH_ONCE_INLINE_FASTPATH
e85f4437 77DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
beb15981 78DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
0ab74447 79void
beb15981
A
80_dispatch_once(dispatch_once_t *predicate,
81 DISPATCH_NOESCAPE dispatch_block_t block)
e85f4437
A
82{
83 if (DISPATCH_EXPECT(*predicate, ~0l) != ~0l) {
84 dispatch_once(predicate, block);
beb15981
A
85 } else {
86 dispatch_compiler_barrier();
e85f4437 87 }
beb15981 88 DISPATCH_COMPILER_CAN_ASSUME(*predicate == ~0l);
e85f4437
A
89}
90#undef dispatch_once
91#define dispatch_once _dispatch_once
0ab74447 92#endif
6b746eb4 93#endif // DISPATCH_ONCE_INLINE_FASTPATH
0ab74447 94
6b746eb4 95API_AVAILABLE(macos(10.6), ios(4.0))
e85f4437 96DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
beb15981 97DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
e85f4437 98void
beb15981 99dispatch_once_f(dispatch_once_t *predicate, void *_Nullable context,
e85f4437
A
100 dispatch_function_t function);
101
6b746eb4 102#if DISPATCH_ONCE_INLINE_FASTPATH
e85f4437
A
103DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL1 DISPATCH_NONNULL3
104DISPATCH_NOTHROW
beb15981 105DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
e85f4437 106void
beb15981 107_dispatch_once_f(dispatch_once_t *predicate, void *_Nullable context,
e85f4437
A
108 dispatch_function_t function)
109{
110 if (DISPATCH_EXPECT(*predicate, ~0l) != ~0l) {
111 dispatch_once_f(predicate, context, function);
beb15981
A
112 } else {
113 dispatch_compiler_barrier();
e85f4437 114 }
beb15981 115 DISPATCH_COMPILER_CAN_ASSUME(*predicate == ~0l);
e85f4437
A
116}
117#undef dispatch_once_f
118#define dispatch_once_f _dispatch_once_f
6b746eb4 119#endif // DISPATCH_ONCE_INLINE_FASTPATH
e85f4437 120
0ab74447
A
121__END_DECLS
122
beb15981
A
123DISPATCH_ASSUME_NONNULL_END
124
0ab74447 125#endif