| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #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: key_); |
| 26 | } |
| 27 | |
| 28 | void* ThreadLocalPointer::swap(void* ptr) { |
| 29 | void* old_ptr = get(); |
| 30 | int err = pthread_setspecific(key: key_, pointer: ptr); |
| 31 | if (err) { |
| 32 | FML_CHECK(false) << "pthread_setspecific failed ("<< err |
| 33 | << "): "<< strerror(errnum: err); |
| 34 | } |
| 35 | return old_ptr; |
| 36 | } |
| 37 | |
| 38 | } // namespace internal |
| 39 | } // namespace fml |
| 40 | |
| 41 | #endif // FML_THREAD_LOCAL_PTHREADS |
| 42 |
