Skip to content

Commit 4b78c92

Browse files
committed
use c++11 primitives for threads and locks
1 parent aae269e commit 4b78c92

17 files changed

+34
-502
lines changed

CMakeLists.txt

-6
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ set(EFSW_CPP_SOURCE
4747
src/efsw/FileWatcherGeneric.cpp
4848
src/efsw/FileWatcherImpl.cpp
4949
src/efsw/Log.cpp
50-
src/efsw/Mutex.cpp
5150
src/efsw/String.cpp
5251
src/efsw/System.cpp
53-
src/efsw/Thread.cpp
5452
src/efsw/Watcher.cpp
5553
src/efsw/WatcherGeneric.cpp
5654
)
@@ -77,16 +75,12 @@ endif()
7775
if(WIN32)
7876
list(APPEND EFSW_CPP_SOURCE
7977
src/efsw/platform/win/FileSystemImpl.cpp
80-
src/efsw/platform/win/MutexImpl.cpp
8178
src/efsw/platform/win/SystemImpl.cpp
82-
src/efsw/platform/win/ThreadImpl.cpp
8379
)
8480
else()
8581
list(APPEND EFSW_CPP_SOURCE
8682
src/efsw/platform/posix/FileSystemImpl.cpp
87-
src/efsw/platform/posix/MutexImpl.cpp
8883
src/efsw/platform/posix/SystemImpl.cpp
89-
src/efsw/platform/posix/ThreadImpl.cpp
9084
)
9185
endif()
9286

