ahead_port

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectWithPort

func ConnectWithPort(port *Port, upstream, downstream interface{})

ConnectWithPort uses an existing port to connect two components. This is useful when you need to create the port separately for monitoring.

Types

type ComponentSync

type ComponentSync struct {
	// contains filtered or unexported fields
}

ComponentSync provides shared synchronization logic for components. It manages both "done" and "ready" states with efficient atomic operations and channel-based notifications optimized for a single waiter.

func NewComponentSync

func NewComponentSync() *ComponentSync

NewComponentSync creates a new ComponentSync instance.

func (*ComponentSync) GetDone

func (cs *ComponentSync) GetDone() int

GetDone gets the component's done state.

func (*ComponentSync) InitReady

func (cs *ComponentSync) InitReady(limit int)

InitReady initializes ready state for the first 'limit' cycles.

func (*ComponentSync) IsReadyNonBlocking

func (cs *ComponentSync) IsReadyNonBlocking(cycle int) (bool, bool)

IsReadyNonBlocking checks ready state without blocking.

func (*ComponentSync) Ready

func (cs *ComponentSync) Ready(cycle int) bool

Ready checks if the component is ready to receive data for the given cycle. This method blocks if the ready state hasn't been decided yet.

func (*ComponentSync) SetDone

func (cs *ComponentSync) SetDone(cycle int)

SetDone sets the component's done state.

func (*ComponentSync) SetReadyUntil

func (cs *ComponentSync) SetReadyUntil(cycle int)

SetReadyUntil sets readyUntil atomically.

func (*ComponentSync) UpdateReady

func (cs *ComponentSync) UpdateReady(cycle int, ready bool)

UpdateReady updates the component's ready state.

func (*ComponentSync) WaitDone

func (cs *ComponentSync) WaitDone(targetCycle int)

WaitDone waits for the component to complete targetCycle.

type InPort

type InPort interface {

	// TrySend attempts to send a packet to the downstream component.
	// This blocks until the downstream component decides its ready state.
	// Returns true if the packet was sent successfully, false if downstream is declared not ready.
	TrySend(cycle int, pkt PacketWithCycle) bool

	// MarkDone marks that the upstream component has completed the specified cycle.
	// This allows the downstream component to safely read data for this cycle.
	MarkDone(cycle int)

	// PeekReady checks if the downstream component is ready without blocking.
	// Returns (ready, decided).
	PeekReady(cycle int) (ready bool, decided bool)

	// IsReady blocks until the downstream component has decided its ready state.
	// Most components should prefer using TrySend directly.
	IsReady(cycle int) bool
}

