2022-01-24 18:37:06 +00:00
|
|
|
/***
|
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:11:19 +00:00
|
|
|
static auto OSEP_f = [](void *that) -> void *
|
2022-01-24 18:37:06 +00:00
|
|
|
{
|
|
|
|
auto handle = reinterpret_cast<AuFunction<void()> *>(that);
|
|
|
|
auto callMe = *handle;
|
|
|
|
delete handle;
|
|
|
|
callMe();
|
2022-04-09 15:53:14 +00:00
|
|
|
return nullptr;
|
2022-01-24 18:37:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto ret = pthread_attr_init(&tattr);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorGen("Couldn't create thread: {}", debugString);
|
|
|
|
return {false, 0};
|
|
|
|
}
|
|
|
|
|
2022-04-05 02:36:39 +00:00
|
|
|
if (staskSize)
|
2022-01-24 18:37:06 +00:00
|
|
|
{
|
2022-04-05 02:36:39 +00:00
|
|
|
ret = pthread_attr_setstacksize(&tattr, AuMax(AuUInt32(PTHREAD_STACK_MIN), AuUInt32(staskSize)));
|
2022-01-24 18:37:06 +00:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
}
|