src/efsw/FileWatcherGeneric.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void FileWatcherGeneric::removeWatch( WatchID watchid ) {
101101

102102
void FileWatcherGeneric::watch() {
103103
if ( NULL == mThread ) {
104-
mThread = new Thread( &FileWatcherGeneric::run, this );
104+
mThread = new Thread([this]{run();});
105105
mThread->launch();
106106
}
107107
}

src/efsw/FileWatcherKqueue.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ bool FileWatcherKqueue::isAddingWatcher() const {
162162

163163
void FileWatcherKqueue::watch() {
164164
if ( NULL == mThread ) {
165-
mThread = new Thread( &FileWatcherKqueue::run, this );
165+
mThread = new Thread([this]{run();});
166166
mThread->launch();
167167
}
168168
}

src/efsw/Lock.hpp

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
#ifndef EFSW_LOCK_HPP
22
#define EFSW_LOCK_HPP
33

4+
#include <mutex>
45
#include <efsw/Mutex.hpp>
56

67
namespace efsw {
7-
8-
/** Simple mutex class */
9-
class Lock {
10-
public:
11-
explicit Lock( Mutex& mutex ) : mMutex( mutex ) { mMutex.lock(); }
12-
13-
~Lock() { mMutex.unlock(); }
14-
15-
private:
16-
Mutex& mMutex;
17-
};
18-
8+
using Lock = std::unique_lock<Mutex>;
199
} // namespace efsw
2010

2111
#endif

src/efsw/Mutex.cpp

-20
This file was deleted.

src/efsw/Mutex.hpp

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
11
#ifndef EFSW_MUTEX_HPP
22
#define EFSW_MUTEX_HPP
33

4-
#include <efsw/base.hpp>
4+
#include <mutex>
55

66
namespace efsw {
7-
8-
namespace Platform {
9-
class MutexImpl;
10-
}
11-
12-
/** Simple mutex class */
13-
class Mutex {
14-
public:
15-
Mutex();
16-
17-
~Mutex();
18-
19-
/** Lock the mutex */
20-
void lock();
21-
22-
/** Unlock the mutex */
23-
void unlock();
24-
25-
private:
26-
Platform::MutexImpl* mMutexImpl;
27-
};
28-
7+
using Mutex = std::mutex;
298
} // namespace efsw
309

3110
#endif

src/efsw/Thread.cpp

-41
This file was deleted.

src/efsw/Thread.hpp

+28-79
Original file line numberDiff line numberDiff line change
@@ -2,99 +2,48 @@
22
#define EFSW_THREAD_HPP
33

44
#include <efsw/base.hpp>
5+
#include <functional>
6+
#include <memory>
7+
#include <thread>
58

69
namespace efsw {
710

8-
namespace Platform {
9-
class ThreadImpl;
10-
}
11-
namespace Private {
12-
struct ThreadFunc;
13-
}
14-
1511
/** @brief Thread manager class */
1612
class Thread {
1713
public:
18-
typedef void ( *FuncType )( void* );
19-
20-
template <typename F> Thread( F function );
21-
22-
template <typename F, typename A> Thread( F function, A argument );
2314

24-
template <typename C> Thread( void ( C::*function )(), C* object );
15+
Thread(std::function<void()> fun)
16+
: mFun{std::move(fun)}
17+
{
18+
}
2519

26-
virtual ~Thread();
20+
~Thread()
21+
{
22+
wait();
23+
}
2724

2825
/** Launch the thread */
29-
virtual void launch();
26+
void launch()
27+
{
28+
if (!mThread)
29+
mThread.reset(new std::thread{std::move(mFun)});
30+
}
3031

3132
/** Wait the thread until end */
32-
void wait();
33-
34-
/** Terminate the thread */
35-
void terminate();
36-
37-
protected:
38-
Thread();
39-
40-
private:
41-
friend class Platform::ThreadImpl;
42-
43-
/** The virtual function to run in the thread */
44-
virtual void run();
45-
46-
Platform::ThreadImpl* mThreadImpl; ///< OS-specific implementation of the thread
47-
Private::ThreadFunc* mEntryPoint; ///< Abstraction of the function to run
48-
};
49-
50-
//! NOTE: Taken from SFML2 threads
51-
namespace Private {
52-
53-
// Base class for abstract thread functions
54-
struct ThreadFunc {
55-
virtual ~ThreadFunc() {}
56-
virtual void run() = 0;
33+
void wait()
34+
{
35+
if (mThread)
36+
{
37+
mThread->join();
38+
mThread.reset();
39+
}
40+
}
41+
private:
42+
43+
std::unique_ptr<std::thread> mThread;
44+
std::function<void()> mFun;
5745
};
5846

59-
// Specialization using a functor (including free functions) with no argument
60-
template <typename T> struct ThreadFunctor : ThreadFunc {
61-
ThreadFunctor( T functor ) : m_functor( functor ) {}
62-
virtual void run() { m_functor(); }
63-
T m_functor;
64-
};
65-
66-
// Specialization using a functor (including free functions) with one argument
67-
template <typename F, typename A> struct ThreadFunctorWithArg : ThreadFunc {
68-
ThreadFunctorWithArg( F function, A arg ) : m_function( function ), m_arg( arg ) {}
69-
virtual void run() { m_function( m_arg ); }
70-
F m_function;
71-
A m_arg;
72-
};
73-
74-
// Specialization using a member function
75-
template <typename C> struct ThreadMemberFunc : ThreadFunc {
76-
ThreadMemberFunc( void ( C::*function )(), C* object ) :
77-
m_function( function ), m_object( object ) {}
78-
virtual void run() { ( m_object->*m_function )(); }
79-
void ( C::*m_function )();
80-
C* m_object;
81-
};
82-
83-
} // namespace Private
84-
85-
template <typename F>
86-
Thread::Thread( F functor ) :
87-
mThreadImpl( NULL ), mEntryPoint( new Private::ThreadFunctor<F>( functor ) ) {}
88-
89-
template <typename F, typename A>
90-
Thread::Thread( F function, A argument ) :
91-
mThreadImpl( NULL ),
92-
mEntryPoint( new Private::ThreadFunctorWithArg<F efCOMMA A>( function, argument ) ) {}
93-
94-
template <typename C>
95-
Thread::Thread( void ( C::*function )(), C* object ) :
96-
mThreadImpl( NULL ), mEntryPoint( new Private::ThreadMemberFunc<C>( function, object ) ) {}
97-
9847
} // namespace efsw
9948

10049
#endif

src/efsw/platform/platformimpl.hpp

-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
#include <efsw/base.hpp>
55

66
#if defined( EFSW_PLATFORM_POSIX )
7-
#include <efsw/platform/posix/ThreadImpl.hpp>
8-
#include <efsw/platform/posix/MutexImpl.hpp>
97
#include <efsw/platform/posix/SystemImpl.hpp>
108
#include <efsw/platform/posix/FileSystemImpl.hpp>
119
#elif EFSW_PLATFORM == EFSW_PLATFORM_WIN32
12-
#include <efsw/platform/win/ThreadImpl.hpp>
13-
#include <efsw/platform/win/MutexImpl.hpp>
1410
#include <efsw/platform/win/SystemImpl.hpp>
1511
#include <efsw/platform/win/FileSystemImpl.hpp>
1612
#else

src/efsw/platform/posix/MutexImpl.cpp

-28
This file was deleted.

src/efsw/platform/posix/MutexImpl.hpp

-30
This file was deleted.

0 commit comments

Comments
 (0)