# Threads and Polling

> The package starts no goroutines: every callback runs on a goroutine the application gave to a polling call, which stays on one thread for that call.

`import "github.com/unum-cloud/ucall"` 1.0.0-rc.1 · b7577426

The package starts no goroutines: every callback runs on a goroutine the application gave to a polling call, which stays on one thread for that call.

- A route handler runs on the goroutine inside `TakeCall` or `TakeCalls` for the event loop that received the request.
- A `PooledBatch` handler runs on the goroutine inside `TakeBatch` or `TakeBatches` for its endpoint, never on an event loop, while a `LocalBatch` handler runs inside `TakeCall` or `TakeCalls` for the loop whose requests it holds.
- A server’s peer authorizer runs on the polling goroutine completing the handshake, and a client’s on the goroutine inside `TakeCompleted` or a blocking call.

Each event loop and each batch endpoint has one polling owner at a time. `TakeCalls` and `TakeBatches` run until `Server.Stop`, while `TakeCall` and `TakeBatch` return after one round, waiting up to their timeout, so a loop of them interleaves polling with other work; `TakeCall` answers `PollingRetired` once the loop drained after a stop. `Server.Stop` works from any goroutine, handlers included, and wakes every polling goroutine; `Server.Close` frees the server once they returned. The first stop drains, answering every request already held before its connection closes, and a second one cuts the replies still in flight. A `Request` and a `Response` move between goroutines, while a `Client` is used by one goroutine at a time. Every thread that polls, replies or reads must run on a CPU supporting the CPU class the server or client resolved at start, so `CPUClassSerial` is the safe choice on machines mixing CPU generations.

## API

### Types

#### `Polling`

Polling is what Server.TakeCall found of its event loop.

[server.go:372 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/server.go#L372)

```go
type Polling int
```

#### Values

| `PollingServing` | `iota` | PollingServing means the loop serves, or drains what it holds after a stop. |
| - | - | - |
| `PollingRetired` | | PollingRetired means the loop drained or cut everything after Server.Stop. |

#### Enum `CPUClass`

CPUClass is a bundle of CPU kernels; CPUClassUnknown sets no ceiling.2

[types.go:419 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L419)

```go
type CPUClass int32
```

#### Values

| `CPUClassUnknown` | | CPUClassUnknown sets no ceiling, picking the newest class the machine runs. |
| - | - | - |
| `CPUClassSerial` | | CPUClassSerial runs portable kernels with no SIMD on every machine. |
| `CPUClassMonoversioned` | | CPUClassMonoversioned is the one class a monoversioned build compiles. |
| `CPUClassIntelHaswell` | | CPUClassIntelHaswell runs AVX2 kernels. |
| `CPUClassIntelSkylake` | | CPUClassIntelSkylake adds AVX-512 kernels. |
| `CPUClassIntelIcelake` | | CPUClassIntelIcelake adds Ice Lake's AVX-512 extensions and SHA. |
| `CPUClassIntelAlder` | | CPUClassIntelAlder runs AVX2 kernels with SHA and no AVX-512. |
| `CPUClassAWSG2` | | CPUClassAWSG2 runs NEON kernels with the AES and SHA extensions. |
| `CPUClassAppleM4` | | CPUClassAppleM4 adds timed waits to the Graviton5 kernels. |
| `CPUClassAWSG5` | | CPUClassAWSG5 runs the Graviton2 kernels with newer atomics. |
| `CPUClassAlibabaC930` | | CPUClassAlibabaC930 runs RVV kernels with Zawrs waits and Zacas compare-and-swap. |
| `CPUClassIBMPower10` | | CPUClassIBMPower10 runs VSX kernels with short pauses and store atomics. |

##### `CPUClassFromName`

CPUClassFromName reads a class back from its stable name, "unknown" included.

[types.go:455 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L455)

```go
func CPUClassFromName(name string) (CPUClass, error)
```

##### `String`

String renders the class's stable name, such as "intel-icelake".

[types.go:450 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/types.go#L450)

```go
func (class CPUClass) String() string
```
