[llvm] r224225 - ThreadLocal: Return a mutable pointer if templated with a non-const type
David Majnemer
david.majnemer at gmail.com
Sun Dec 14 17:04:46 PST 2014
Author: majnemer
Date: Sun Dec 14 19:04:45 2014
New Revision: 224225
URL: http://llvm.org/viewvc/llvm-project?rev=224225&view=rev
Log:
ThreadLocal: Return a mutable pointer if templated with a non-const type
It makes more sense for ThreadLocal<const T>::get to return a const T*
and ThreadLocal<T>::get to return a T*.
Modified:
llvm/trunk/include/llvm/Support/ThreadLocal.h
llvm/trunk/lib/Support/ThreadLocal.cpp
llvm/trunk/lib/Support/Unix/ThreadLocal.inc
llvm/trunk/lib/Support/Windows/ThreadLocal.inc
llvm/trunk/unittests/Support/ThreadLocalTest.cpp
Modified: llvm/trunk/include/llvm/Support/ThreadLocal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/ThreadLocal.h?rev=224225&r1=224224&r2=224225&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/ThreadLocal.h (original)
+++ llvm/trunk/include/llvm/Support/ThreadLocal.h Sun Dec 14 19:04:45 2014
@@ -36,7 +36,7 @@ namespace llvm {
ThreadLocalImpl();
virtual ~ThreadLocalImpl();
void setInstance(const void* d);
- const void* getInstance();
+ void *getInstance();
void removeInstance();
};
Modified: llvm/trunk/lib/Support/ThreadLocal.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/ThreadLocal.cpp?rev=224225&r1=224224&r2=224225&view=diff
==============================================================================
--- llvm/trunk/lib/Support/ThreadLocal.cpp (original)
+++ llvm/trunk/lib/Support/ThreadLocal.cpp Sun Dec 14 19:04:45 2014
@@ -31,7 +31,7 @@ void ThreadLocalImpl::setInstance(const
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) errorcode;
}
-const void* ThreadLocalImpl::getInstance() {
+void *ThreadLocalImpl::getInstance() {
pthread_key_t* key = reinterpret_cast<pthread_key_t*>(&data);
return pthread_getspecific(*key);
}
Modified: llvm/trunk/lib/Support/Unix/ThreadLocal.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/ThreadLocal.inc?rev=224225&r1=224224&r2=224225&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/ThreadLocal.inc (original)
+++ llvm/trunk/lib/Support/Unix/ThreadLocal.inc Sun Dec 14 19:04:45 2014
@@ -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); }
}
Modified: llvm/trunk/lib/Support/Windows/ThreadLocal.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/ThreadLocal.inc?rev=224225&r1=224224&r2=224225&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/ThreadLocal.inc (original)
+++ llvm/trunk/lib/Support/Windows/ThreadLocal.inc Sun Dec 14 19:04:45 2014
@@ -34,7 +34,7 @@ ThreadLocalImpl::~ThreadLocalImpl() {
TlsFree(*tls);
}
-const void* ThreadLocalImpl::getInstance() {
+void *ThreadLocalImpl::getInstance() {
DWORD* tls = reinterpret_cast<DWORD*>(&data);
return TlsGetValue(*tls);
}
Modified: llvm/trunk/unittests/Support/ThreadLocalTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ThreadLocalTest.cpp?rev=224225&r1=224224&r2=224225&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ThreadLocalTest.cpp (original)
+++ llvm/trunk/unittests/Support/ThreadLocalTest.cpp Sun Dec 14 19:04:45 2014
@@ -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());
}
}
More information about the llvm-commits
mailing list