1 // Copyright 2014 The Chromium 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.
5 #ifndef MOJO_PUBLIC_UTILITY_LIB_THREAD_LOCAL_H_
6 #define MOJO_PUBLIC_UTILITY_LIB_THREAD_LOCAL_H_
12 #include "mojo/public/cpp/system/macros.h"
17 // Helper functions that abstract the cross-platform APIs.
18 struct ThreadLocalPlatform {
20 typedef unsigned long SlotType;
22 typedef pthread_key_t SlotType;
25 static void AllocateSlot(SlotType* slot);
26 static void FreeSlot(SlotType slot);
27 static void* GetValueFromSlot(SlotType slot);
28 static void SetValueInSlot(SlotType slot, void* value);
31 // This class is intended to be statically allocated.
33 class ThreadLocalPointer {
35 ThreadLocalPointer() : slot_() {
39 ThreadLocalPlatform::AllocateSlot(&slot_);
43 ThreadLocalPlatform::FreeSlot(slot_);
47 return static_cast<P*>(ThreadLocalPlatform::GetValueFromSlot(slot_));
51 ThreadLocalPlatform::SetValueInSlot(slot_, value);
55 ThreadLocalPlatform::SlotType slot_;
58 } // namespace internal
61 #endif // MOJO_PUBLIC_UTILITY_LIB_THREAD_LOCAL_H_