2 +----------------------------------------------------------------------+
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
23 #ifdef HAVE_ARCH_PRCTL
25 #include <asm/prctl.h>
26 #include <sys/prctl.h>
28 extern int arch_prctl(int, unsigned long*);
30 #endif //HAVE_ARCH_PRCTL
34 #ifdef USE_GCC_FAST_TLS
36 void ThreadLocalManager::OnThreadExit(void* p
) {
37 auto list
= getList(p
);
39 list
->~ThreadLocalList();
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
);
50 void ThreadLocalManager::initTypeIndices() {
51 auto list
= getList(pthread_getspecific(m_key
));
53 for (auto p
= list
->head
; p
!= nullptr;) {
54 auto node
= static_cast<ThreadLocalNode
<void>*>(p
);
55 node
->m_init_tyindex(node
);
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
;
75 ThreadLocalManager
& ThreadLocalManager::GetManager() {
76 static ThreadLocalManager m
;
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
97 std::pair
<void*,size_t> getCppTdata() {
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
};
109 // how do you find the thread local section on your system?
110 std::pair
<void*,size_t> getCppTdata() {
114 #endif //HAVE_ARCH_PRCTL