sun::codegen::intrinsics::ThreadUtils

sun::codegen::intrinsics::ThreadUtils

class · Source (opens in a new tab)

class sun::codegen::intrinsics::ThreadUtils

Utility class for thread-related code generation.

Provides:

  • Futex emitters (via libc syscall(), used by Mutex and futex* intrinsics)
  • Thread structure types (context, handle)
  • Thread trampoline generation (the pthread_create entry point)

This class does NOT depend on CodegenVisitor, allowing it to be used independently for thread-related IR generation.

Public Functions

ThreadUtils

public · function · Source (opens in a new tab)

sun::codegen::intrinsics::ThreadUtils::ThreadUtils(sun::codegen::CodegenContext &ctx, llvm::Module *module)

Binds thread runtime helpers to the current LLVM context and module.

Related: sun::codegen::CodegenContext

emitSyscallFutex

public · function · Source (opens in a new tab)

Value * sun::codegen::intrinsics::ThreadUtils::emitSyscallFutex(llvm::Value *addr, llvm::Value *op, llvm::Value *val)

Low-level futex(2) via libc syscall().

The futex syscall number is per-target data resolved from the module's triple.

param

  • addr: Address of the futex word (i32*).

  • op: Futex operation: FUTEX_WAIT (0) or FUTEX_WAKE (1).

  • val: For WAIT: expected value (blocks if *addr == val). For WAKE: number of waiters to wake (usually 1).

return: 0 on success, -1 on failure (libc convention).

Related: syscall()

emitSyscallFutexWait

public · function · Source (opens in a new tab)

void sun::codegen::intrinsics::ThreadUtils::emitSyscallFutexWait(llvm::Value *addr, llvm::Value *expected)

Blocks until the futex word changes from the expected value.

Note: May return spuriously; caller should re-check condition in a loop.

emitSyscallFutexWake

public · function · Source (opens in a new tab)

void sun::codegen::intrinsics::ThreadUtils::emitSyscallFutexWake(llvm::Value *addr)

Wakes at most one thread waiting on a futex.

getOrCreateThreadTrampoline

public · function · Source (opens in a new tab)

Function * sun::codegen::intrinsics::ThreadUtils::getOrCreateThreadTrampoline(llvm::FunctionType *lambdaFuncType, llvm::StructType *fatType, llvm::Type *resultLLVMType, llvm::StructType *contextType, llvm::StructType *argsType)

Returns (creating on first use per lambda signature) the pthread entry point for a spawned lambda:

ptr __sun_thread_start(ptr context)

The trampoline loads {func, env} from the context, rebuilds the lambda fat pointer, reads the arguments spawn moved into the context, calls the lambda, releases the argument block, stores the result through the context's result slot, and returns null. Memoized by lambda signature and argument layout, since two spawns of same-typed lambdas share one trampoline.

param

  • lambdaFuncType: LLVM signature invoked by the trampoline.

  • fatType: Layout holding the lambda pointer and environment.

  • resultLLVMType: LLVM storage type for the thread result.

  • contextType: The layout of std.thread.ThreadContext, declared in Sun so the trampoline and the standard library cannot drift apart.

  • argsType: The argument block's layout, or null when the lambda takes no arguments.

Private Fields

ctx

private · variable · Source (opens in a new tab)

sun::codegen::CodegenContext& sun::codegen::intrinsics::ThreadUtils::ctx

No documentation comment.

Related: sun::codegen::CodegenContext

module

private · variable · Source (opens in a new tab)

llvm::Module* sun::codegen::intrinsics::ThreadUtils::module

No documentation comment.

trampolineCache

private · variable · Source (opens in a new tab)

std::map<std::string, llvm::Function*> sun::codegen::intrinsics::ThreadUtils::trampolineCache

No documentation comment.