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

Remove pthread based thread local support. #49297

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions fml/thread_local.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,3 @@
// found in the LICENSE file.

#include "flutter/fml/thread_local.h"

#if FML_THREAD_LOCAL_PTHREADS

#include <cstring>

#include "flutter/fml/logging.h"

namespace fml {
namespace internal {

ThreadLocalPointer::ThreadLocalPointer(void (*destroy)(void*)) {
FML_CHECK(pthread_key_create(&key_, destroy) == 0);
}

ThreadLocalPointer::~ThreadLocalPointer() {
FML_CHECK(pthread_key_delete(key_) == 0);
}

void* ThreadLocalPointer::get() const {
return pthread_getspecific(key_);
}

void* ThreadLocalPointer::swap(void* ptr) {
void* old_ptr = get();
int err = pthread_setspecific(key_, ptr);
if (err) {
FML_CHECK(false) << "pthread_setspecific failed (" << err
<< "): " << strerror(err);
}
return old_ptr;
}

} // namespace internal
} // namespace fml

#endif // FML_THREAD_LOCAL_PTHREADS
64 changes: 9 additions & 55 deletions fml/thread_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,25 @@
#include "flutter/fml/build_config.h"
#include "flutter/fml/macros.h"

#define FML_THREAD_LOCAL_PTHREADS \
FML_OS_MACOSX || FML_OS_LINUX || FML_OS_ANDROID

#if FML_THREAD_LOCAL_PTHREADS
#include <pthread.h>
#endif

namespace fml {

#if FML_THREAD_LOCAL_PTHREADS

#define FML_THREAD_LOCAL static

namespace internal {

class ThreadLocalPointer {
public:
explicit ThreadLocalPointer(void (*destroy)(void*));
~ThreadLocalPointer();

void* get() const;
void* swap(void* ptr);

private:
pthread_key_t key_;

FML_DISALLOW_COPY_AND_ASSIGN(ThreadLocalPointer);
};

} // namespace internal

template <typename T>
class ThreadLocalUniquePtr {
public:
ThreadLocalUniquePtr() : ptr_(destroy) {}

T* get() const { return reinterpret_cast<T*>(ptr_.get()); }
void reset(T* ptr) { destroy(ptr_.swap(ptr)); }

private:
static void destroy(void* ptr) { delete reinterpret_cast<T*>(ptr); }

internal::ThreadLocalPointer ptr_;

FML_DISALLOW_COPY_AND_ASSIGN(ThreadLocalUniquePtr);
};

#else // FML_THREAD_LOCAL_PTHREADS

#define FML_THREAD_LOCAL static thread_local

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

T* get() const { return ptr_.get(); }

void reset(T* ptr) { ptr_.reset(ptr); }

private:
Expand All @@ -75,14 +37,6 @@ class ThreadLocalUniquePtr {
FML_DISALLOW_COPY_AND_ASSIGN(ThreadLocalUniquePtr);
};

#endif // FML_THREAD_LOCAL_PTHREADS

#ifndef FML_THREAD_LOCAL

#error Thread local storage unavailable on the platform.

#endif // FML_THREAD_LOCAL

} // namespace fml

#endif // FLUTTER_FML_THREAD_LOCAL_H_