ThreadLocal: Return a mutable pointer if templated with a non-const type
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 15 Dec 2014 01:04:45 +0000 (01:04 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 15 Dec 2014 01:04:45 +0000 (01:04 +0000)
It makes more sense for ThreadLocal<const T>::get to return a const T*
and ThreadLocal<T>::get to return a T*.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224225 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/ThreadLocal.h
lib/Support/ThreadLocal.cpp
lib/Support/Unix/ThreadLocal.inc
lib/Support/Windows/ThreadLocal.inc
unittests/Support/ThreadLocalTest.cpp

index 7518626901e0b279dcae9ba9f781364684084d4a..427a67e2a96d0ead04980385f60f91593a7ba101 100644 (file)
@@ -36,7 +36,7 @@ namespace llvm {
       ThreadLocalImpl();
       virtual ~ThreadLocalImpl();
       void setInstance(const void* d);
-      const void* getInstance();
+      void *getInstance();
       void removeInstance();
     };
 
index 2dec9eb417f5bf4324e7b014c825a16c2664e9c8..e06221925668c713212e056672019eb8f10e8c62 100644 (file)
@@ -31,7 +31,7 @@ void ThreadLocalImpl::setInstance(const void* d) {
   void **pd = reinterpret_cast<void**>(&data);
   *pd = const_cast<void*>(d);
 }
-const void* ThreadLocalImpl::getInstance() {
+void *ThreadLocalImpl::getInstance() {
   void **pd = reinterpret_cast<void**>(&data);
   return *pd;
 }
@@ -72,7 +72,7 @@ void ThreadLocalImpl::setInstance(const void* d) {
   (void) errorcode;
 }
 
-const void* ThreadLocalImpl::getInstance() {
+void *ThreadLocalImpl::getInstance() {
   pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
   return pthread_getspecific(*key);
 }
index f14d0fa3d522e3b592525968f73bb12b7f1e66ee..fa746a628e346db516a34be80c6033e577025674 100644 (file)
@@ -21,6 +21,6 @@ using namespace sys;
 ThreadLocalImpl::ThreadLocalImpl() : data() { }
 ThreadLocalImpl::~ThreadLocalImpl() { }
 void ThreadLocalImpl::setInstance(const void* d) { data = const_cast<void*>(d);}
-const void* ThreadLocalImpl::getInstance() { return data; }
+void *ThreadLocalImpl::getInstance() { return data; }
 void ThreadLocalImpl::removeInstance() { setInstance(0); }
 }
index 14ce61933cba72a559ecbadae560f20d386483dd..b9cb8ff9836ecf4c87b1bed5745328bfa5d3ac7d 100644 (file)
@@ -34,7 +34,7 @@ ThreadLocalImpl::~ThreadLocalImpl() {
   TlsFree(*tls);
 }
 
-const void* ThreadLocalImpl::getInstance() {
+void *ThreadLocalImpl::getInstance() {
   DWORD* tls = reinterpret_cast<DWORD*>(&data);
   return TlsGetValue(*tls);
 }
index ea751be230ccfc4e3140deae8695f19835bfb40c..e71c7dba22fc98135fcc503a4816934736cef815 100644 (file)
@@ -1,6 +1,6 @@
-//===- llvm/unittest/Support/ThreadLocalTest.cpp - Therad Local tests   ---===//
+//===- llvm/unittest/Support/ThreadLocalTest.cpp - ThreadLocal tests ------===//
 //
-//                    The LLVM Compiler Infrastructure
+//                     The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
@@ -9,6 +9,7 @@
 
 #include "llvm/Support/ThreadLocal.h"
 #include "gtest/gtest.h"
+#include <type_traits>
 
 using namespace llvm;
 using namespace sys;
@@ -25,6 +26,10 @@ struct S {
 TEST_F(ThreadLocalTest, Basics) {
   ThreadLocal<const S> x;
 
+  static_assert(
+      std::is_const<std::remove_pointer<decltype(x.get())>::type>::value,
+      "ThreadLocal::get didn't return a pointer to const object");
+
   EXPECT_EQ(nullptr, x.get());
 
   S s;
@@ -33,6 +38,20 @@ TEST_F(ThreadLocalTest, Basics) {
 
   x.erase();
   EXPECT_EQ(nullptr, x.get());
+
+  ThreadLocal<S> y;
+
+  static_assert(
+      !std::is_const<std::remove_pointer<decltype(y.get())>::type>::value,
+      "ThreadLocal::get returned a pointer to const object");
+
+  EXPECT_EQ(nullptr, y.get());
+
+  y.set(&s);
+  EXPECT_EQ(&s, y.get());
+
+  y.erase();
+  EXPECT_EQ(nullptr, y.get());
 }
 
 }