You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2013/05/29 19:41:10 UTC

[32/35] git commit: Added 'ThreadLocal' to stout/thread.hpp.

Added 'ThreadLocal' to stout/thread.hpp.

Review: https://reviews.apache.org/r/11474


Project: http://git-wip-us.apache.org/repos/asf/incubator-mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mesos/commit/bb1c3534
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mesos/tree/bb1c3534
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mesos/diff/bb1c3534

Branch: refs/heads/master
Commit: bb1c3534e7ec678ef88173a99afe6a5c6465cc1c
Parents: 5307934
Author: Benjamin Hindman <be...@twitter.com>
Authored: Mon May 27 12:21:48 2013 -0700
Committer: Benjamin Hindman <be...@twitter.com>
Committed: Tue May 28 14:20:25 2013 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/3rdparty/stout/Makefile.am     |    2 +
 .../3rdparty/stout/include/stout/thread.hpp        |   49 +++++++++++++++
 .../3rdparty/stout/tests/thread_tests.cpp          |   26 ++++++++
 3 files changed, 77 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/bb1c3534/3rdparty/libprocess/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am
index 4fdee2c..84062a0 100644
--- a/3rdparty/libprocess/3rdparty/stout/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am
@@ -43,6 +43,7 @@ EXTRA_DIST =					\
   include/stout/stopwatch.hpp			\
   include/stout/stringify.hpp			\
   include/stout/strings.hpp			\
+  include/stout/thread.hpp			\
   include/stout/try.hpp				\
   include/stout/utils.hpp			\
   include/stout/uuid.hpp			\
@@ -59,4 +60,5 @@ EXTRA_DIST =					\
   tests/os_tests.cpp				\
   tests/proc_tests.cpp				\
   tests/strings_tests.cpp			\
+  tests/thread_tests.cpp			\
   tests/uuid_tests.cpp

http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/bb1c3534/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..c5dbe79
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/thread.hpp
@@ -0,0 +1,49 @@
+#ifndef __STOUT_THREAD_HPP__
+#define __STOUT_THREAD_HPP__
+
+#include <pthread.h>
+#include <stdio.h> // For perror.
+#include <stdlib.h> // For abort.
+
+template <typename T>
+struct ThreadLocal
+{
+  ThreadLocal()
+  {
+    if (pthread_key_create(&key, NULL) != 0) {
+      perror("Failed to create thread local, pthread_key_create");
+      abort();
+    }
+  }
+
+  ThreadLocal<T>& operator = (T* t)
+  {
+    if (pthread_setspecific(key, t) != 0) {
+      perror("Failed to set thread local, pthread_setspecific");
+      abort();
+    }
+    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/incubator-mesos/blob/bb1c3534/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..7519b12
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/tests/thread_tests.cpp
@@ -0,0 +1,26 @@
+#include <gtest/gtest.h>
+
+#include <string>
+
+#include <stout/thread.hpp>
+
+TEST(Thread, 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_;
+}