/*** Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: AuSpawnThread.Unix.cpp Date: Author: Reece ***/ #include #include "AuThreads.hpp" #include "AuSpawnThread.hpp" #if defined(AURORA_HAS_PTHREADS) #include #endif namespace Aurora::Threading::Threads { AuPair /*success, oshandle*/ SpawnThread(const AuFunction &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(entrypoint); if (!callbackClone) { return {false, 0}; } static auto OSEP_f = [](void *that) -> void * { auto handle = reinterpret_cast *>(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}; } }