Skip to content

Commit

Permalink
use c++11 primitives for threads and locks
Browse files Browse the repository at this point in the history
  • Loading branch information
maddanio committed Nov 22, 2023
1 parent aae269e commit 4b78c92
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 502 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ set(EFSW_CPP_SOURCE
src/efsw/FileWatcherGeneric.cpp
src/efsw/FileWatcherImpl.cpp
src/efsw/Log.cpp
src/efsw/Mutex.cpp
src/efsw/String.cpp
src/efsw/System.cpp
src/efsw/Thread.cpp
src/efsw/Watcher.cpp
src/efsw/WatcherGeneric.cpp
)
Expand All @@ -77,16 +75,12 @@ endif()
if(WIN32)
list(APPEND EFSW_CPP_SOURCE
src/efsw/platform/win/FileSystemImpl.cpp
src/efsw/platform/win/MutexImpl.cpp
src/efsw/platform/win/SystemImpl.cpp
src/efsw/platform/win/ThreadImpl.cpp
)
else()
list(APPEND EFSW_CPP_SOURCE
src/efsw/platform/posix/FileSystemImpl.cpp
src/efsw/platform/posix/MutexImpl.cpp
src/efsw/platform/posix/SystemImpl.cpp
src/efsw/platform/posix/ThreadImpl.cpp
)
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/efsw/FileWatcherGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void FileWatcherGeneric::removeWatch( WatchID watchid ) {

void FileWatcherGeneric::watch() {
if ( NULL == mThread ) {
mThread = new Thread( &FileWatcherGeneric::run, this );
mThread = new Thread([this]{run();});
mThread->launch();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/efsw/FileWatcherKqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ bool FileWatcherKqueue::isAddingWatcher() const {

void FileWatcherKqueue::watch() {
if ( NULL == mThread ) {
mThread = new Thread( &FileWatcherKqueue::run, this );
mThread = new Thread([this]{run();});
mThread->launch();
}
}
Expand Down
14 changes: 2 additions & 12 deletions src/efsw/Lock.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
#ifndef EFSW_LOCK_HPP
#define EFSW_LOCK_HPP

#include <mutex>
#include <efsw/Mutex.hpp>

namespace efsw {

/** Simple mutex class */
class Lock {
public:
explicit Lock( Mutex& mutex ) : mMutex( mutex ) { mMutex.lock(); }

~Lock() { mMutex.unlock(); }

private:
Mutex& mMutex;
};

using Lock = std::unique_lock<Mutex>;
} // namespace efsw

#endif
20 changes: 0 additions & 20 deletions src/efsw/Mutex.cpp

This file was deleted.

25 changes: 2 additions & 23 deletions src/efsw/Mutex.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
#ifndef EFSW_MUTEX_HPP
#define EFSW_MUTEX_HPP

#include <efsw/base.hpp>
#include <mutex>

namespace efsw {

namespace Platform {
class MutexImpl;
}

/** Simple mutex class */
class Mutex {
public:
Mutex();

~Mutex();

/** Lock the mutex */
void lock();

/** Unlock the mutex */
void unlock();

private:
Platform::MutexImpl* mMutexImpl;
};

using Mutex = std::mutex;
} // namespace efsw

#endif
41 changes: 0 additions & 41 deletions src/efsw/Thread.cpp

This file was deleted.

107 changes: 28 additions & 79 deletions src/efsw/Thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,48 @@
#define EFSW_THREAD_HPP

#include <efsw/base.hpp>
#include <functional>
#include <memory>
#include <thread>

namespace efsw {

namespace Platform {
class ThreadImpl;
}
namespace Private {
struct ThreadFunc;
}

/** @brief Thread manager class */
class Thread {
public:
typedef void ( *FuncType )( void* );

template <typename F> Thread( F function );

template <typename F, typename A> Thread( F function, A argument );

template <typename C> Thread( void ( C::*function )(), C* object );
Thread(std::function<void()> fun)
: mFun{std::move(fun)}
{
}

virtual ~Thread();
~Thread()
{
wait();
}

/** Launch the thread */
virtual void launch();
void launch()
{
if (!mThread)
mThread.reset(new std::thread{std::move(mFun)});
}

/** Wait the thread until end */
void wait();

/** Terminate the thread */
void terminate();

protected:
Thread();

private:
friend class Platform::ThreadImpl;

/** The virtual function to run in the thread */
virtual void run();

Platform::ThreadImpl* mThreadImpl; ///< OS-specific implementation of the thread
Private::ThreadFunc* mEntryPoint; ///< Abstraction of the function to run
};

//! NOTE: Taken from SFML2 threads
namespace Private {

// Base class for abstract thread functions
struct ThreadFunc {
virtual ~ThreadFunc() {}
virtual void run() = 0;
void wait()
{
if (mThread)
{
mThread->join();
mThread.reset();
}
}
private:

std::unique_ptr<std::thread> mThread;
std::function<void()> mFun;
};

// Specialization using a functor (including free functions) with no argument
template <typename T> struct ThreadFunctor : ThreadFunc {
ThreadFunctor( T functor ) : m_functor( functor ) {}
virtual void run() { m_functor(); }
T m_functor;
};

// Specialization using a functor (including free functions) with one argument
template <typename F, typename A> struct ThreadFunctorWithArg : ThreadFunc {
ThreadFunctorWithArg( F function, A arg ) : m_function( function ), m_arg( arg ) {}
virtual void run() { m_function( m_arg ); }
F m_function;
A m_arg;
};

// Specialization using a member function
template <typename C> struct ThreadMemberFunc : ThreadFunc {
ThreadMemberFunc( void ( C::*function )(), C* object ) :
m_function( function ), m_object( object ) {}
virtual void run() { ( m_object->*m_function )(); }
void ( C::*m_function )();
C* m_object;
};

} // namespace Private

template <typename F>
Thread::Thread( F functor ) :
mThreadImpl( NULL ), mEntryPoint( new Private::ThreadFunctor<F>( functor ) ) {}

template <typename F, typename A>
Thread::Thread( F function, A argument ) :
mThreadImpl( NULL ),
mEntryPoint( new Private::ThreadFunctorWithArg<F efCOMMA A>( function, argument ) ) {}

template <typename C>
Thread::Thread( void ( C::*function )(), C* object ) :
mThreadImpl( NULL ), mEntryPoint( new Private::ThreadMemberFunc<C>( function, object ) ) {}

} // namespace efsw

#endif
4 changes: 0 additions & 4 deletions src/efsw/platform/platformimpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
#include <efsw/base.hpp>

#if defined( EFSW_PLATFORM_POSIX )
#include <efsw/platform/posix/ThreadImpl.hpp>
#include <efsw/platform/posix/MutexImpl.hpp>
#include <efsw/platform/posix/SystemImpl.hpp>
#include <efsw/platform/posix/FileSystemImpl.hpp>
#elif EFSW_PLATFORM == EFSW_PLATFORM_WIN32
#include <efsw/platform/win/ThreadImpl.hpp>
#include <efsw/platform/win/MutexImpl.hpp>
#include <efsw/platform/win/SystemImpl.hpp>
#include <efsw/platform/win/FileSystemImpl.hpp>
#else
Expand Down
28 changes: 0 additions & 28 deletions src/efsw/platform/posix/MutexImpl.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions src/efsw/platform/posix/MutexImpl.hpp

This file was deleted.

Loading

0 comments on commit 4b78c92

Please sign in to comment.