InPort represents the upstream view of a port (sender's perspective). Upstream components use this interface to send data to downstream components.

Recommended Workflow (Standard API):

  1. Check if ready and send data using TrySend().
  2. After sending all data for the cycle, call MarkDone().

type MockDownstream

type MockDownstream struct {
	// contains filtered or unexported fields
}

MockDownstream is a simple downstream component for testing. It can receive packets and update ready status.

func (*MockDownstream) ReceivePackets

func (m *MockDownstream) ReceivePackets(cycle int) []packet.Packet

ReceivePackets receives packets for a specific cycle.

func (*MockDownstream) SetUpstreamPort

func (m *MockDownstream) SetUpstreamPort(port OutPort)

SetUpstreamPort sets the upstream port.

func (*MockDownstream) UpdateReady

func (m *MockDownstream) UpdateReady(cycle int, ready bool)

UpdateReady updates the ready status for a specific cycle.

func (*MockDownstream) WaitDone

func (m *MockDownstream) WaitDone(cycle int)

WaitDone waits for the upstream to complete a cycle.

type MockUpstream

type MockUpstream struct {
	// contains filtered or unexported fields
}

MockUpstream is a simple upstream component for testing. It can send packets and mark cycles as done.

func (*MockUpstream) MarkDone

func (m *MockUpstream) MarkDone(cycle int)

MarkDone marks a cycle as done.

func (*MockUpstream) SendPacket

func (m *MockUpstream) SendPacket(cycle int, pkt packet.Packet) bool

SendPacket sends a packet to the downstream component. This uses the blocking TrySend method.

func (*MockUpstream) SetDownstreamPort

func (m *MockUpstream) SetDownstreamPort(port InPort)

SetDownstreamPort sets the downstream port.

func (*MockUpstream) TryPeekSendPacket

func (m *MockUpstream) TryPeekSendPacket(cycle int, pkt packet.Packet) bool

TryPeekSendPacket attempts to send a packet using non-blocking check.

type OutPort

type OutPort interface {

	// Receive retrieves all packets for the specified cycle from the upstream component.
	// This is a blocking call that ensures all packets for the cycle are collected.
	// Note: It internally handles the wait for upstream to be done (WaitDone).
	Receive(cycle int) []packet.Packet

	// UpdateReady updates the downstream component's ready state for the given cycle.
	// This informs the upstream whether it's ready to receive data for the specific cycle.
	UpdateReady(cycle int, ready bool)

	// WaitDone blocks until the upstream component has completed the specified cycle.
	// Note: Receive() already calls this internally.
	WaitDone(cycle int)

	// PeekDone returns the highest cycle that the upstream component has completed.
	// This is a non-blocking query method.
	PeekDone() int
}

OutPort represents the downstream view of a port (receiver's perspective). Downstream components use this interface to receive data from upstream components.

Recommended Workflow (Standard API):

  1. Retrieve all packets for the cycle using Receive().
  2. Determine subsequent readiness and call UpdateReady().

type Packet

type Packet = packet.Packet

type PacketWithCycle

type PacketWithCycle = packet.PacketWithCycle

PacketWithCycle represents a packet with its associated cycle.

type Port

type Port struct {
	// contains filtered or unexported fields
}

Port is a unified port implementation that acts as a connection between two components. It implements both InPort and OutPort interfaces, providing different views for upstream and downstream components.

Design principles: - Port is an independent entity, not owned by any component - One port instance per connection (not two) - Type safety through interface views (AsInPort/AsOutPort) - Synchronization logic centralized in Port

func Connect

func Connect(upstream, downstream interface{}) *Port

Connect creates a Port and connects two components. The upstream component will receive the InPort view (for sending data). The downstream component will receive the OutPort view (for receiving data).

Usage:

port := Connect(outputQueue, link)
// outputQueue.toDownstream is now set to port.AsInPort()
// link.fromUpstream is now set to port.AsOutPort()

Returns the created Port for monitoring/debugging if needed.

func NewPort

func NewPort() *Port

NewPort creates a new port instance.

func (*Port) AsInPort

func (p *Port) AsInPort() InPort

AsInPort returns the InPort view of this port. This should be used by upstream components that send data.

func (*Port) AsOutPort

func (p *Port) AsOutPort() OutPort

AsOutPort returns the OutPort view of this port. This should be used by downstream components that receive data.

func (*Port) IsReady

func (p *Port) IsReady(cycle int) bool

IsReady blocks until the downstream component has decided its ready state for the given cycle.

func (*Port) MarkDone

func (p *Port) MarkDone(cycle int)

MarkDone marks that the upstream component has completed the specified cycle.

func (*Port) PeekDone

func (p *Port) PeekDone() int

PeekDone returns the highest cycle that the upstream component has completed.

func (*Port) PeekReady

func (p *Port) PeekReady(cycle int) (bool, bool)

PeekReady checks if the downstream component is ready to receive data for the given cycle.

func (*Port) Receive

func (p *Port) Receive(cycle int) []packet.Packet

Receive retrieves all packets for the specified cycle from the upstream component. This is now a blocking call that ensures all packets for the cycle are collected. Receive retrieves all packets for the specified cycle from the upstream component. This is now a blocking call that ensures all packets for the cycle are collected.

func (*Port) TrySend

func (p *Port) TrySend(cycle int, pkt PacketWithCycle) bool

TrySend attempts to send a packet to the downstream component. This blocks until the downstream component decides its ready state. Returns true if the packet was sent successfully, false if downstream is declared not ready.

func (*Port) UpdateReady

func (p *Port) UpdateReady(cycle int, ready bool)

UpdateReady updates the downstream component's ready state for the given cycle.

func (*Port) WaitDone

func (p *Port) WaitDone(cycle int)

WaitDone blocks until the upstream component has completed the specified cycle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL