Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c673abe

Browse files
committed
Remove pthread based thread local support.
The thread_local storage class was not available pre-C++11. Even when C++11 was available, the C++ runtime in versions of iOS <= 9.0 was not capable of supporting this storage class. So we ended up using pthread directly in that case. The unique pointer support was added later. Now that the storage class has been supported on all Flutter platforms for a while, we can remove the fallback and remove a bunch of code. The ThreadLocalUniquePtr can be removed too but that can be attempted in a separate migration.
1 parent ee1ded6 commit c673abe

File tree

2 files changed

+9
-91
lines changed

2 files changed

+9
-91
lines changed

fml/thread_local.cc

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,3 @@
33
// found in the LICENSE file.
44

55
#include "flutter/fml/thread_local.h"
6-
7-
#if FML_THREAD_LOCAL_PTHREADS
8-
9-
#include <cstring>
10-
11-
#include "flutter/fml/logging.h"
12-
13-
namespace fml {
14-
namespace internal {
15-
16-
ThreadLocalPointer::ThreadLocalPointer(void (*destroy)(void*)) {
17-
FML_CHECK(pthread_key_create(&key_, destroy) == 0);
18-
}
19-
20-
ThreadLocalPointer::~ThreadLocalPointer() {
21-
FML_CHECK(pthread_key_delete(key_) == 0);
22-
}
23-
24-
void* ThreadLocalPointer::get() const {
25-
return pthread_getspecific(key_);
26-
}
27-
28-
void* ThreadLocalPointer::swap(void* ptr) {
29-
void* old_ptr = get();
30-
int err = pthread_setspecific(key_, ptr);
31-
if (err) {
32-
FML_CHECK(false) << "pthread_setspecific failed (" << err
33-
<< "): " << strerror(err);
34-
}
35-
return old_ptr;
36-
}
37-
38-
} // namespace internal
39-
} // namespace fml
40-
41-
#endif // FML_THREAD_LOCAL_PTHREADS

fml/thread_local.h

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,25 @@
1010
#include "flutter/fml/build_config.h"
1111
#include "flutter/fml/macros.h"
1212

13-
#define FML_THREAD_LOCAL_PTHREADS \
14-
FML_OS_MACOSX || FML_OS_LINUX || FML_OS_ANDROID
15-
16-
#if FML_THREAD_LOCAL_PTHREADS
17-
#include <pthread.h>
18-
#endif
19-
2013
namespace fml {
2114

22-
#if FML_THREAD_LOCAL_PTHREADS
23-
24-
#define FML_THREAD_LOCAL static
25-
26-
namespace internal {
27-
28-
class ThreadLocalPointer {
29-
public:
30-
explicit ThreadLocalPointer(void (*destroy)(void*));
31-
~ThreadLocalPointer();
32-
33-
void* get() const;
34-
void* swap(void* ptr);
35-
36-
private:
37-
pthread_key_t key_;
38-
39-
FML_DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer);
40-
};
41-
42-
} // namespace internal
43-
44-
template <typename T>
45-
class ThreadLocalUniquePtr {
46-
public:
47-
ThreadLocalUniquePtr() : ptr_(destroy) {}
48-
49-
T* get() const { return reinterpret_cast<T*>(ptr_.get()); }
50-
void reset(T* ptr) { destroy(ptr_.swap(ptr)); }
51-
52-
private:
53-
static void destroy(void* ptr) { delete reinterpret_cast<T*>(ptr); }
54-
55-
internal::ThreadLocalPointer ptr_;
56-
57-
FML_DISALLOW_COPY_AND_ASSIGN(ThreadLocalUniquePtr);
58-
};
59-
60-
#else // FML_THREAD_LOCAL_PTHREADS
61-
6215
#define FML_THREAD_LOCAL static thread_local
6316

17+
//------------------------------------------------------------------------------
18+
/// @brief A wrapper for a thread-local unique pointer. Do NOT use this
19+
/// class in new code and instead use unique pointers with the
20+
/// thread_local storage class directly. This was necessary for
21+
/// pre-C++11 code.
22+
///
23+
/// @tparam T The type held in the thread local unique pointer.
24+
///
6425
template <typename T>
6526
class ThreadLocalUniquePtr {
6627
public:
6728
ThreadLocalUniquePtr() = default;
6829

6930
T* get() const { return ptr_.get(); }
31+
7032
void reset(T* ptr) { ptr_.reset(ptr); }
7133

7234
private:
@@ -77,12 +39,4 @@ class ThreadLocalUniquePtr {
7739

7840
#endif // FML_THREAD_LOCAL_PTHREADS
7941

80-
#ifndef FML_THREAD_LOCAL
81-
82-
#error Thread local storage unavailable on the platform.
83-
84-
#endif // FML_THREAD_LOCAL
85-
8642
} // namespace fml
87-
88-
#endif // FLUTTER_FML_THREAD_LOCAL_H_

0 commit comments

Comments
 (0)