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_CPP_UTILITY_LIB_THREAD_LOCAL_H_
6 #define MOJO_PUBLIC_CPP_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_() {}
37 void Allocate() { ThreadLocalPlatform::AllocateSlot(&slot_); }
39 void Free() { ThreadLocalPlatform::FreeSlot(slot_); }
42 return static_cast<P*>(ThreadLocalPlatform::GetValueFromSlot(slot_));
45 void Set(P* value) { ThreadLocalPlatform::SetValueInSlot(slot_, value); }
48 ThreadLocalPlatform::SlotType slot_;
51 } // namespace internal
54 #endif // MOJO_PUBLIC_CPP_UTILITY_LIB_THREAD_LOCAL_H_