Track setting access count
[hiphop-php.git] / hphp / util / thread-local.cpp
blob7b6415a7ad452d58e747f2efec6e093954a85433
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/thread-local.h"
19 #if defined(__linux__) && defined(__x86_64__)
20 #define HAVE_ARCH_PRCTL 1
21 #endif
23 #ifdef HAVE_ARCH_PRCTL
24 #include <link.h>
25 #include <asm/prctl.h>
26 #include <sys/prctl.h>
27 extern "C" {
28 extern int arch_prctl(int, unsigned long*);
30 #endif //HAVE_ARCH_PRCTL
32 namespace HPHP {
34 #ifdef USE_GCC_FAST_TLS
36 void ThreadLocalManager::OnThreadExit(void* p) {
37 auto list = getList(p);
38 p = list->head;
39 list->~ThreadLocalList();
40 local_free(list);
41 while (p != nullptr) {
42 auto* pNode = static_cast<ThreadLocalNode<void>*>(p);
43 if (pNode->m_on_thread_exit_fn) {
44 pNode->m_on_thread_exit_fn(p);
46 p = pNode->m_next;
50 void ThreadLocalManager::initTypeIndices() {
51 auto list = getList(pthread_getspecific(m_key));
52 if (!list) return;
53 for (auto p = list->head; p != nullptr;) {
54 auto node = static_cast<ThreadLocalNode<void>*>(p);
55 node->m_init_tyindex(node);
56 p = node->m_next;
60 void ThreadLocalManager::PushTop(void* nodePtr, uint32_t nodeSize,
61 type_scan::Index tyindex) {
62 auto& node = *static_cast<ThreadLocalNode<void>*>(nodePtr);
63 auto key = GetManager().m_key;
64 auto list = getList(pthread_getspecific(key));
65 if (UNLIKELY(!list)) {
66 list = new (local_malloc(sizeof(ThreadLocalList))) ThreadLocalList;
67 ThreadLocalSetValue(key, list);
69 node.m_next = list->head;
70 node.m_size = nodeSize;
71 node.m_tyindex = tyindex;
72 list->head = &node;
75 ThreadLocalManager& ThreadLocalManager::GetManager() {
76 static ThreadLocalManager m;
77 return m;
80 #endif
82 #ifdef HAVE_ARCH_PRCTL
84 static int visit_phdr(dl_phdr_info* info, size_t, void*) {
85 for (size_t i = 0, n = info->dlpi_phnum; i < n; ++i) {
86 const auto& hdr = info->dlpi_phdr[i];
87 auto addr = info->dlpi_addr + hdr.p_vaddr;
88 if (addr < 0x100000000LL && hdr.p_type == PT_TLS) {
89 // found the main thread-local section
90 assert(int(hdr.p_memsz) == hdr.p_memsz); // ensure no truncation
91 return hdr.p_memsz;
94 return 0;
97 std::pair<void*,size_t> getCppTdata() {
98 uintptr_t addr;
99 if (!arch_prctl(ARCH_GET_FS, &addr)) {
100 // fs points to the end of the threadlocal area.
101 size_t size = dl_iterate_phdr(&visit_phdr, nullptr);
102 return {(void*)(addr - size), size};
104 return {nullptr, 0};
107 #else
109 // how do you find the thread local section on your system?
110 std::pair<void*,size_t> getCppTdata() {
111 return {nullptr, 0};
114 #endif //HAVE_ARCH_PRCTL