Action Engine
Loading...
Searching...
No Matches
select.h File Reference

Detailed Description

Provides the Select and SelectUntil functions for selecting a fiber to proceed if one of several cases is ready.

This file defines the thread::Select and thread::SelectUntil functions, which allow a fiber to wait for (block until) one of several cases happens to proceed, either until a specified deadline or indefinitely.

#include <absl/log/check.h>
#include "thread/cases.h"

Go to the source code of this file.

Functions

int thread::SelectUntil (absl::Time deadline, const CaseArray &cases)
 Returns the index of the first case that is ready, or -1 if the deadline has expired without any case becoming ready.
 
int thread::Select (const CaseArray &cases)
 Returns the index of the first case that is ready, blocking until one is.
 

Function Documentation

◆ Select()

int thread::Select ( const CaseArray & cases)
inline

Returns the index of the first case that is ready, blocking until one is.

This is equivalent to calling SelectUntil with an infinite deadline.

Parameters
casesThe array of cases to select from. Usually passed as an initializer list.
Returns
The index of the first case that is ready, or -1 if no case is ready.

◆ SelectUntil()

int thread::SelectUntil ( absl::Time deadline,
const CaseArray & cases )

Returns the index of the first case that is ready, or -1 if the deadline has expired without any case becoming ready.

Example:

// Try to write against the channel "c", waiting for up to 1ms when there is
// no space immediately available. Returns true if the write was
// successfully enqueued, false otherwise.
bool TryPut(thread::Channel<int>* c, int v) {
return thread::SelectUntil(
absl::Now() + absl::Milliseconds(1),
{c->writer()->OnWrite(v)}) == 0;
}
A Channel is a bounded FIFO queue that allows multiple readers and writers to communicate with each o...
Definition channel.h:278
Writer< T > *absl_nonnull writer()
Definition channel.h:324
Note
If a Case transitions into a ready state after the deadline has expired, but before SelectUntil returns, the return value is not guaranteed to be -1 and may instead be the index of that case. However, if -1 is returned, it is guaranteed that no case has proceeded.
Parameters
deadlineThe absolute time until which to wait for a case to become ready.
casesThe array of cases to select from. Usually passed as an initializer list.
Returns
The index of the first case that is ready, or -1 if the deadline has expired.