From 1f59bf44410889fc91c4acdfc5da21572ff6d3fa Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Mon, 15 Dec 2014 01:04:45 +0000 Subject: [PATCH] ThreadLocal: Return a mutable pointer if templated with a non-const type It makes more sense for ThreadLocal::get to return a const T* and ThreadLocal::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 | 2 +- lib/Support/ThreadLocal.cpp | 4 ++-- lib/Support/Unix/ThreadLocal.inc | 2 +- lib/Support/Windows/ThreadLocal.inc | 2 +- unittests/Support/ThreadLocalTest.cpp | 23 +++++++++++++++++++++-- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/llvm/Support/ThreadLocal.h b/include/llvm/Support/ThreadLocal.h index 7518626901e..427a67e2a96 100644 --- a/include/llvm/Support/ThreadLocal.h +++ b/include/llvm/Support/ThreadLocal.h @@ -36,7 +36,7 @@ namespace llvm { ThreadLocalImpl(); virtual ~ThreadLocalImpl(); void setInstance(const void* d); - const void* getInstance(); + void *getInstance(); void removeInstance(); }; diff --git a/lib/Support/ThreadLocal.cpp b/lib/Support/ThreadLocal.cpp index 2dec9eb417f..e0622192566 100644 --- a/lib/Support/ThreadLocal.cpp +++ b/lib/Support/ThreadLocal.cpp @@ -31,7 +31,7 @@ void ThreadLocalImpl::setInstance(const void* d) { void **pd = reinterpret_cast(&data); *pd = const_cast(d); } -const void* ThreadLocalImpl::getInstance() { +void *ThreadLocalImpl::getInstance() { void **pd = reinterpret_cast(&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(&data); return pthread_getspecific(*key); } diff --git a/lib/Support/Unix/ThreadLocal.inc b/lib/Support/Unix/ThreadLocal.inc index f14d0fa3d52..fa746a628e3 100644 --- a/lib/Support/Unix/ThreadLocal.inc +++ b/lib/Support/Unix/ThreadLocal.inc @@ -21,6 +21,6 @@ using namespace sys; ThreadLocalImpl::ThreadLocalImpl() : data() { } ThreadLocalImpl::~ThreadLocalImpl() { } void ThreadLocalImpl::setInstance(const void* d) { data = const_cast(d);} -const void* ThreadLocalImpl::getInstance() { return data; } +void *ThreadLocalImpl::getInstance() { return data; } void ThreadLocalImpl::removeInstance() { setInstance(0); } } diff --git a/lib/Support/Windows/ThreadLocal.inc b/lib/Support/Windows/ThreadLocal.inc index 14ce61933cb..b9cb8ff9836 100644 --- a/lib/Support/Windows/ThreadLocal.inc +++ b/lib/Support/Windows/ThreadLocal.inc @@ -34,7 +34,7 @@ ThreadLocalImpl::~ThreadLocalImpl() { TlsFree(*tls); } -const void* ThreadLocalImpl::getInstance() { +void *ThreadLocalImpl::getInstance() { DWORD* tls = reinterpret_cast(&data); return TlsGetValue(*tls); } diff --git a/unittests/Support/ThreadLocalTest.cpp b/unittests/Support/ThreadLocalTest.cpp index ea751be230c..e71c7dba22f 100644 --- a/unittests/Support/ThreadLocalTest.cpp +++ b/unittests/Support/ThreadLocalTest.cpp @@ -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 using namespace llvm; using namespace sys; @@ -25,6 +26,10 @@ struct S { TEST_F(ThreadLocalTest, Basics) { ThreadLocal x; + static_assert( + std::is_const::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 y; + + static_assert( + !std::is_const::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()); } } -- 2.34.1