Fix fibers gdb utils script
[folly.git] / folly / CallOnce.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Drop-in replacement for std::call_once() with a fast path, which the GCC
19  * implementation lacks.  The tradeoff is a slightly larger `once_flag' struct
20  * (8 bytes vs 4 bytes with GCC on Linux/x64).
21  *
22  * $ call_once_test --benchmark --bm_min_iters=100000000 --threads=16
23  * ============================================================================
24  * folly/test/CallOnceTest.cpp                     relative  time/iter  iters/s
25  * ============================================================================
26  * StdCallOnceBench                                             3.54ns  282.82M
27  * FollyCallOnceBench                                         698.48ps    1.43G
28  * ============================================================================
29  */
30
31 #pragma once
32
33 #include <atomic>
34 #include <mutex>
35 #include <utility>
36
37 #include <folly/Likely.h>
38 #include <folly/Portability.h>
39
40 namespace folly {
41
42 class once_flag {
43  public:
44   constexpr once_flag() noexcept = default;
45   once_flag(const once_flag&) = delete;
46   once_flag& operator=(const once_flag&) = delete;
47
48   template <typename Callable, class... Args>
49   friend void call_once(once_flag& flag, Callable&& f, Args&&... args);
50   template <typename Callable, class... Args>
51   friend void call_once_impl_no_inline(once_flag& flag,
52                                        Callable&& f,
53                                        Args&&... args);
54
55  private:
56   std::atomic<bool> called_{false};
57   std::once_flag std_once_flag_;
58 };
59
60 template <class Callable, class... Args>
61 void FOLLY_ALWAYS_INLINE
62 call_once(once_flag& flag, Callable&& f, Args&&... args) {
63   if (LIKELY(flag.called_.load(std::memory_order_acquire))) {
64     return;
65   }
66   call_once_impl_no_inline(
67       flag, std::forward<Callable>(f), std::forward<Args>(args)...);
68 }
69
70 // Implementation detail: out-of-line slow path
71 template <class Callable, class... Args>
72 void FOLLY_NOINLINE
73 call_once_impl_no_inline(once_flag& flag, Callable&& f, Args&&... args) {
74   std::call_once(flag.std_once_flag_,
75                  std::forward<Callable>(f),
76                  std::forward<Args>(args)...);
77   flag.called_.store(true, std::memory_order_release);
78 }
79 }