Get rid of our own silly reimplementation of std::thread

This commit is contained in:
Henrik Rydgard 2017-02-27 20:51:36 +01:00
parent 5de190a45f
commit 50be4e72b2
23 changed files with 31 additions and 355 deletions

View File

@ -15,8 +15,9 @@
// Official git repository and contact information can be found at // Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <thread>
#include "i18n/i18n.h" #include "i18n/i18n.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"

View File

@ -17,7 +17,8 @@
#pragma once #pragma once
#include "thread/thread.h" #include <thread>
#include "base/mutex.h" #include "base/mutex.h"
#include "Core/Dialog/PSPDialog.h" #include "Core/Dialog/PSPDialog.h"
#include "Core/Dialog/SavedataParam.h" #include "Core/Dialog/SavedataParam.h"

View File

@ -15,8 +15,10 @@
// Official git repository and contact information can be found at // Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <string.h> #include <cstring>
#include "thread/thread.h" #include <thread>
#include <algorithm>
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "base/timeutil.h" #include "base/timeutil.h"
#include "Core/FileLoaders/CachingFileLoader.h" #include "Core/FileLoaders/CachingFileLoader.h"

View File

@ -15,9 +15,10 @@
// Official git repository and contact information can be found at // Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <string.h> #include <algorithm>
#include <thread>
#include <cstring>
#include "base/timeutil.h" #include "base/timeutil.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "Core/FileLoaders/RamCachingFileLoader.h" #include "Core/FileLoaders/RamCachingFileLoader.h"

View File

@ -17,9 +17,10 @@
#pragma once #pragma once
#include <thread>
#include "base/timeutil.h" #include "base/timeutil.h"
#include "base/mutex.h" #include "base/mutex.h"
#include "thread/thread.h"
#include "net/resolve.h" #include "net/resolve.h"
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"

View File

@ -17,8 +17,8 @@
#include <cstdlib> #include <cstdlib>
#include <set> #include <set>
#include <thread>
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "profiler/profiler.h" #include "profiler/profiler.h"

View File

@ -28,11 +28,11 @@
#include <cstdlib> #include <cstdlib>
#include <functional> #include <functional>
#include <thread>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/mutex.h" #include "base/mutex.h"
#include "profiler/profiler.h" #include "profiler/profiler.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "Common/Log.h" #include "Common/Log.h"
#include "Core/Config.h" #include "Core/Config.h"

View File

@ -15,6 +15,8 @@
// Official git repository and contact information can be found at // Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <thread>
#include "Core/Reporting.h" #include "Core/Reporting.h"
#include "Common/CPUDetect.h" #include "Common/CPUDetect.h"
@ -38,7 +40,6 @@
#include "base/stringutil.h" #include "base/stringutil.h"
#include "base/buffer.h" #include "base/buffer.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "file/zip_read.h" #include "file/zip_read.h"

View File

@ -17,11 +17,11 @@
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <thread>
#include "base/mutex.h" #include "base/mutex.h"
#include "base/timeutil.h" #include "base/timeutil.h"
#include "i18n/i18n.h" #include "i18n/i18n.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"

View File

@ -24,9 +24,9 @@
#include <string> #include <string>
#include <codecvt> #include <codecvt>
#endif #endif
#include <thread>
#include "math/math_util.h" #include "math/math_util.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "base/mutex.h" #include "base/mutex.h"
#include "util/text/utf8.h" #include "util/text/utf8.h"

View File

@ -18,6 +18,7 @@
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <cstring> #include <cstring>
#include <thread>
#include "file/file_util.h" #include "file/file_util.h"
#ifdef SHARED_LIBZIP #ifdef SHARED_LIBZIP
@ -25,7 +26,6 @@
#else #else
#include "ext/libzip/zip.h" #include "ext/libzip/zip.h"
#endif #endif
#include "thread/thread.h"
#include "util/text/utf8.h" #include "util/text/utf8.h"
#include "Common/Log.h" #include "Common/Log.h"

View File

