subscribers.rs

Overview

This file defines a Subscriber trait for handling thread lifecycle events related to blocks and threads within a concurrency or parallelism framework. It provides an abstraction for reacting to thread start and stop events via two key methods. Additionally, the file implements a macro to automatically generate Subscriber trait implementations for tuples of multiple Subscriber objects, enabling simultaneous notification of several subscribers.

The core purpose is to facilitate event-driven handling of thread operations by decoupling the event producer from the event consumers (subscribers). This allows multiple subscribers to be notified of thread lifecycle changes in a composable and scalable manner.

Trait: Subscriber

Description

The Subscriber trait defines an interface for receiving notifications when threads start and stop. It abstracts over any type capable of handling these events, enabling flexible subscriber implementations.

Methods

handle_start_thread

fn handle_start_thread(
    &mut self,
    parent_split_block: &BlockIdentifier,
    thread_identifier: &ThreadIdentifier,
    threads_table: Option<ThreadsTable>,
);
impl Subscriber for Logger {
    fn handle_start_thread(
        &mut self,
        parent_split_block: &BlockIdentifier,
        thread_identifier: &ThreadIdentifier,
        threads_table: Option<ThreadsTable>,
    ) {
        // Log the start of a thread
        self.log(format!("Thread {:?} started from block {:?}", thread_identifier, parent_split_block));
    }
    // ...
}

handle_stop_thread

fn handle_stop_thread(
    &mut self,
    last_thread_block: &BlockIdentifier,
    thread_identifier: &ThreadIdentifier,
);
impl Subscriber for Logger {
    fn handle_stop_thread(
        &mut self,
        last_thread_block: &BlockIdentifier,
        thread_identifier: &ThreadIdentifier,
    ) {
        // Log the stop of a thread
        self.log(format!("Thread {:?} stopped at block {:?}", thread_identifier, last_thread_block));
    }
}

Macro: tuple_subs!

Description

The macro tuple_subs! provides a convenient way to implement the Subscriber trait for tuple types containing multiple mutable references to subscriber instances. This allows a single call to a subscriber tuple to broadcast events to all contained subscribers.

The macro accepts pairs of tuple indices and generic type identifiers, generating an implementation of Subscriber for tuples up to 5 elements in size (as instantiated below the macro). Each subscriber in the tuple receives the same event notifications with cloned threads_table where applicable.

Generated Implementation Details

Instantiations in this file

tuple_subs!(0 A);
tuple_subs!(0 A, 1 B);
tuple_subs!(0 A, 1 B, 2 C);
tuple_subs!(0 A, 1 B, 2 C, 3 D);
tuple_subs!(0 A, 1 B, 2 C, 3 D, 4 E);

These lines create Subscriber implementations for tuples with 1 to 5 mutable subscribers.

Usage example

let mut subscriber1 = Logger::new();
let mut subscriber2 = MetricsCollector::new();

let mut combined_subscribers = (&mut subscriber1, &mut subscriber2);

combined_subscribers.handle_start_thread(&block_id, &thread_id, Some(threads_table));
combined_subscribers.handle_stop_thread(&block_id, &thread_id);

In this example, both subscriber1 and subscriber2 receive the thread lifecycle events via the tuple implementation.

Important Implementation Details

Interaction with Other Parts of the System

Structure Diagram

classDiagram
class Subscriber {
<<trait>>
+handle_start_thread()
+handle_stop_thread()
}
class tuple_subs {
<<macro>>
}
Subscriber <|.. tuple_subs

This diagram shows: