Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
WorkerStack.h File Reference

Worker threads with an explicit stack size, and the bounds of the calling thread's stack. More...

#include <pthread.h>
#include <cstddef>
#include <cstdint>
#include <optional>
Include dependency graph for WorkerStack.h:
This graph shows which files directly or indirectly include this file:

Classes

struct  donner::tasks::StackBounds
 The stack of one thread, which grows down on every platform the scheduler builds for: the lowest usable address (the floor) and the size above it. More...

Namespaces

namespace  donner
 Top-level Donner namespace, which is split into different sub-namespaces such as donner::svg and donner::css.

Typedefs

using donner::tasks::WorkerThreadCreate = int (*)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)
 Thread creation entry point used by the scheduler and its failure-injection tests.

Functions

StackBounds donner::tasks::CurrentThreadStackBounds ()
 The calling thread's stack bounds, as the platform reports them: pthread_get_stackaddr_np and pthread_get_stacksize_np on Apple platforms, pthread_getattr_np elsewhere.
std::optional< StackBounds > donner::tasks::TryCurrentThreadStackBounds ()
 Try to measure the calling thread's stack without asserting on a platform query failure.
std::uintptr_t donner::tasks::CurrentStackPosition ()
 The calling frame's position on the real stack.
constexpr std::size_t donner::tasks::StackRemaining (std::uintptr_t position, const StackBounds &bounds)
 Bytes of bounds' stack left below position: zero at or below the floor, never negative.
std::size_t donner::tasks::WorkerStackSize (std::size_t bytes)
 The stack size a worker is created with for a requested bytes: rounded up to whole pages.
constexpr StackBounds donner::tasks::UsableStack (const StackBounds &platform, std::size_t usableBytes)
 The part of a thread's stack a worker uses: the top usableBytes of platform's bounds, or all of them if the platform's stack is smaller.
std::optional< pthread_t > donner::tasks::TryStartThreadWithStack (std::size_t bytes, void *(*entry)(void *), void *argument, WorkerThreadCreate create)
 Attempts to start a thread with an explicit stack size.
pthread_t donner::tasks::StartThreadWithStack (std::size_t bytes, void *(*entry)(void *), void *argument)
 Starts a thread running entry(argument) on a stack of WorkerStackSize(bytes) bytes.

Variables

constexpr std::size_t donner::tasks::kStackReserveBytes = 32 * 1024
 Stack kept free below every task a worker runs: a worker that starts a task with less than this left fails a release assertion instead of overflowing. It covers the assertion's own reporting and the sanitizer runtimes' use of the stack.

Detailed Description

Worker threads with an explicit stack size, and the bounds of the calling thread's stack.

Function Documentation

◆ CurrentStackPosition()

std::uintptr_t donner::tasks::CurrentStackPosition ( )
inline

The calling frame's position on the real stack.

Under AddressSanitizer with its fake stack, the addresses of locals can lie on the heap; the frame address never does.

Unsupported on an alternate signal stack: in a handler installed with SA_ONSTACK, the frame is on that stack, not the thread's, so a stack-left measurement taken there is meaningless.

◆ CurrentThreadStackBounds()

StackBounds donner::tasks::CurrentThreadStackBounds ( )

The calling thread's stack bounds, as the platform reports them: pthread_get_stackaddr_np and pthread_get_stacksize_np on Apple platforms, pthread_getattr_np elsewhere.

Intended for threads created with an explicit stack size; the main thread's bounds depend on the platform's resource limits.

◆ StackRemaining()

std::size_t donner::tasks::StackRemaining ( std::uintptr_t position,
const StackBounds & bounds )
constexpr

Bytes of bounds' stack left below position: zero at or below the floor, never negative.

Parameters
positionA position on the stack, from CurrentStackPosition.
boundsThe stack's bounds.

◆ StartThreadWithStack()

pthread_t donner::tasks::StartThreadWithStack ( std::size_t bytes,
void *(* entry )(void *),
void * argument )

Starts a thread running entry(argument) on a stack of WorkerStackSize(bytes) bytes.

Fails a release assertion if the thread cannot be created, as a std::thread built without exceptions ends the process.

Returns
The thread, to pass to pthread_join.

◆ TryStartThreadWithStack()

std::optional< pthread_t > donner::tasks::TryStartThreadWithStack ( std::size_t bytes,
void *(* entry )(void *),
void * argument,
WorkerThreadCreate create )

Attempts to start a thread with an explicit stack size.

Invalid sizes fail a release assertion.

Parameters
bytesRequested stack size.
entryThread entry point.
argumentArgument passed to the entry point.
createThread creation function.
Returns
A joinable handle, or empty when the platform refused to start the thread.

◆ UsableStack()

StackBounds donner::tasks::UsableStack ( const StackBounds & platform,
std::size_t usableBytes )
constexpr

The part of a thread's stack a worker uses: the top usableBytes of platform's bounds, or all of them if the platform's stack is smaller.

A platform can hand a new thread a larger stack than it asked for (glibc reuses a cached stack up to four times the requested size), so the configured size, not the platform's, is what a worker counts as its stack.

Parameters
platformThe thread's bounds, from CurrentThreadStackBounds.
usableBytesThe configured stack size, from WorkerStackSize.

◆ WorkerStackSize()

std::size_t donner::tasks::WorkerStackSize ( std::size_t bytes)

The stack size a worker is created with for a requested bytes: rounded up to whole pages.

Fails a release assertion when bytes is below the platform's minimum thread stack or does not leave room above kStackReserveBytes.