1*4882a593SmuzhiyunFrom d46b92ecdae0c7dd2f284e3333641866aab4f604 Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: Martin Jansa <Martin.Jansa@gmail.com>
3*4882a593SmuzhiyunDate: Tue, 26 Jan 2021 08:50:45 +0100
4*4882a593SmuzhiyunSubject: [PATCH] Revert "Fix workaround in pthread destructor"
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunThis reverts commit 81ce2d1d6fa741de4d27b939a378147a02019ec1.
7*4882a593Smuzhiyun
8*4882a593SmuzhiyuncurrentThreadData was reverted in 5.12 before this commit:
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun81ce2d1d6f Fix workaround in pthread destructor
11*4882a593Smuzhiyun8867e0eaa7 Revert "Remove pthread storage for thread local data"
12*4882a593Smuzhiyun78665d8a0c Remove pthread storage for thread local data
13*4882a593Smuzhiyun
14*4882a593Smuzhiyuncausing build failures in configurations which use this
15*4882a593Smuzhiyun| /home/jenkins/workspace/luneos-unstable/webos-ports/tmp-glibc/work/cortexa8t2hf-neon-halium-webos-linux-gnueabi/qtbase/5.15.2+gitAUTOINC+40143c189b-r0/git/src/corelib/thread/qthread_unix.cpp: In function 'void destroy_current_thread_data(void*)':
16*4882a593Smuzhiyun| /home/jenkins/workspace/luneos-unstable/webos-ports/tmp-glibc/work/cortexa8t2hf-neon-halium-webos-linux-gnueabi/qtbase/5.15.2+gitAUTOINC+40143c189b-r0/git/src/corelib/thread/qthread_unix.cpp:121:5: error: 'currentThreadData' was not declared in this scope
17*4882a593Smuzhiyun|   121 |     currentThreadData = data;
18*4882a593Smuzhiyun|       |     ^~~~~~~~~~~~~~~~~
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunUpstream-Status: Pending
21*4882a593SmuzhiyunSigned-off-by: Martin Jansa <Martin.Jansa@gmail.com>
22*4882a593Smuzhiyun---
23*4882a593Smuzhiyun src/corelib/thread/qthread_unix.cpp | 25 +++++++++++++++++++------
24*4882a593Smuzhiyun 1 file changed, 19 insertions(+), 6 deletions(-)
25*4882a593Smuzhiyun
26*4882a593Smuzhiyundiff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp
27*4882a593Smuzhiyunindex 659d5fb03c..1da68b3130 100644
28*4882a593Smuzhiyun--- a/src/corelib/thread/qthread_unix.cpp
29*4882a593Smuzhiyun+++ b/src/corelib/thread/qthread_unix.cpp
30*4882a593Smuzhiyun@@ -116,11 +116,18 @@ static pthread_key_t current_thread_data_key;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun static void destroy_current_thread_data(void *p)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun+#if defined(Q_OS_VXWORKS)
35*4882a593Smuzhiyun+    // Calling setspecific(..., 0) sets the value to 0 for ALL threads.
36*4882a593Smuzhiyun+    // The 'set to 1' workaround adds a bit of an overhead though,
37*4882a593Smuzhiyun+    // since this function is called twice now.
38*4882a593Smuzhiyun+    if (p == (void *)1)
39*4882a593Smuzhiyun+        return;
40*4882a593Smuzhiyun+#endif
41*4882a593Smuzhiyun+    // POSIX says the value in our key is set to zero before calling
42*4882a593Smuzhiyun+    // this destructor function, so we need to set it back to the
43*4882a593Smuzhiyun+    // right value...
44*4882a593Smuzhiyun+    pthread_setspecific(current_thread_data_key, p);
45*4882a593Smuzhiyun     QThreadData *data = static_cast<QThreadData *>(p);
46*4882a593Smuzhiyun-    // thread_local variables are set to zero before calling this destructor function,
47*4882a593Smuzhiyun-    // if they are internally using pthread-specific data management,
48*4882a593Smuzhiyun-    // so we need to set it back to the right value...
49*4882a593Smuzhiyun-    currentThreadData = data;
50*4882a593Smuzhiyun     if (data->isAdopted) {
51*4882a593Smuzhiyun         QThread *thread = data->thread.loadAcquire();
52*4882a593Smuzhiyun         Q_ASSERT(thread);
53*4882a593Smuzhiyun@@ -131,8 +138,14 @@ static void destroy_current_thread_data(void *p)
54*4882a593Smuzhiyun     data->deref();
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun     // ... but we must reset it to zero before returning so we aren't
57*4882a593Smuzhiyun-    // leaving a dangling pointer.
58*4882a593Smuzhiyun-    currentThreadData = nullptr;
59*4882a593Smuzhiyun+    // called again (POSIX allows implementations to call destructor
60*4882a593Smuzhiyun+    // functions repeatedly until all values are zero)
61*4882a593Smuzhiyun+    pthread_setspecific(current_thread_data_key,
62*4882a593Smuzhiyun+#if defined(Q_OS_VXWORKS)
63*4882a593Smuzhiyun+                                                 (void *)1);
64*4882a593Smuzhiyun+#else
65*4882a593Smuzhiyun+                                                 nullptr);
66*4882a593Smuzhiyun+#endif
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun static void create_current_thread_data_key()
70