boost::capy::execution_context

Registers, looks up, and shuts down service objects owned by a derived context.

Synopsis

class execution_context;

Description

An execution context represents a place where function objects are executed. It provides a service registry where polymorphic services can be stored and retrieved by type. Each service type may be stored at most once. Services may specify a nested key_type to enable lookup by a base class type.

Derived classes such as io_context extend this to provide execution facilities like event loops and thread pools. Derived class destructors must call shutdown() and destroy() to ensure proper service cleanup before member destruction. Between the two, a context that queues work abandons what is still queued, including work posted while it does so. A service created by an unwound chain is shut down as it is created, so its work is taken too. Services do not post from their destructors. A context may drain once more after destroy(), so that stray work is still destroyed, but no service may be created after destroy() returns.

Service Lifecycle

Services are created on first use via use_service() or explicitly via make_service(). During destruction, shutdown() is called on each service in reverse order of creation, then destroy() deletes them. Both functions are idempotent.

Thread Safety

Service registration and lookup functions are thread‐safe. The shutdown() and destroy() functions are not thread‐safe and must only be called during destruction.

Example

struct file_service : capy::execution_context::service
{
protected:
    void shutdown() override {}
};

struct posix_file_service : file_service
{
    using key_type = file_service;

    explicit posix_file_service(capy::execution_context&) {}
};

class io_context : public capy::execution_context
{
public:
    ~io_context()
    {
        shutdown();
        destroy();
    }
};

void configure_services(io_context& ctx)
{
    ctx.make_service<posix_file_service>();
    ctx.find_service<file_service>();       // returns posix_file_service*
    ctx.find_service<posix_file_service>(); // also works
}

Types

Name

Description

service

Gives a derived service a shutdown() hook, run when its owning execution_context is destroyed.

Member Functions

Name

Description

execution_context [constructor]

Constructors

~execution_context [destructor]

Destructor.

operator= [deleted]

Copy assignment is disabled; a context owns its services.

find_service

Return a pointer to the service of type T, or nullptr.

get_frame_allocator

Return the memory resource used for coroutine frame allocation.

has_service

Return true if a service of type T exists.

make_service

Construct and add a service.

set_frame_allocator

set_frame_allocator overloads

target

Return a pointer to this context if it matches the requested type.

use_service

Return a reference to the service of type T, creating it if needed.

Protected Member Functions

Name

Description

execution_context [constructor]

Construct from the most‐derived context type.

destroy

Destroy all services.

shutdown

Shut down all services.

Derived Classes

Name

Description

blocking_context

Runs a work queue and event loop on the calling thread until the task completes.

thread_pool

Distributes posted work across a fixed group of worker threads via a shared queue.

See Also