Skip to content

Commit

Permalink
switch IlmThread to use c++11 threads when enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kdt3rd authored and nickrasmussen committed Aug 8, 2018
1 parent 610179c commit eea1e60
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 22 deletions.
48 changes: 42 additions & 6 deletions IlmBase/IlmThread/IlmThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
{
Expand All @@ -73,8 +108,9 @@ Thread::start ()
{
throw IEX_NAMESPACE::NoImplExc ("Threads not supported on this platform.");
}
# endif
#endif


ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT

#endif
42 changes: 26 additions & 16 deletions IlmBase/IlmThread/IlmThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,19 @@
#include "IlmThreadExport.h"
#include "IlmThreadNamespace.h"

#if defined _WIN32 || defined _WIN64
#ifdef NOMINMAX
#undef NOMINMAX
#endif
#define NOMINMAX
#include <windows.h>
#include <process.h>
#elif HAVE_PTHREAD
#include <pthread.h>
#ifdef ILMBASE_FORCE_CXX03
# if defined _WIN32 || defined _WIN64
# ifdef NOMINMAX
# undef NOMINMAX
# endif
# define NOMINMAX
# include <windows.h>
# include <process.h>
# elif HAVE_PTHREAD
# include <pthread.h>
# endif
#else
# include <thread>
#endif

ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
Expand All @@ -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
};


Expand Down
2 changes: 2 additions & 0 deletions IlmBase/IlmThread/IlmThreadPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "IlmBaseConfig.h"

#if HAVE_PTHREAD
#ifdef ILMBASE_FORCE_CXX03

#include "IlmThread.h"
#include "Iex.h"
Expand Down Expand Up @@ -96,3 +97,4 @@ Thread::start ()
ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT

#endif
#endif
6 changes: 6 additions & 0 deletions IlmBase/IlmThread/IlmThreadWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
//-----------------------------------------------------------------------------


#include "IlmBaseConfig.h"

#ifdef ILMBASE_FORCE_CXX03

#include "IlmThread.h"
#include "Iex.h"
#include <iostream>
Expand Down Expand Up @@ -93,3 +97,5 @@ Thread::start ()


ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT

#endif

0 comments on commit eea1e60

Please sign in to comment.