Skip to content

Commit

Permalink
switch mutex to be based on std::mutex when available
Browse files Browse the repository at this point in the history
  • Loading branch information
kdt3rd authored and nickrasmussen committed Aug 8, 2018
1 parent eea1e60 commit 848c8c3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
8 changes: 8 additions & 0 deletions IlmBase/IlmThread/IlmThreadForward.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@

#include "IlmThreadNamespace.h"

#ifndef ILMBASE_FORCE_CXX03
namespace std { class mutex; }
#endif

ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER

class Thread;
#ifdef ILMBASE_FORCE_CXX03
class Mutex;
#else
using Mutex = std::mutex;
#endif
class Lock;
class ThreadPool;
class Task;
Expand Down
7 changes: 4 additions & 3 deletions IlmBase/IlmThread/IlmThreadMutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@

#include "IlmBaseConfig.h"

#if !defined (_WIN32) && !(_WIN64) && !(HAVE_PTHREAD)

#include "IlmThreadMutex.h"
#ifdef ILMBASE_FORCE_CXX03
# if !defined (_WIN32) && !(_WIN64) && !(HAVE_PTHREAD)
# include "IlmThreadMutex.h"

ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_ENTER

Expand All @@ -56,4 +56,5 @@ void Mutex::unlock () const {}

ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT

# endif
#endif
50 changes: 35 additions & 15 deletions IlmBase/IlmThread/IlmThreadMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,39 @@
#include "IlmBaseConfig.h"
#include "IlmThreadNamespace.h"

#if defined _WIN32 || defined _WIN64
#ifdef NOMINMAX
#undef NOMINMAX
#endif
#define NOMINMAX
#include <windows.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>
# elif HAVE_PTHREAD
# include <pthread.h>
# endif
#else
# include <mutex>
#endif

ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER

class Lock;

// in c++11, this can just be
//
// using Mutex = std::mutex;
// unfortunately we can't use std::unique_lock as a replacement for Lock since
// they have different API.
//
// if we decide to break the API, we can just
//
// using Lock = std::lock_guard<std::mutex>;
// or
// using Lock = std::unique_lock<std::mutex>;
//
// (or eliminate the type completely and have people use the std library)
#ifdef ILMBASE_FORCE_CXX03

class Lock;

class ILMTHREAD_EXPORT Mutex
{
Expand All @@ -108,15 +127,16 @@ class ILMTHREAD_EXPORT Mutex

friend class Lock;
};

#else
using Mutex = std::mutex;
#endif

class ILMTHREAD_EXPORT Lock
{
public:

Lock (const Mutex& m, bool autoLock = true):
_mutex (m),
_locked (false)
Lock (Mutex& m, bool autoLock = true):
_mutex (m), _locked (false)
{
if (autoLock)
{
Expand Down Expand Up @@ -150,8 +170,8 @@ class ILMTHREAD_EXPORT Lock

private:

const Mutex & _mutex;
bool _locked;
Mutex & _mutex;
bool _locked;
};


Expand Down
10 changes: 6 additions & 4 deletions IlmBase/IlmThread/IlmThreadMutexPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@

#include "IlmBaseConfig.h"

#if HAVE_PTHREAD
#ifdef ILMBASE_FORCE_CXX03
# if HAVE_PTHREAD

#include "IlmThreadMutex.h"
#include "Iex.h"
#include <assert.h>
# include "IlmThreadMutex.h"
# include "Iex.h"
# include <assert.h>

ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_ENTER

Expand Down Expand Up @@ -82,4 +83,5 @@ Mutex::unlock () const

ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT

# endif
#endif
9 changes: 7 additions & 2 deletions IlmBase/IlmThread/IlmThreadMutexWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
//
//-----------------------------------------------------------------------------

#include "IlmThreadMutex.h"
#include "Iex.h"
#include "IlmBaseConfig.h"

#ifdef ILMBASE_FORCE_CXX03
# include "IlmThreadMutex.h"
# include "Iex.h"

ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_ENTER

Expand Down Expand Up @@ -71,3 +74,5 @@ Mutex::unlock () const


ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT

#endif

0 comments on commit 848c8c3

Please sign in to comment.