| Index: mojo/public/utility/lib/thread_local.h
|
| diff --git a/mojo/public/utility/thread_local_posix.cc b/mojo/public/utility/lib/thread_local.h
|
| similarity index 21%
|
| copy from mojo/public/utility/thread_local_posix.cc
|
| copy to mojo/public/utility/lib/thread_local.h
|
| index d728e2c8925de4d4cf0ec47f2bc99a5201a36fc2..a663da3add389528305c89c81081fc0738a97f45 100644
|
| --- a/mojo/public/utility/thread_local_posix.cc
|
| +++ b/mojo/public/utility/lib/thread_local.h
|
| @@ -2,39 +2,60 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "mojo/public/utility/thread_local.h"
|
| +#ifndef MOJO_PUBLIC_UTILITY_LIB_THREAD_LOCAL_H_
|
| +#define MOJO_PUBLIC_UTILITY_LIB_THREAD_LOCAL_H_
|
|
|
| -#include <assert.h>
|
| +#ifndef _WIN32
|
| #include <pthread.h>
|
| +#endif
|
| +
|
| +#include "mojo/public/system/macros.h"
|
|
|
| namespace mojo {
|
| namespace internal {
|
|
|
| -// static
|
| -void ThreadLocalPlatform::AllocateSlot(SlotType* slot) {
|
| - if (pthread_key_create(slot, NULL) != 0) {
|
| - assert(false);
|
| +// Helper functions that abstract the cross-platform APIs.
|
| +struct ThreadLocalPlatform {
|
| +#ifdef _WIN32
|
| + typedef unsigned long SlotType;
|
| +#else
|
| + typedef pthread_key_t SlotType;
|
| +#endif
|
| +
|
| + static void AllocateSlot(SlotType* slot);
|
| + static void FreeSlot(SlotType slot);
|
| + static void* GetValueFromSlot(SlotType slot);
|
| + static void SetValueInSlot(SlotType slot, void* value);
|
| +};
|
| +
|
| +// This class is intended to be statically allocated.
|
| +template <typename P>
|
| +class ThreadLocalPointer {
|
| + public:
|
| + ThreadLocalPointer() : slot_() {
|
| + }
|
| +
|
| + void Allocate() {
|
| + ThreadLocalPlatform::AllocateSlot(&slot_);
|
| }
|
| -}
|
|
|
| -// static
|
| -void ThreadLocalPlatform::FreeSlot(SlotType slot) {
|
| - if (pthread_key_delete(slot) != 0) {
|
| - assert(false);
|
| + void Free() {
|
| + ThreadLocalPlatform::FreeSlot(slot_);
|
| }
|
| -}
|
|
|
| -// static
|
| -void* ThreadLocalPlatform::GetValueFromSlot(SlotType slot) {
|
| - return pthread_getspecific(slot);
|
| -}
|
| + P* Get() {
|
| + return static_cast<P*>(ThreadLocalPlatform::GetValueFromSlot(slot_));
|
| + }
|
|
|
| -// static
|
| -void ThreadLocalPlatform::SetValueInSlot(SlotType slot, void* value) {
|
| - if (pthread_setspecific(slot, value) != 0) {
|
| - assert(false);
|
| + void Set(P* value) {
|
| + ThreadLocalPlatform::SetValueInSlot(slot_, value);
|
| }
|
| -}
|
| +
|
| + private:
|
| + ThreadLocalPlatform::SlotType slot_;
|
| +};
|
|
|
| } // namespace internal
|
| } // namespace mojo
|
| +
|
| +#endif // MOJO_PUBLIC_UTILITY_LIB_THREAD_LOCAL_H_
|
|
|