|
3 | 3 | [](https://www.nuget.org/packages/soenneker.atomics.nullablebools/) |
4 | 4 | [](https://github.com/soenneker/soenneker.atomics.nullablebools/actions/workflows/codeql.yml) |
5 | 5 |
|
6 | | -#  Soenneker.Atomics.NullableBools |
7 | | -### A lock free atomic nullable boolean. |
| 6 | +# Soenneker.Atomics.NullableBools |
8 | 7 |
|
9 | | -## Installation |
| 8 | +A lightweight atomic tri-state flag implemented on top of an inline `ValueAtomicInt`. Backing values: `-1` = null / unknown `0` = false `1` = true. |
10 | 9 |
|
11 | | -``` |
| 10 | +## Install |
| 11 | + |
| 12 | +```bash |
12 | 13 | dotnet add package Soenneker.Atomics.NullableBools |
13 | 14 | ``` |
| 15 | + |
| 16 | +## What you get |
| 17 | + |
| 18 | +- `AtomicNullableBool` — A lightweight atomic tri-state flag implemented on top of an inline `ValueAtomicInt`. Backing values: `-1` = null / unknown `0` = false `1` = true. |
| 19 | + |
| 20 | +## API at a glance |
| 21 | + |
| 22 | +| API | What it does | Result / important behavior | |
| 23 | +| --- | --- | --- | |
| 24 | +| `AtomicNullableBool.HasValue` | Gets a value indicating whether the current state is non-null. | Gets a value indicating whether the current state is non-null. | |
| 25 | +| `AtomicNullableBool.Value` | Gets or sets the current value as a nullable boolean. | Gets or sets the current value as a nullable boolean. | |
| 26 | +| `AtomicNullableBool.Read()` | Reads the raw backing state. | `-1` (null), `0` (false), or `1` (true). | |
| 27 | +| `AtomicNullableBool.GetValueOrFalse()` | Gets the value, treating `null`/`unknown` as `false`. | true if gets the value, treating null/unknown as; otherwise, false. | |
| 28 | +| `AtomicNullableBool.GetValueOrTrue()` | Gets the value, treating `null`/`unknown` as `true`. | true if gets the value, treating null/unknown as; otherwise, false. | |
| 29 | +| `AtomicNullableBool.Set(value)` | Sets the state to `true` or `false`. | Returns no value; the requested change is complete when the method returns. | |
| 30 | +| `AtomicNullableBool.TrySet(value)` | Attempts to set the state to `true` or `false` only if the current state is `null`/`unknown`. | true if the requested update was applied; otherwise, false. | |
| 31 | +| `AtomicNullableBool.TryCompareExchange(newState, expected)` | Attempts to transition the state from `expected` to `newState`. | true if the requested update was applied; otherwise, false. | |
| 32 | +| `AtomicNullableBool.Reset()` | Resets the state to `null`/`unknown`. | Returns no value; the requested change is complete when the method returns. | |
| 33 | +| `AtomicNullableBool.ToString()` | Returns a string representation of the current state. | Returns `string`. | |
| 34 | + |
| 35 | +## Important behavior |
| 36 | + |
| 37 | +- `AtomicNullableBool`: Reads establish acquire semantics and writes establish release semantics. This is a mutable reference type. Use as a private field and avoid exposing the instance publicly unless you want shared, aliasable state. |
| 38 | +- `AtomicNullableBool.Write(state)`: Callers must only provide valid values: `-1`, `0`, or `1`. |
0 commit comments