AuroraRuntime/Source/Threading/Threads/AuSpawnThread.Unix.cpp

70 lines
1.8 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuSpawnThread.Unix.cpp
Date:
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuThreads.hpp"
#include "AuSpawnThread.hpp"
#if defined(AURORA_HAS_PTHREADS)
#include <sched.h>
#endif
namespace Aurora::Threading::Threads
{
AuPair<bool, AuUInt> /*success, oshandle*/ SpawnThread(const AuFunction<void()> &entrypoint, const AuString &debugString, AuUInt32 staskSize)
{
pthread_attr_t tattr;
pthread_t handle;
if (!entrypoint)
{
return {false, 0};
}
// i dont like this allocate
auto callbackClone = _new AuFunction<void()>(entrypoint);
if (!callbackClone)
{
return {false, 0};
}
static auto OSEP_f = [](void *that) -> void *
{
auto handle = reinterpret_cast<AuFunction<void()> *>(that);
auto callMe = *handle;
delete handle;
callMe();
return nullptr;
};
auto ret = pthread_attr_init(&tattr);
if (ret != 0)
{
SysPushErrorGen("Couldn't create thread: {}", debugString);
return {false, 0};
}
if (staskSize)
{
ret = pthread_attr_setstacksize(&tattr, AuMax(AuUInt32(PTHREAD_STACK_MIN), AuUInt32(staskSize)));
if (ret != 0)
{
SysPushErrorGen("Couldn't create thread: {}", debugString);
return {false, 0};
}
}
ret = pthread_create(&handle, &tattr, OSEP_f, callbackClone);
if (ret != 0)
{
SysPushErrorGen("Couldn't create thread: {}", debugString);
return {false, 0};
}
return {true, handle};
}
}