Initial Commit

This commit is contained in:
Norman Lansing
2026-01-28 19:08:51 -05:00
commit ecb33115bf
54042 changed files with 9695586 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
// ©2013 Cameron Desrochers
#include "simplethread.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
struct SimpleThread::ThreadRef
{
HANDLE handle;
static DWORD WINAPI ThreadProc(LPVOID param)
{
auto threadRef = static_cast<ThreadRef*>(param);
threadRef->callbackFunc(threadRef->callbackObj);
return 0;
}
ThreadRef(void* callbackObj, CallbackFunc callbackFunc)
: callbackObj(callbackObj), callbackFunc(callbackFunc)
{
}
void* callbackObj;
CallbackFunc callbackFunc;
};
void SimpleThread::startThread(void* callbackObj, CallbackFunc callbackFunc)
{
thread = new ThreadRef(callbackObj, callbackFunc);
thread->handle = CreateThread(NULL, StackSize, &ThreadRef::ThreadProc, thread, 0, NULL);
}
void SimpleThread::join()
{
if (thread != nullptr && thread->handle != NULL) {
WaitForSingleObject(thread->handle, INFINITE);
CloseHandle(thread->handle);
thread->handle = NULL;
}
}
#else
#include <thread>
struct SimpleThread::ThreadRef
{
std::thread thread;
static void threadProc(ThreadRef* threadRef)
{
threadRef->callbackFunc(threadRef->callbackObj);
}
ThreadRef(void* callbackObj, CallbackFunc callbackFunc)
: callbackObj(callbackObj), callbackFunc(callbackFunc)
{
}
void* callbackObj;
CallbackFunc callbackFunc;
};
void SimpleThread::startThread(void* callbackObj, CallbackFunc callbackFunc)
{
thread = new ThreadRef(callbackObj, callbackFunc);
thread->thread = std::thread(&ThreadRef::threadProc, thread);
}
void SimpleThread::join()
{
if (thread != nullptr && thread->thread.joinable()) {
thread->thread.join();
}
}
#endif
SimpleThread::~SimpleThread()
{
if (thread != nullptr) {
join();
delete thread;
}
}

View File

