Fix some copyright lines in folly/detail/ and folly/test/
[folly.git] / folly / detail / AtFork.cpp
1 /*
2  * Copyright 2017-present 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 #include <folly/detail/AtFork.h>
17
18 #include <list>
19 #include <mutex>
20
21 #include <folly/Exception.h>
22
23 namespace folly {
24
25 namespace detail {
26
27 namespace {
28
29 struct AtForkTask {
30   void* object;
31   folly::Function<void()> prepare;
32   folly::Function<void()> parent;
33   folly::Function<void()> child;
34 };
35
36 class AtForkList {
37  public:
38   static AtForkList& instance() {
39     static auto instance = new AtForkList();
40     return *instance;
41   }
42
43   static void prepare() noexcept {
44     instance().tasksLock.lock();
45     auto& tasks = instance().tasks;
46     for (auto task = tasks.rbegin(); task != tasks.rend(); ++task) {
47       task->prepare();
48     }
49   }
50
51   static void parent() noexcept {
52     auto& tasks = instance().tasks;
53     for (auto& task : tasks) {
54       task.parent();
55     }
56     instance().tasksLock.unlock();
57   }
58
59   static void child() noexcept {
60     auto& tasks = instance().tasks;
61     for (auto& task : tasks) {
62       task.child();
63     }
64     instance().tasksLock.unlock();
65   }
66
67   std::mutex tasksLock;
68   std::list<AtForkTask> tasks;
69
70  private:
71   AtForkList() {
72 #if FOLLY_HAVE_PTHREAD_ATFORK
73     int ret = pthread_atfork(
74         &AtForkList::prepare, &AtForkList::parent, &AtForkList::child);
75     checkPosixError(ret, "pthread_atfork failed");
76 #elif !__ANDROID__ && !defined(_MSC_VER)
77 // pthread_atfork is not part of the Android NDK at least as of n9d. If
78 // something is trying to call native fork() directly at all with Android's
79 // process management model, this is probably the least of the problems.
80 //
81 // But otherwise, this is a problem.
82 #warning pthread_atfork unavailable
83 #endif
84   }
85 };
86 } // namespace
87
88 void AtFork::init() {
89   AtForkList::instance();
90 }
91
92 void AtFork::registerHandler(
93     void* object,
94     folly::Function<void()> prepare,
95     folly::Function<void()> parent,
96     folly::Function<void()> child) {
97   std::lock_guard<std::mutex> lg(AtForkList::instance().tasksLock);
98   AtForkList::instance().tasks.push_back(
99       {object, std::move(prepare), std::move(parent), std::move(child)});
100 }
101
102 void AtFork::unregisterHandler(void* object) {
103   auto& list = AtForkList::instance();
104   std::lock_guard<std::mutex> lg(list.tasksLock);
105   for (auto it = list.tasks.begin(); it != list.tasks.end(); ++it) {
106     if (it->object == object) {
107       list.tasks.erase(it);
108       return;
109     }
110   }
111 }
112
113 } // namespace detail
114 } // namespace folly