notification.rs

Overview

This file defines a Notification struct that provides a simple mechanism to signal and wait for notifications or updates within a concurrent environment. It uses atomic counters and synchronization primitives to allow multiple threads or asynchronous tasks to coordinate on changes or events signaled by instances of Notification.

The main purpose of this file is to manage notification state with minimal overhead and provide blocking wait functionality with optional timeouts, facilitating coordination between threads or components that require update awareness.

Struct: Notification

The Notification struct encapsulates a unique identifier and a shared notification state protected by synchronization primitives. It is designed to be cloned safely and used in a multithreaded context.

Fields

Implementation Details

Important Notes

Methods

fn new() -> Self

Creates a new Notification instance.

fn id(&self) -> u32

Returns the unique identifier of the Notification.

fn touch(&mut self)

Signals an update by incrementing the internal notification counter and notifying all threads waiting on the condition variable.

fn stamp(&self) -> u32

Returns the current notification counter value (stamp).

fn wait_for_updates(&mut self, stamp: u32)

Blocks the current thread until the notification counter changes from the given stamp.

fn wait_for_updates_timeout(&mut self, stamp: u32, timeout: Duration)

Blocks the current thread until the notification counter changes from the given stamp or the specified timeout elapses.

Interaction with Other Parts of the System

Implementation Details and Algorithms

Diagram

classDiagram
class Notification {
-id: u32
-notifications: Arc<(Mutex<u32>, Condvar)>
+new()
+id() u32
+touch()
+stamp() u32
+wait_for_updates(stamp: u32)
+wait_for_updates_timeout(stamp: u32, timeout: Duration)
}
Notification ..> AtomicU32 : uses for ID
Notification ..> Mutex : uses for notification counter
Notification ..> Condvar : uses for waiting/notification

This diagram illustrates the Notification struct's components and its relationships with synchronization primitives and atomic counters. The methods provide creation, identification, signaling, and waiting functionalities.