@@ -0,0 +1,162 @@
// ©2013 Cameron Desrochers
#pragma once
// Like C++11's std::thread, but with a reduced API, and works on Windows with MSVC2010+.
// Wraps std::thread on other OSes. Perhaps the most significant departure between
// std::thread and this mini-library is that join() is called implicitly in the destructor,
// if the thread is joinable. The thread callback functions should not throw exceptions.
#include <utility>
#include <type_traits>
namespace details
{
template<typename TArg1 = void, typename TArg2 = void, typename TArg3 = void>
struct ArgWrapper
{
typename std::remove_reference<TArg1>::type arg1;
typename std::remove_reference<TArg2>::type arg2;
typename std::remove_reference<TArg3>::type arg3;
ArgWrapper(ArgWrapper const& o) : arg1(o.arg1), arg2(o.arg2), arg3(o.arg3) { }
ArgWrapper(ArgWrapper&& o) : arg1(std::move(o.arg1)), arg2(std::move(o.arg2)), arg3(std::move(o.arg3)) { }
template<typename T, typename U, typename V>
ArgWrapper(T&& a1, U&& a2, V&& a3) : arg1(std::forward<T>(a1)), arg2(std::forward<U>(a2)), arg3(std::forward<V>(a3)) { }
template<typename TCallback>
void callCallback(TCallback&& callback) const { std::forward<TCallback>(callback)(std::move(arg1), std::move(arg2), std::move(arg3)); }
};
template<typename TArg1, typename TArg2>
struct ArgWrapper<TArg1, TArg2, void>
{
typename std::remove_reference<TArg1>::type arg1;
typename std::remove_reference<TArg2>::type arg2;
ArgWrapper(ArgWrapper const& o) : arg1(o.arg1), arg2(o.arg2) { }
ArgWrapper(ArgWrapper&& o) : arg1(std::move(o.arg1)), arg2(std::move(o.arg2)) { }
template<typename T, typename U>
ArgWrapper(T&& a1, U&& a2) : arg1(std::forward<T>(a1)), arg2(std::forward<U>(a2)) { }
template<typename TCallback>
void callCallback(TCallback&& callback) const { std::forward<TCallback>(callback)(std::move(arg1), std::move(arg2)); }
};
template<typename TArg1>
struct ArgWrapper<TArg1, void, void>
{
typename std::remove_reference<TArg1>::type arg1;
ArgWrapper(ArgWrapper const& o) : arg1(o.arg1) { }
ArgWrapper(ArgWrapper&& o) : arg1(std::move(o.arg1)) { }
template<typename T>
ArgWrapper(T&& a1) : arg1(std::forward<T>(a1)) { }
template<typename TCallback>
void callCallback(TCallback&& callback) const { std::forward<TCallback>(callback)(std::move(arg1)); }
};
template<> struct ArgWrapper<void, void, void>
{
template<typename TCallback> void callCallback(TCallback&& callback) const { std::forward<TCallback>(callback)(); }
};
}
class SimpleThread
{
private:
struct ThreadRef;
template<typename TCallback, typename TArgs>
struct CallbackWrapper
{
template<typename U>
CallbackWrapper(TCallback&& callback, U&& args)
: callback(std::forward<TCallback>(callback)), args(std::forward<U>(args))
{
}
static void callAndDelete(void* wrapper)
{
auto typedWrapper = static_cast<CallbackWrapper*>(wrapper);
typedWrapper->args.callCallback(std::move(typedWrapper->callback));
delete typedWrapper;
}
typename std::decay<TCallback>::type callback;
TArgs args;
};
typedef void (*CallbackFunc)(void*);
void startThread(void* callbackObj, CallbackFunc callbackFunc);
public:
static const int StackSize = 4 * 1024; // bytes
SimpleThread() : thread(nullptr) { }
SimpleThread(SimpleThread&& other)
: thread(other.thread)
{
other.thread = nullptr;
}
SimpleThread& operator=(SimpleThread&& other)
{
thread = other.thread;
other.thread = nullptr;
return *this;
}
// Disable copying and copy-assignment
private:
SimpleThread(SimpleThread const&);
SimpleThread& operator=(SimpleThread const&);
public:
template<typename TCallback>
explicit SimpleThread(TCallback&& callback)
{
auto wrapper = new CallbackWrapper<TCallback, ::details::ArgWrapper<>>(
std::forward<TCallback>(callback),
::details::ArgWrapper<>()
);
startThread(wrapper, &CallbackWrapper<TCallback, ::details::ArgWrapper<>>::callAndDelete);
}
template<typename TCallback, typename TArg1>
explicit SimpleThread(TCallback&& callback, TArg1&& arg1)
{
auto wrapper = new CallbackWrapper<TCallback, ::details::ArgWrapper<TArg1>>(
std::forward<TCallback>(callback),
::details::ArgWrapper<TArg1>(std::forward<TArg1>(arg1))
);
startThread(wrapper, &CallbackWrapper<TCallback, ::details::ArgWrapper<TArg1>>::callAndDelete);
}
template<typename TCallback, typename TArg1, typename TArg2>
explicit SimpleThread(TCallback&& callback, TArg1&& arg1, TArg2&& arg2)
{
auto wrapper = new CallbackWrapper<TCallback, ::details::ArgWrapper<TArg1, TArg2>>(
std::forward<TCallback>(callback),
::details::ArgWrapper<TArg1, TArg2>(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2))
);
startThread(wrapper, &CallbackWrapper<TCallback, ::details::ArgWrapper<TArg1, TArg2>>::callAndDelete);
}
template<typename TCallback, typename TArg1, typename TArg2, typename TArg3>
explicit SimpleThread(TCallback&& callback, TArg1&& arg1, TArg2&& arg2, TArg3&& arg3)
{
auto wrapper = new CallbackWrapper<TCallback, ::details::ArgWrapper<TArg1, TArg2, TArg3>>(
std::forward<TCallback>(callback),
::details::ArgWrapper<TArg1, TArg2, TArg3>(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2), std::forward<TArg3>(arg3))
);
startThread(wrapper, &CallbackWrapper<TCallback, ::details::ArgWrapper<TArg1, TArg2, TArg3>>::callAndDelete);
}
~SimpleThread();
void join();
private:
ThreadRef* thread;
};

