From eea1e607177e339e05daa1a2ec969a9dd12f2497 Mon Sep 17 00:00:00 2001 From: Kimball Thurston Date: Wed, 4 Apr 2018 00:35:17 +1200 Subject: [PATCH] switch IlmThread to use c++11 threads when enabled --- IlmBase/IlmThread/IlmThread.cpp | 48 ++++++++++++++++++++++++---- IlmBase/IlmThread/IlmThread.h | 42 ++++++++++++++---------- IlmBase/IlmThread/IlmThreadPosix.cpp | 2 ++ IlmBase/IlmThread/IlmThreadWin32.cpp | 6 ++++ 4 files changed, 76 insertions(+), 22 deletions(-) diff --git a/IlmBase/IlmThread/IlmThread.cpp b/IlmBase/IlmThread/IlmThread.cpp index 5e9c38375f..6ad573a3fb 100644 --- a/IlmBase/IlmThread/IlmThread.cpp +++ b/IlmBase/IlmThread/IlmThread.cpp @@ -34,21 +34,56 @@ //----------------------------------------------------------------------------- // -// class Thread -- dummy implementation for -// platforms that do not support threading +// class Thread -- this file contains two implementations of thread: +// - dummy implementation for platforms that do not support threading +// when FORCE_CXX03 is on +// - c++11 and newer version // //----------------------------------------------------------------------------- #include "IlmBaseConfig.h" - -#if !defined (_WIN32) &&!(_WIN64) && !(HAVE_PTHREAD) - #include "IlmThread.h" #include "Iex.h" ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_ENTER +#ifndef ILMBASE_FORCE_CXX03 +//----------------------------------------------------------------------------- +// C++11 and newer implementation +//----------------------------------------------------------------------------- +bool +supportsThreads () +{ + return true; +} + +Thread::Thread () +{ + // empty +} + +Thread::~Thread () +{ + // hopefully the thread has basically exited and we are just + // cleaning up, because run is a virtual function, so the v-table + // has already been partly destroyed... + if ( _thread.joinable () ) + _thread.join (); +} + + +void +Thread::start () +{ + _thread = std::thread (&Thread::run, this); +} + +#else +# if !defined (_WIN32) &&!(_WIN64) && !(HAVE_PTHREAD) +//----------------------------------------------------------------------------- +// FORCE_CXX03 with no windows / pthread support +//----------------------------------------------------------------------------- bool supportsThreads () { @@ -73,8 +108,9 @@ Thread::start () { throw IEX_NAMESPACE::NoImplExc ("Threads not supported on this platform."); } +# endif +#endif ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT -#endif diff --git a/IlmBase/IlmThread/IlmThread.h b/IlmBase/IlmThread/IlmThread.h index 1f6d9e68cd..249258e786 100644 --- a/IlmBase/IlmThread/IlmThread.h +++ b/IlmBase/IlmThread/IlmThread.h @@ -94,15 +94,19 @@ #include "IlmThreadExport.h" #include "IlmThreadNamespace.h" -#if defined _WIN32 || defined _WIN64 - #ifdef NOMINMAX - #undef NOMINMAX - #endif - #define NOMINMAX - #include - #include -#elif HAVE_PTHREAD - #include +#ifdef ILMBASE_FORCE_CXX03 +# if defined _WIN32 || defined _WIN64 +# ifdef NOMINMAX +# undef NOMINMAX +# endif +# define NOMINMAX +# include +# include +# elif HAVE_PTHREAD +# include +# endif +#else +# include #endif ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER @@ -122,19 +126,25 @@ class ILMTHREAD_EXPORT Thread Thread (); virtual ~Thread (); - void start (); - virtual void run () = 0; - + void start (); + virtual void run () = 0; + private: - #if defined _WIN32 || defined _WIN64 +#ifdef ILMBASE_FORCE_CXX03 +# if defined _WIN32 || defined _WIN64 HANDLE _thread; - #elif HAVE_PTHREAD +# elif HAVE_PTHREAD pthread_t _thread; - #endif - +# endif void operator = (const Thread& t); // not implemented Thread (const Thread& t); // not implemented +#else + std::thread _thread; + + Thread &operator= (const Thread& t) = delete; + Thread (const Thread& t) = delete; +#endif }; diff --git a/IlmBase/IlmThread/IlmThreadPosix.cpp b/IlmBase/IlmThread/IlmThreadPosix.cpp index c487e39fa9..4aee3f3ca2 100644 --- a/IlmBase/IlmThread/IlmThreadPosix.cpp +++ b/IlmBase/IlmThread/IlmThreadPosix.cpp @@ -42,6 +42,7 @@ #include "IlmBaseConfig.h" #if HAVE_PTHREAD +#ifdef ILMBASE_FORCE_CXX03 #include "IlmThread.h" #include "Iex.h" @@ -96,3 +97,4 @@ Thread::start () ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT #endif +#endif diff --git a/IlmBase/IlmThread/IlmThreadWin32.cpp b/IlmBase/IlmThread/IlmThreadWin32.cpp index c563091b71..43ec23f6e6 100644 --- a/IlmBase/IlmThread/IlmThreadWin32.cpp +++ b/IlmBase/IlmThread/IlmThreadWin32.cpp @@ -39,6 +39,10 @@ //----------------------------------------------------------------------------- +#include "IlmBaseConfig.h" + +#ifdef ILMBASE_FORCE_CXX03 + #include "IlmThread.h" #include "Iex.h" #include @@ -93,3 +97,5 @@ Thread::start () ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT + +#endif