Revert "Removed ThreadLocal from stout." This reverts commit 8574d0c2b953ed7abf8621b5e3591d4473d91376.
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/96545120 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/96545120 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/96545120 Branch: refs/heads/master Commit: 96545120c0182b7301a8974238118ac15bd98112 Parents: 9635137 Author: Benjamin Hindman <[email protected]> Authored: Fri Jul 24 16:40:13 2015 -0700 Committer: Benjamin Hindman <[email protected]> Committed: Fri Jul 24 16:40:33 2015 -0700 ---------------------------------------------------------------------- 3rdparty/libprocess/3rdparty/stout/Makefile.am | 1 + .../3rdparty/stout/include/Makefile.am | 1 + .../3rdparty/stout/include/stout/thread.hpp | 80 ++++++++++++++++++++ .../3rdparty/stout/tests/CMakeLists.txt | 3 +- .../3rdparty/stout/tests/thread_tests.cpp | 40 ++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/96545120/3rdparty/libprocess/3rdparty/stout/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am index f95ed03..89e7b18 100644 --- a/3rdparty/libprocess/3rdparty/stout/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am @@ -56,5 +56,6 @@ EXTRA_DIST = \ tests/strings_tests.cpp \ tests/subcommand_tests.cpp \ tests/svn_tests.cpp \ + tests/thread_tests.cpp \ tests/uuid_tests.cpp \ tests/version_tests.cpp http://git-wip-us.apache.org/repos/asf/mesos/blob/96545120/3rdparty/libprocess/3rdparty/stout/include/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am index 173f18b..5c19e3e 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am @@ -84,6 +84,7 @@ nobase_include_HEADERS = \ stout/svn.hpp \ stout/synchronized.hpp \ stout/tests/utils.hpp \ + stout/thread.hpp \ stout/try.hpp \ stout/unreachable.hpp \ stout/utils.hpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/96545120/3rdparty/libprocess/3rdparty/stout/include/stout/thread.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/thread.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/thread.hpp new file mode 100644 index 0000000..552d6e9 --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/thread.hpp @@ -0,0 +1,80 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STOUT_THREAD_HPP__ +#define __STOUT_THREAD_HPP__ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> // For perror. + +#include <string> + +#include <stout/abort.hpp> + +template <typename T> +struct ThreadLocal +{ + ThreadLocal() + { + errno = pthread_key_create(&key, NULL); + + if (errno != 0) { + ABORT(std::string("Failed to create thread local, pthread_key_create: ") + + strerror(errno)); + } + } + + ~ThreadLocal() + { + errno = pthread_key_delete(key); + + if (errno != 0) { + ABORT("Failed to destruct thread local, pthread_key_delete: " + + std::string(strerror(errno))); + } + } + + ThreadLocal<T>& operator = (T* t) + { + errno = pthread_setspecific(key, t); + + if (errno != 0) { + ABORT(std::string("Failed to set thread local, pthread_setspecific: ") + + strerror(errno)); + } + return *this; + } + + operator T* () const + { + return reinterpret_cast<T*>(pthread_getspecific(key)); + } + + T* operator -> () const + { + return reinterpret_cast<T*>(pthread_getspecific(key)); + } + +private: + // Not expecting any other operators to be used (and the rest?). + bool operator * (const ThreadLocal<T>&) const; + bool operator == (const ThreadLocal<T>&) const; + bool operator != (const ThreadLocal<T>&) const; + bool operator < (const ThreadLocal<T>&) const; + bool operator > (const ThreadLocal<T>&) const; + + pthread_key_t key; +}; + +#endif // __STOUT_THREAD_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/96545120/3rdparty/libprocess/3rdparty/stout/tests/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/CMakeLists.txt b/3rdparty/libprocess/3rdparty/stout/tests/CMakeLists.txt index 3ed9145..b1a5190 100644 --- a/3rdparty/libprocess/3rdparty/stout/tests/CMakeLists.txt +++ b/3rdparty/libprocess/3rdparty/stout/tests/CMakeLists.txt @@ -54,6 +54,7 @@ set(STOUT_TESTS_SRC some_tests.cpp strings_tests.cpp subcommand_tests.cpp + thread_tests.cpp uuid_tests.cpp version_tests.cpp ) @@ -82,4 +83,4 @@ add_dependencies(${STOUT_TESTS_TARGET} ${STOUT_TEST_DEPENDENCIES}) # ADD TEST TARGET (runs when you do, e.g., `make check`). ######################################################### -add_test(NAME StoutTests COMMAND ./${STOUT_TESTS_TARGET}) +add_test(NAME StoutTests COMMAND ./${STOUT_TESTS_TARGET}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/mesos/blob/96545120/3rdparty/libprocess/3rdparty/stout/tests/thread_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/thread_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/thread_tests.cpp new file mode 100644 index 0000000..319fcdf --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/tests/thread_tests.cpp @@ -0,0 +1,40 @@ +/** +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License +*/ + +#include <string> + +#include <gtest/gtest.h> + +#include <stout/thread.hpp> + +TEST(ThreadTest, Local) +{ + ThreadLocal<std::string>* _s_ = new ThreadLocal<std::string>(); + + std::string* s = new std::string(); + + ASSERT_TRUE(*(_s_) == NULL); + + (*_s_) = s; + + ASSERT_TRUE(*(_s_) == s); + ASSERT_FALSE(*(_s_) == NULL); + + (*_s_) = NULL; + + ASSERT_TRUE(*(_s_) == NULL); + + delete s; + delete _s_; +}
