Files
ares/nall/thread.cpp
T
invertego e1e11cf75e nall: split implementation into .cpp files
Begin the process of splitting nall into separate translation units.
It can now be used in two ways:

1) as a header-only library by defining NALL_HEADER_ONLY
2) by compiling nall/nall.cpp

sourcery has been updated to demonstrate the header-only use and
everything else now builds nall.cpp.

So far the only things moved into .cpp files are implementations that
call Windows APIs. This allows most of ares to build without including
windows.h and enables the removal of the abomination that was
windows/guard.hpp. The things that actually need windows.h (hiro, ruby)
must avoid using names reserved by Windows headers, but the rest of ares
is free to use names like "far", "boolean", and "interface" with
reckless abandon.

This work is done in preparation for building against MSVC and Windows
SDK headers/libraries an an alternative to MinGW-w64.
2022-12-18 09:18:52 +00:00

54 lines
1.2 KiB
C++

#include <nall/thread.hpp>
namespace nall {
#if defined(API_WINDOWS)
NALL_HEADER_INLINE auto WINAPI _threadCallback(void* parameter) -> DWORD {
auto context = (thread::context*)parameter;
context->callback(context->parameter);
delete context;
return 0;
}
NALL_HEADER_INLINE auto thread::close() -> void {
if(handle) {
CloseHandle(handle);
handle = 0;
}
}
NALL_HEADER_INLINE auto thread::join() -> void {
if(handle) {
//wait until the thread has finished executing ...
WaitForSingleObject(handle, INFINITE);
CloseHandle(handle);
handle = 0;
}
}
NALL_HEADER_INLINE auto thread::create(const function<void (uintptr)>& callback, uintptr parameter, u32 stacksize) -> thread {
thread instance;
auto context = new thread::context;
context->callback = callback;
context->parameter = parameter;
instance.handle = CreateThread(nullptr, stacksize, _threadCallback, (void*)context, 0, nullptr);
return instance;
}
NALL_HEADER_INLINE auto thread::detach() -> void {
//Windows threads do not use this concept:
//~thread() frees resources via CloseHandle()
//thread continues to run even after handle is closed
}
NALL_HEADER_INLINE auto thread::exit() -> void {
ExitThread(0);
}
#endif
}