Begin making folly compile cleanly with a few of MSVC's sign mismatch warnings enabled
[folly.git] / folly / detail / ThreadLocalDetail.cpp
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 #include <folly/ThreadLocal.h>
17
18 #include <list>
19 #include <mutex>
20
21 namespace folly { namespace threadlocal_detail {
22
23 StaticMetaBase::StaticMetaBase(ThreadEntry* (*threadEntry)(), bool strict)
24     : nextId_(1), threadEntry_(threadEntry), strict_(strict) {
25   head_.next = head_.prev = &head_;
26   int ret = pthread_key_create(&pthreadKey_, &onThreadExit);
27   checkPosixError(ret, "pthread_key_create failed");
28   PthreadKeyUnregister::registerKey(pthreadKey_);
29 }
30
31 void StaticMetaBase::onThreadExit(void* ptr) {
32 #ifdef FOLLY_TLD_USE_FOLLY_TLS
33   auto threadEntry = static_cast<ThreadEntry*>(ptr);
34 #else
35   std::unique_ptr<ThreadEntry> threadEntry(static_cast<ThreadEntry*>(ptr));
36 #endif
37   DCHECK_GT(threadEntry->elementsCapacity, 0u);
38   auto& meta = *threadEntry->meta;
39
40   // Make sure this ThreadEntry is available if ThreadLocal A is accessed in
41   // ThreadLocal B destructor.
42   pthread_setspecific(meta.pthreadKey_, &(*threadEntry));
43   SCOPE_EXIT {
44     pthread_setspecific(meta.pthreadKey_, nullptr);
45   };
46
47   {
48     SharedMutex::ReadHolder rlock;
49     if (meta.strict_) {
50       rlock = SharedMutex::ReadHolder(meta.accessAllThreadsLock_);
51     }
52     {
53       std::lock_guard<std::mutex> g(meta.lock_);
54       meta.erase(&(*threadEntry));
55       // No need to hold the lock any longer; the ThreadEntry is private to this
56       // thread now that it's been removed from meta.
57     }
58     // NOTE: User-provided deleter / object dtor itself may be using ThreadLocal
59     // with the same Tag, so dispose() calls below may (re)create some of the
60     // elements or even increase elementsCapacity, thus multiple cleanup rounds
61     // may be required.
62     for (bool shouldRun = true; shouldRun;) {
63       shouldRun = false;
64       FOR_EACH_RANGE (i, 0, threadEntry->elementsCapacity) {
65         if (threadEntry->elements[i].dispose(TLPDestructionMode::THIS_THREAD)) {
66           shouldRun = true;
67         }
68       }
69     }
70   }
71   free(threadEntry->elements);
72   threadEntry->elements = nullptr;
73   threadEntry->meta = nullptr;
74 }
75
76 uint32_t StaticMetaBase::allocate(EntryID* ent) {
77   uint32_t id;
78   auto& meta = *this;
79   std::lock_guard<std::mutex> g(meta.lock_);
80
81   id = ent->value.load();
82   if (id != kEntryIDInvalid) {
83     return id;
84   }
85
86   if (!meta.freeIds_.empty()) {
87     id = meta.freeIds_.back();
88     meta.freeIds_.pop_back();
89   } else {
90     id = meta.nextId_++;
91   }
92
93   uint32_t old_id = ent->value.exchange(id);
94   DCHECK_EQ(old_id, kEntryIDInvalid);
95   return id;
96 }
97
98 void StaticMetaBase::destroy(EntryID* ent) {
99   try {
100     auto& meta = *this;
101
102     // Elements in other threads that use this id.
103     std::vector<ElementWrapper> elements;
104
105     {
106       SharedMutex::WriteHolder wlock;
107       if (meta.strict_) {
108         /*
109          * In strict mode, the logic guarantees per-thread instances are
110          * destroyed by the moment ThreadLocal<> dtor returns.
111          * In order to achieve that, we should wait until concurrent
112          * onThreadExit() calls (that might acquire ownership over per-thread
113          * instances in order to destroy them) are finished.
114          */
115         wlock = SharedMutex::WriteHolder(meta.accessAllThreadsLock_);
116       }
117
118       {
119         std::lock_guard<std::mutex> g(meta.lock_);
120         uint32_t id = ent->value.exchange(kEntryIDInvalid);
121         if (id == kEntryIDInvalid) {
122           return;
123         }
124
125         for (ThreadEntry* e = meta.head_.next; e != &meta.head_; e = e->next) {
126           if (id < e->elementsCapacity && e->elements[id].ptr) {
127             elements.push_back(e->elements[id]);
128
129             /*
130              * Writing another thread's ThreadEntry from here is fine;
131              * the only other potential reader is the owning thread --
132              * from onThreadExit (which grabs the lock, so is properly
133              * synchronized with us) or from get(), which also grabs
134              * the lock if it needs to resize the elements vector.
135              *
136              * We can't conflict with reads for a get(id), because
137              * it's illegal to call get on a thread local that's
138              * destructing.
139              */
140             e->elements[id].ptr = nullptr;
141             e->elements[id].deleter1 = nullptr;
142             e->elements[id].ownsDeleter = false;
143           }
144         }
145         meta.freeIds_.push_back(id);
146       }
147     }
148     // Delete elements outside the locks.
149     for (ElementWrapper& elem : elements) {
150       elem.dispose(TLPDestructionMode::ALL_THREADS);
151     }
152   } catch (...) { // Just in case we get a lock error or something anyway...
153     LOG(WARNING) << "Destructor discarding an exception that was thrown.";
154   }
155 }
156
157 /**
158  * Reserve enough space in the ThreadEntry::elements for the item
159  * @id to fit in.
160  */
161 void StaticMetaBase::reserve(EntryID* id) {
162   auto& meta = *this;
163   ThreadEntry* threadEntry = (*threadEntry_)();
164   size_t prevCapacity = threadEntry->elementsCapacity;
165
166   uint32_t idval = id->getOrAllocate(meta);
167   if (prevCapacity > idval) {
168     return;
169   }
170   // Growth factor < 2, see folly/docs/FBVector.md; + 5 to prevent
171   // very slow start.
172   size_t newCapacity = static_cast<size_t>((idval + 5) * 1.7);
173   assert(newCapacity > prevCapacity);
174   ElementWrapper* reallocated = nullptr;
175
176   // Need to grow. Note that we can't call realloc, as elements is
177   // still linked in meta, so another thread might access invalid memory
178   // after realloc succeeds. We'll copy by hand and update our ThreadEntry
179   // under the lock.
180   if (usingJEMalloc()) {
181     bool success = false;
182     size_t newByteSize = nallocx(newCapacity * sizeof(ElementWrapper), 0);
183
184     // Try to grow in place.
185     //
186     // Note that xallocx(MALLOCX_ZERO) will only zero newly allocated memory,
187     // even if a previous allocation allocated more than we requested.
188     // This is fine; we always use MALLOCX_ZERO with jemalloc and we
189     // always expand our allocation to the real size.
190     if (prevCapacity * sizeof(ElementWrapper) >= jemallocMinInPlaceExpandable) {
191       success =
192           (xallocx(threadEntry->elements, newByteSize, 0, MALLOCX_ZERO) ==
193            newByteSize);
194     }
195
196     // In-place growth failed.
197     if (!success) {
198       success =
199           ((reallocated = static_cast<ElementWrapper*>(
200                 mallocx(newByteSize, MALLOCX_ZERO))) != nullptr);
201     }
202
203     if (success) {
204       // Expand to real size
205       assert(newByteSize / sizeof(ElementWrapper) >= newCapacity);
206       newCapacity = newByteSize / sizeof(ElementWrapper);
207     } else {
208       throw std::bad_alloc();
209     }
210   } else { // no jemalloc
211     // calloc() is simpler than malloc() followed by memset(), and
212     // potentially faster when dealing with a lot of memory, as it can get
213     // already-zeroed pages from the kernel.
214     reallocated = static_cast<ElementWrapper*>(
215         calloc(newCapacity, sizeof(ElementWrapper)));
216     if (!reallocated) {
217       throw std::bad_alloc();
218     }
219   }
220
221   // Success, update the entry
222   {
223     std::lock_guard<std::mutex> g(meta.lock_);
224
225     if (prevCapacity == 0) {
226       meta.push_back(threadEntry);
227     }
228
229     if (reallocated) {
230       /*
231        * Note: we need to hold the meta lock when copying data out of
232        * the old vector, because some other thread might be
233        * destructing a ThreadLocal and writing to the elements vector
234        * of this thread.
235        */
236       if (prevCapacity != 0) {
237         memcpy(
238             reallocated,
239             threadEntry->elements,
240             sizeof(*reallocated) * prevCapacity);
241       }
242       std::swap(reallocated, threadEntry->elements);
243     }
244     threadEntry->elementsCapacity = newCapacity;
245   }
246
247   free(reallocated);
248 }
249
250 namespace {
251
252 struct AtForkTask {
253   folly::Function<void()> prepare;
254   folly::Function<void()> parent;
255   folly::Function<void()> child;
256 };
257
258 class AtForkList {
259  public:
260   static AtForkList& instance() {
261     static auto instance = new AtForkList();
262     return *instance;
263   }
264
265   static void prepare() noexcept {
266     instance().tasksLock.lock();
267     auto& tasks = instance().tasks;
268     for (auto task = tasks.rbegin(); task != tasks.rend(); ++task) {
269       task->prepare();
270     }
271   }
272
273   static void parent() noexcept {
274     auto& tasks = instance().tasks;
275     for (auto& task : tasks) {
276       task.parent();
277     }
278     instance().tasksLock.unlock();
279   }
280
281   static void child() noexcept {
282     auto& tasks = instance().tasks;
283     for (auto& task : tasks) {
284       task.child();
285     }
286     instance().tasksLock.unlock();
287   }
288
289   std::mutex tasksLock;
290   std::list<AtForkTask> tasks;
291
292  private:
293   AtForkList() {
294 #if FOLLY_HAVE_PTHREAD_ATFORK
295     int ret = pthread_atfork(
296         &AtForkList::prepare, &AtForkList::parent, &AtForkList::child);
297     checkPosixError(ret, "pthread_atfork failed");
298 #elif !__ANDROID__ && !defined(_MSC_VER)
299 // pthread_atfork is not part of the Android NDK at least as of n9d. If
300 // something is trying to call native fork() directly at all with Android's
301 // process management model, this is probably the least of the problems.
302 //
303 // But otherwise, this is a problem.
304 #warning pthread_atfork unavailable
305 #endif
306   }
307 };
308 }
309
310 void StaticMetaBase::initAtFork() {
311   AtForkList::instance();
312 }
313
314 void StaticMetaBase::registerAtFork(
315     folly::Function<void()> prepare,
316     folly::Function<void()> parent,
317     folly::Function<void()> child) {
318   std::lock_guard<std::mutex> lg(AtForkList::instance().tasksLock);
319   AtForkList::instance().tasks.push_back(
320       {std::move(prepare), std::move(parent), std::move(child)});
321 }
322
323 FOLLY_STATIC_CTOR_PRIORITY_MAX
324 PthreadKeyUnregister PthreadKeyUnregister::instance_;
325 }}