Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::tasks::ParkerPool Class Reference

The parkers of one scheduler's non-worker scope creators: slots at stable addresses, allocated in fixed blocks, and a free list under a mutex. More...

#include "donner/base/tasks/ParkerPool.h"

Classes

struct  Slot
 A slot handed out by acquire. More...

Public Member Functions

 ParkerPool (const ParkerPool &)=delete
 ParkerPool (ParkerPool &&)=delete
ParkerPool & operator= (const ParkerPool &)=delete
ParkerPool & operator= (ParkerPool &&)=delete
Slot acquire ()
 Hands out a free slot, adding a block when none is free.
void release (std::size_t index)
 Returns the slot at index to the free list.
void close ()
 Marks the pool closed: no scope of its scheduler is live, so none of its slots is in use.
bool closed () const
 Whether close was called.
std::size_t slotCount () const
 Slots created so far, in use or free.
std::size_t slotsInUse () const
 Slots handed out and not yet returned.

Static Public Member Functions

static Parker * ParkerForThread (std::uint64_t generation, const std::shared_ptr< ParkerPool > &pool)
 The calling thread's parker for the scheduler with generation generation, registering the thread with pool on the first call for that generation.
static std::uint64_t NextGeneration ()
 A unique process-wide generation used for scheduler and adopting-thread identities.
static std::size_t LiveCountForTesting ()
 Pools alive in the process, for tests.

Detailed Description

The parkers of one scheduler's non-worker scope creators: slots at stable addresses, allocated in fixed blocks, and a free list under a mutex.

The scheduler creates its pool and shares it by std::shared_ptr. A thread that creates a scope gets its slot through ParkerForThread, which keeps a thread-local map from the scheduler's generation number (unique for the life of the process, unlike an address) to an entry holding a reference to the pool and the slot. When the thread exits, the map's destructor returns each slot and drops each reference, so a pool outlives every thread that holds one of its slots, and a scheduler destroyed first leaves nothing dangling. The scheduler marks its pool closed once every worker is joined; a thread registering with a scheduler first drops every entry whose pool is closed, so it holds pools only for schedulers that were open at its last registration, plus the one it registers with.

A finisher that read a slot's parker before the slot passed to another thread can still change it; the new owner then sees one spurious wake, which it re-checks.


Class Documentation

◆ donner::tasks::ParkerPool::Slot

struct donner::tasks::ParkerPool::Slot

A slot handed out by acquire.

Class Members
size_t index The slot's index, to pass back to Release.
Parker * parker The slot's parker, at a stable address while the pool lives.

Member Function Documentation

◆ ParkerForThread()

Parker * donner::tasks::ParkerPool::ParkerForThread ( std::uint64_t generation,
const std::shared_ptr< ParkerPool > & pool )
static

The calling thread's parker for the scheduler with generation generation, registering the thread with pool on the first call for that generation.

Registration first drops the thread's entries for closed pools.

The map is a thread-local object, so calling this while the calling thread's own thread-local objects are being destroyed (from another thread-local's destructor, at thread exit) is undefined: the map may already be gone. Creating a TaskScope there is the same call.

Parameters
generationThe scheduler's generation number, from NextGeneration.
poolThe scheduler's pool.

The documentation for this class was generated from the following file: