Files
VoxelForge/Standalone/Source/StandaloneWorkerThread.h
T
Fr0zkaandClaude Opus 5.5 303b7b9f24 wip(assembler): round L2 -- the cross-platform hash check can fail; FP contraction changes the world
Clang -ffp-contract=on/fast and GCC's default (fast) all change the density hash on the L1 view;
contraction off reproduces L1 (= Windows S13) at W1/W12. Seed sweep: 48 FloatingIslands seeds and
14 effective SurfaceWorld seeds (34 of 48 fell on a seed-independent cube) match across Clang/GCC
and W1/W12. Standalone gains --seed/--recipe/--render-distance-chunks (viewer + headless) and a
Windows replay script. Linux standalone workers get 32 MiB pthread stacks (TLS is 17.3 MiB/thread),
so no external stack limit is needed. No Core or world-output change; MSVC compile unverified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:18:34 +02:00

97 lines
2.9 KiB
C++

#pragma once
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <system_error>
#include <thread>
#include <type_traits>
#include <utility>
#if defined(__linux__)
#include <pthread.h>
#endif
// Linux's static TLS image is larger than the default pthread stack in this standalone build.
// Give only host-owned generation workers enough stack for that TLS plus normal call frames.
#if defined(__linux__)
class FStandaloneWorkerThread
{
public:
static constexpr std::size_t StackSizeBytes=32u*1024u*1024u;
FStandaloneWorkerThread()=default;
template<class F>
explicit FStandaloneWorkerThread(F&& function)
{
using FTask=std::decay_t<F>;
auto task=std::make_unique<FTask>(std::forward<F>(function));
pthread_attr_t attributes;
int result=pthread_attr_init(&attributes);
if(result!=0)throw std::system_error(result,std::generic_category(),"pthread_attr_init");
result=pthread_attr_setstacksize(&attributes,StackSizeBytes);
if(result==0)
{
result=pthread_create(&thread_,&attributes,&RunTask<FTask>,task.get());
if(result==0)task.release();
}
const int destroyResult=pthread_attr_destroy(&attributes);
if(result!=0)throw std::system_error(result,std::generic_category(),"pthread_create");
if(destroyResult!=0)
{
(void)pthread_join(thread_,nullptr);
throw std::system_error(destroyResult,std::generic_category(),"pthread_attr_destroy");
}
joinable_=true;
}
FStandaloneWorkerThread(const FStandaloneWorkerThread&)=delete;
FStandaloneWorkerThread& operator=(const FStandaloneWorkerThread&)=delete;
FStandaloneWorkerThread(FStandaloneWorkerThread&& other) noexcept
:thread_(other.thread_),joinable_(std::exchange(other.joinable_,false))
{}
FStandaloneWorkerThread& operator=(FStandaloneWorkerThread&& other) noexcept
{
if(this!=&other)
{
if(joinable_)(void)pthread_join(thread_,nullptr);
thread_=other.thread_;
joinable_=std::exchange(other.joinable_,false);
}
return *this;
}
~FStandaloneWorkerThread()
{
if(joinable_)(void)pthread_join(thread_,nullptr);
}
bool joinable()const noexcept{return joinable_;}
void join()
{
if(!joinable_)throw std::system_error(std::make_error_code(std::errc::invalid_argument));
const int result=pthread_join(thread_,nullptr);
if(result!=0)throw std::system_error(result,std::generic_category(),"pthread_join");
joinable_=false;
}
private:
template<class F>
static void* RunTask(void* raw)
{
std::unique_ptr<F> task(static_cast<F*>(raw));
(*task)();
return nullptr;
}
pthread_t thread_{};
bool joinable_=false;
};
#else
using FStandaloneWorkerThread=std::thread;
#endif