Rust · Threads and Polling

Threads and Polling

use ucall::Polling;use ucall::CPUClass; 1.0.0-rc.1 · 60202332

The crate starts no threads and needs no async runtime: every callback runs on a thread the application lent to a polling call.

  • A route handler runs on the thread inside take_call or take_calls for the event loop that received the request.
  • A batch handler runs on the thread inside take_batch or take_batches for its endpoint, never on an event loop.
  • A server’s peer authorizer runs on the polling thread completing the handshake, and a client’s on the client’s own thread inside take_completed, call, get or post.

Each event loop and each batch endpoint has one polling owner at a time, and a second thread polling it concurrently gets Status::Contended. take_calls and take_batches run until Server::stop, while take_call and take_batch return after one round, waiting up to their timeout, so a loop of them interleaves polling with other work on the same thread. Server::stop works from any thread, handlers included, wakes every polling thread, and dropping the Server stops it too. The first stop drains, answering every request already held before its connection closes, and a second one cuts the replies still in flight. An interface accepts on its own number of event loops, one by default, and the server runs the largest of those counts.

Registration takes &mut Server, and polling, stopping and statistics take &Server, so the borrow checker orders setup before serving. A Request and a Response move between threads, while a Client stays on the thread that created it. Every thread that polls, replies or reads must run on a CPU supporting the CPU class the server or client resolved at start, so CPUClass::Serial is the safe choice on machines mixing CPU generations.

API

EnumPollingWhat Server::take_call found of its event loop.
server.rs:667 ↗
pub enum Polling

Values

ServingThe loop serves, or drains what it holds after a stop.
RetiredThe loop drained or cut everything after Server::stop, so a caller polling in slices stops.

Implements

Clone, Copy, StructuralPartialEq, PartialEq, Eq, Debug
EnumCPUClassThe CPU kernels a server or client runs, from the portable Serial up, where None takes what the constructing thread's CPU offers.
types.rs:753 ↗
pub enum CPUClass

A class is a ceiling: the newest compiled class under it runs, failing with Status::Unsupported where the CPU does not run it. Every thread that later polls, replies, or reads must run on a CPU supporting the class resolved at construction, and Serial is the conservative choice where they differ. Prints and parses its stable name, like "intel-icelake".

Values

Serial1Portable kernels that run on any CPU.
IntelHaswell2x86 cores from Intel Haswell on.
IntelSkylake3x86 server cores from Intel Skylake on.
IntelIcelake4x86 server cores from Intel Ice Lake on.
IntelAlder5x86 hybrid cores from Intel Alder Lake on.
AWSG26Arm cores from AWS Graviton 2 on.
AWSG57Arm server cores from AWS Graviton 5 on.
Monoversioned8The one class of a monoversioned build, with every capability it compiled.

Implements

Clone, Copy, StructuralPartialEq, PartialEq, Eq, Debug, Hash, Display, FromStr