View File

@@ -0,0 +1,144 @@
// ©2013-2014 Cameron Desrochers
#include "systemtime.h"
#include <climits>
#if defined(_MSC_VER) && _MSC_VER < 1700
#include <intrin.h>
#define CompilerMemBar() _ReadWriteBarrier()
#else
#include <atomic>
#define CompilerMemBar() std::atomic_signal_fence(std::memory_order_seq_cst)
#endif
#if defined(ST_WINDOWS)
#include <windows.h>
namespace moodycamel
{
void sleep(int milliseconds)
{
::Sleep(milliseconds);
}
SystemTime getSystemTime()
{
LARGE_INTEGER t;
CompilerMemBar();
if (!QueryPerformanceCounter(&t)) {
return static_cast<SystemTime>(-1);
}
CompilerMemBar();
return static_cast<SystemTime>(t.QuadPart);
}
double getTimeDelta(SystemTime start)
{
LARGE_INTEGER t;
CompilerMemBar();
if (start == static_cast<SystemTime>(-1) || !QueryPerformanceCounter(&t)) {
return -1;
}
CompilerMemBar();
auto now = static_cast<SystemTime>(t.QuadPart);
LARGE_INTEGER f;
if (!QueryPerformanceFrequency(&f)) {
return -1;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
return static_cast<double>(static_cast<__int64>(now - start)) / f.QuadPart * 1000;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
} // end namespace moodycamel
#elif defined(ST_APPLE)
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>
#include <time.h>
namespace moodycamel
{
void sleep(int milliseconds)
{
::usleep(milliseconds * 1000);
}
SystemTime getSystemTime()
{
CompilerMemBar();
std::uint64_t result = mach_absolute_time();
CompilerMemBar();
return result;
}
double getTimeDelta(SystemTime start)
{
CompilerMemBar();
std::uint64_t end = mach_absolute_time();
CompilerMemBar();
mach_timebase_info_data_t tb = { 0 };
mach_timebase_info(&tb);
double toNano = static_cast<double>(tb.numer) / tb.denom;
return static_cast<double>(end - start) * toNano * 0.000001;
}
} // end namespace moodycamel
#elif defined(ST_NIX)
#include <unistd.h>
namespace moodycamel
{
void sleep(int milliseconds)
{
::usleep(milliseconds * 1000);
}
SystemTime getSystemTime()
{
timespec t;
CompilerMemBar();
if (clock_gettime(CLOCK_MONOTONIC_RAW, &t) != 0) {
t.tv_sec = (time_t)-1;
t.tv_nsec = -1;
}
CompilerMemBar();
return t;
}
double getTimeDelta(SystemTime start)
{
timespec t;
CompilerMemBar();
if ((start.tv_sec == (time_t)-1 && start.tv_nsec == -1) || clock_gettime(CLOCK_MONOTONIC_RAW, &t) != 0) {
return -1;
}
CompilerMemBar();
return static_cast<double>(static_cast<long>(t.tv_sec) - static_cast<long>(start.tv_sec)) * 1000 + double(t.tv_nsec - start.tv_nsec) / 1000000;
}
} // end namespace moodycamel
#endif

View File

@@ -0,0 +1,33 @@
// ©2013-2014 Cameron Desrochers
#pragma once
#if defined(_WIN32)
#define ST_WINDOWS
#elif defined(__APPLE__) && defined(__MACH__)
#define ST_APPLE
#elif defined(__linux__) || defined(__FreeBSD__) || defined(BSD)
#define ST_NIX
#else
#error "Unknown platform"
#endif
#if defined(ST_WINDOWS)
namespace moodycamel { typedef unsigned long long SystemTime; }
#elif defined(ST_APPLE)
#include <cstdint>
namespace moodycamel { typedef std::uint64_t SystemTime; }
#elif defined(ST_NIX)
#include <time.h>
namespace moodycamel { typedef timespec SystemTime; }
#endif
namespace moodycamel
{
void sleep(int milliseconds);
SystemTime getSystemTime();
// Returns the delta time, in milliseconds
double getTimeDelta(SystemTime start);
}