@ -22,7 +22,7 @@
#pragma once #pragma once
#include "thread/thread.h" #include <thread>
#include "net/http_client.h" #include "net/http_client.h"
class GameManager { class GameManager {

View File

@ -34,6 +34,7 @@
#include <algorithm> #include <algorithm>
#endif #endif
#include <memory> #include <memory>
#include <thread>
#if defined(_WIN32) #if defined(_WIN32)
#include "Windows/DSoundStream.h" #include "Windows/DSoundStream.h"
@ -46,7 +47,6 @@
#include "base/NativeApp.h" #include "base/NativeApp.h"
#include "file/vfs.h" #include "file/vfs.h"
#include "file/zip_read.h" #include "file/zip_read.h"
#include "thread/thread.h"
#include "net/http_client.h" #include "net/http_client.h"
#include "gfx_es2/draw_text.h" #include "gfx_es2/draw_text.h"
#include "gfx_es2/gpu_features.h" #include "gfx_es2/gpu_features.h"

View File

@ -15,6 +15,9 @@
// Official git repository and contact information can be found at // Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <algorithm>
#include <thread>
#include "base/timeutil.h" #include "base/timeutil.h"
#include "ext/vjson/json.h" #include "ext/vjson/json.h"
#include "file/fd_util.h" #include "file/fd_util.h"
@ -23,7 +26,6 @@
#include "net/http_server.h" #include "net/http_server.h"
#include "net/resolve.h" #include "net/resolve.h"
#include "net/sinks.h" #include "net/sinks.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"

View File

@ -16,10 +16,10 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <list> #include <list>
#include <thread>
#include <memory> #include <memory>
#include "base/mutex.h" #include "base/mutex.h"
#include "input/input_state.h" #include "input/input_state.h"
#include "thread/thread.h"
#include "thread/threadutil.h" #include "thread/threadutil.h"
#include "Core/Config.h" #include "Core/Config.h"
#include "Core/Host.h" #include "Core/Host.h"

View File

@ -5,8 +5,8 @@
#include "stdafx.h" #include "stdafx.h"
#include <functional> #include <functional>
#include <thread>
#include "thread/thread.h"
#include "util/text/utf8.h" #include "util/text/utf8.h"
#include "ShellUtil.h" #include "ShellUtil.h"
#include "CommDlg.h" #include "CommDlg.h"

View File

@ -2,8 +2,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "thread/thread.h" #include <thread>
namespace W32Util namespace W32Util
{ {

View File

@ -273,7 +273,6 @@
<ClInclude Include="thin3d\thin3d.h" /> <ClInclude Include="thin3d\thin3d.h" />
<ClInclude Include="thread\executor.h" /> <ClInclude Include="thread\executor.h" />
<ClInclude Include="thread\prioritizedworkqueue.h" /> <ClInclude Include="thread\prioritizedworkqueue.h" />
<ClInclude Include="thread\thread.h" />
<ClInclude Include="thread\threadpool.h" /> <ClInclude Include="thread\threadpool.h" />
<ClInclude Include="thread\threadutil.h" /> <ClInclude Include="thread\threadutil.h" />
<ClInclude Include="ui\screen.h" /> <ClInclude Include="ui\screen.h" />

View File

@ -176,9 +176,6 @@
<ClInclude Include="thread\prioritizedworkqueue.h"> <ClInclude Include="thread\prioritizedworkqueue.h">
<Filter>thread</Filter> <Filter>thread</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="thread\thread.h">
<Filter>thread</Filter>
</ClInclude>
<ClInclude Include="thread\threadutil.h"> <ClInclude Include="thread\threadutil.h">
<Filter>thread</Filter> <Filter>thread</Filter>
</ClInclude> </ClInclude>

View File

@ -2,10 +2,10 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <thread>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/buffer.h" #include "base/buffer.h"
#include "thread/thread.h"
#ifdef _WIN32 #ifdef _WIN32
#ifndef NOMINMAX #ifndef NOMINMAX

View File

@ -1,8 +1,8 @@
#include <functional> #include <functional>
#include <thread>
#include "base/logging.h" #include "base/logging.h"
#include "base/timeutil.h" #include "base/timeutil.h"
#include "thread/thread.h"
#include "thread/prioritizedworkqueue.h" #include "thread/prioritizedworkqueue.h"
PrioritizedWorkQueue::~PrioritizedWorkQueue() { PrioritizedWorkQueue::~PrioritizedWorkQueue() {

View File

@ -1,328 +0,0 @@
#ifndef STD_THREAD_H_
#define STD_THREAD_H_
#define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
#define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
#if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ && !defined(__ANDROID__)
// GCC 4.4 provides <thread>
#ifndef _GLIBCXX_USE_SCHED_YIELD
#define _GLIBCXX_USE_SCHED_YIELD
#endif
#include <thread>
#else
// partial std::thread implementation for win32/pthread
#include <algorithm>
#if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
#define USE_RVALUE_REFERENCES
#endif
//#ifdef __APPLE__
//#import <Foundation/NSAutoreleasePool.h>
//#endif
#if defined(_WIN32)
// WIN32
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#if defined(_MSC_VER) && defined(_MT)
// When linking with LIBCMT (the multithreaded C library), Microsoft recommends
// using _beginthreadex instead of CreateThread.
#define USE_BEGINTHREADEX
#include <process.h>
#endif
#ifdef USE_BEGINTHREADEX
#define THREAD_ID unsigned
#define THREAD_RETURN unsigned __stdcall
#else
#define THREAD_ID DWORD
#define THREAD_RETURN DWORD WINAPI
#endif
#define THREAD_HANDLE HANDLE
#else
// PTHREAD
#include <unistd.h>
#ifndef _POSIX_THREADS
#error unsupported platform (no pthreads?)
#endif
#include <pthread.h>
#define THREAD_ID pthread_t
#define THREAD_HANDLE pthread_t
#define THREAD_RETURN void*
#endif
namespace std
{
class thread
{
public:
typedef THREAD_HANDLE native_handle_type;
class id
{
friend class thread;
public:
id() : m_thread(0) {}
id(THREAD_ID _id) : m_thread(_id) {}
bool operator==(const id& rhs) const
{
return m_thread == rhs.m_thread;
}
bool operator!=(const id& rhs) const
{
return !(*this == rhs);
}
bool operator<(const id& rhs) const
{
return m_thread < rhs.m_thread;
}
private:
THREAD_ID m_thread;
};
// no variadic template support in msvc
//template <typename C, typename... A>
//thread(C&& func, A&&... args);
template <typename C>
thread(C func)
{
StartThread(new Func<C>(func));
}
template <typename C, typename A>
thread(C func, A arg)
{
StartThread(new FuncArg<C, A>(func, arg));
}
thread() /*= default;*/ {}
#ifdef USE_RVALUE_REFERENCES
thread(const thread&) /*= delete*/;
thread(thread&& other)
{
#else
thread(const thread& t)
{
// ugly const_cast to get around lack of rvalue references
thread& other = const_cast<thread&>(t);
#endif
swap(other);
}
#ifdef USE_RVALUE_REFERENCES
thread& operator=(const thread&) /*= delete*/;
thread& operator=(thread&& other)
{
#else
thread& operator=(const thread& t)
{
// ugly const_cast to get around lack of rvalue references
thread& other = const_cast<thread&>(t);
#endif
if (joinable())
detach();
swap(other);
return *this;
}
~thread()
{
if (joinable())
detach();
}
bool joinable() const
{
return m_id != id();
}
id get_id() const
{
return m_id;
}
native_handle_type native_handle()
{
#ifdef _WIN32
return m_handle;
#else
return m_id.m_thread;
#endif
}
void join()
{
#ifdef _WIN32
WaitForSingleObject(m_handle, INFINITE);
detach();
#else
pthread_join(m_id.m_thread, NULL);
m_id = id();
#endif
}
void detach()
{
#ifdef _WIN32
CloseHandle(m_handle);
#else
pthread_detach(m_id.m_thread);
#endif
m_id = id();
}
void swap(thread& other)
{
std::swap(m_id, other.m_id);
#ifdef _WIN32
std::swap(m_handle, other.m_handle);
#endif
}
static unsigned hardware_concurrency()
{
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return static_cast<unsigned>(sysinfo.dwNumberOfProcessors);
#else
return 0;
#endif
}
private:
id m_id;
#ifdef _WIN32
native_handle_type m_handle;
#endif
template <typename F>
void StartThread(F* param)
{
#ifdef USE_BEGINTHREADEX
m_handle = (HANDLE)_beginthreadex(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread);
#elif defined(_WIN32)
m_handle = CreateThread(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread);
#else
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 1024 * 1024);
if (pthread_create(&m_id.m_thread, &attr, &RunAndDelete<F>, param))
m_id = id();
#endif
}
template <typename C>
class Func
{
public:
Func(C _func) : func(_func) {}
void Run() { func(); }
private:
// Both Visual Studio 2012 & 2013 need the following ifdef, or else they complain about losing const-volatile qualifiers:
// Error 23 error C3848: expression having type 'const std::_Bind<true,void,void
//(__cdecl *const )(PrioritizedWorkQueue *),PrioritizedWorkQueue *&>' would lose some const-volatile qualifiers in order to call 'void std::_Bind<true,void,void (__cdecl *const )(PrioritizedWorkQueue *),PrioritizedWorkQueue *&>::operator ()<>(void)' (thread\prioritizedworkqueue.cpp)
// Error 24 error C3848: expression having type 'const std::_Bind<true,void,std::_Pmf_wrap<void (__cdecl WorkerThread::* )(void),void,WorkerThread,>,WorkerThread *const >'
// would lose some const-volatile qualifiers in order to call
// 'void std::_Bind<true,void,std::_Pmf_wrap<void (__cdecl WorkerThread::* )(void),void,WorkerThread,>,WorkerThread *const >::operator ()<>(void)' (thread\threadpool.cpp)
// Error 25 error C3848 : expression having type 'const std::_Bind<true,void,std::_Pmf_wrap<void (__cdecl http::Download::* )(std::shared_ptr<http::Download>),void,http::Download,std::shared_ptr<http::Download>>,http::Download *const ,std::shared_ptr<http::Download> &>'
// would lose some const - volatile qualifiers in order to call
// 'void std::_Bind<true,void,std::_Pmf_wrap<void (__cdecl http::Download::* )(std::shared_ptr<http::Download>),void,http::Download,std::shared_ptr<http::Download>>,http::Download *const ,std::shared_ptr<http::Download> &>::operator ()<>(void)' (net\http_client.cpp)
#if _MSC_VER >= 1700
C func;
#else
C const func;
#endif
};
template <typename C, typename A>
class FuncArg
{
public:
FuncArg(C _func, A _arg) : func(_func), arg(_arg) {}
void Run() { func(arg); }
private:
C const func;
A arg;
};
template <typename F>
static THREAD_RETURN RunAndDelete(void* param)
{
#ifdef __APPLE__
// NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#endif
static_cast<F*>(param)->Run();
delete static_cast<F*>(param);
#ifdef __APPLE__
// [pool release];
#endif
return 0;
}
};
namespace this_thread
{
inline void yield()
{
#ifdef _WIN32
SwitchToThread();
#else
sleep(0);
#endif
}
inline thread::id get_id()
{
#ifdef _WIN32
return GetCurrentThreadId();
#else
return pthread_self();
#endif
}
} // namespace this_thread
} // namespace std
#undef USE_RVALUE_REFERENCES
#undef USE_BEGINTHREADEX
#undef THREAD_ID
#undef THREAD_RETURN
#undef THREAD_HANDLE
#endif
#endif

View File

@ -3,8 +3,8 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <thread>
#include "thread.h"
#include "base/mutex.h" #include "base/mutex.h"
// This is the simplest possible worker implementation I can think of // This is the simplest possible worker implementation I can think of