76 lines
2.0 KiB
C++
76 lines
2.0 KiB
C++
|
/***
|
||
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: SpawnThread.Unix.cpp
|
||
|
Date:
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#include <Source/RuntimeInternal.hpp>
|
||
|
#include "Threads.hpp"
|
||
|
#include "SpawnThread.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 callVoidPtr_f = [](void *that) -> void
|
||
|
{
|
||
|
auto handle = reinterpret_cast<AuFunction<void()> *>(that);
|
||
|
auto callMe = *handle;
|
||
|
delete handle;
|
||
|
callMe();
|
||
|
};
|
||
|
|
||
|
void *(*OSEP_f)(void *) = [](void *that) -> void *
|
||
|
{
|
||
|
auto thiz = reinterpret_cast<OSThread *>(that);
|
||
|
thiz->_ThreadEP();
|
||
|
return nullptr;
|
||
|
};
|
||
|
|
||
|
auto ret = pthread_attr_init(&tattr);
|
||
|
if (ret != 0)
|
||
|
{
|
||
|
SysPushErrorGen("Couldn't create thread: {}", debugString);
|
||
|
return {false, 0};
|
||
|
}
|
||
|
|
||
|
if (info_.stackSize)
|
||
|
{
|
||
|
ret = pthread_attr_setstacksize(&tattr, AuMin(AuUInt32(PTHREAD_STACK_MIN), info_.stackSize));
|
||
|
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};
|
||
|
}
|
||
|
}
|