[brlcad-commits] SF.net SVN: brlcad:[55020] brlcad/trunk/src/libbu/thread.cpp
Open Source Solid Modeling CAD
Brought to you by:
brlcad
From: <br...@us...> - 2013-04-03 05:27:33
|
Revision: 55020 http://brlcad.svn.sourceforge.net/brlcad/?rev=55020&view=rev Author: brlcad Date: 2013-04-03 05:27:26 +0000 (Wed, 03 Apr 2013) Log Message: ----------- add initial support for getting/setting the cpu number via thread local storage (TLS) for pthreads Modified Paths: -------------- brlcad/trunk/src/libbu/thread.cpp Modified: brlcad/trunk/src/libbu/thread.cpp =================================================================== --- brlcad/trunk/src/libbu/thread.cpp 2013-04-03 05:14:34 UTC (rev 55019) +++ brlcad/trunk/src/libbu/thread.cpp 2013-04-03 05:27:26 UTC (rev 55020) @@ -20,22 +20,82 @@ #include "common.h" -//static boost::thread_specific_ptr<int> thread_cpu; -//__thread int thread_cpu; -//__declspec(thread) int thread_cpu; +#include <vector> +#ifdef HAVE_PTHREAD_H +# include <pthread.h> +#endif + + +template<typename T> +class ThreadLocal +{ +private: +#ifdef HAVE_PTHREAD_H + pthread_key_t key; +#endif + std::vector<T*> vals; +public: + ThreadLocal() { +#ifdef HAVE_PTHREAD_H + pthread_key_create(&key, NULL); +#endif + } + ~ThreadLocal() { +#ifdef HAVE_PTHREAD_H + pthread_key_delete(key); +#endif + while (!vals.empty()) { + delete vals.back(); + vals.pop_back(); + } + vals.clear(); + } + ThreadLocal& operator=(T& val) { + T* value = new T(val); + vals.push_back(value); +#ifdef HAVE_PTHREAD_H + pthread_setspecific(key, value); +#endif + return *this; + } + bool operator!() { +#ifdef HAVE_PTHREAD_H + return (pthread_getspecific(key) == NULL); +#endif + } + operator T() { +#ifdef HAVE_PTHREAD_H + return *static_cast<T*>(pthread_getspecific(key)); +#endif + } +}; + + +#if defined(__cplusplus) && __cplusplus > 199711L +// C++11 provides thread-local storage +thread_local int thread_cpu = 0; +#elif defined(HAVE___THREAD) +__thread int thread_cpu = 0; +#elif defined(_MSC_VER) +__declspec(thread) int thread_cpu; +#else +ThreadLocal<int> thread_cpu; +#endif + extern "C" { void -thread_set_cpu(int UNUSED(cpu)) +thread_set_cpu(int cpu) { + thread_cpu = cpu; } int thread_get_cpu(void) { - return 0; + return thread_cpu; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |