Stdlib module threading/mutex.vitl
This page is a wiki-style reference for one concrete stdlib file. It explains what the file owns, where it fits in the family, and how to decide whether this is the right surface to depend on.
threading/mutex.vitl.Family: threading
Kind: public stdlib surface
Page style: this reference follows the same “encyclopedic card + portrait + usage contract” logic as the keyword pages, but for stdlib modules.
Summary
- Overview
- Purpose
- Taxonomy
- Implementation profile
- Top-level API inventory
- Position in family
- Declaration map
- Representative signatures
- How to use this module
- User example
- Keyword coverage
- Source shape
- Source landmarks
- Source organization
- Complete API catalog
- Integration boundaries
- Composition guidance
- Relationship table
- Neighbor modules
Overview
| Field | Value |
|---|---|
| Path | threading/mutex.vitl |
| Family | threading |
| Kind | public stdlib surface |
| Line count | 351 |
| Declared procedures | 30 |
| Declared forms/picks | 4 |
`threading/mutex.vitl` is a public stdlib surface inside the `threading` family. It should be read as one focused slice of the broader family responsibility: Thread, mutex, and pool-based concurrency helpers.
Purpose
This file should be chosen because of responsibility, not because its name “sounds close enough”. Inside the threading family, it carries one focused part of the contract and keeps that responsibility separate from neighboring concerns.
- A worker pool can process tasks in parallel while leaving task definition and result aggregation elsewhere.
Taxonomy
Think of this page as a generated encyclopedia entry rather than a hand-written tutorial. The goal is to show what kind of module this is, how dense it is, and what reading strategy makes sense before depending on it.
- Large algorithm surface: this file exposes many procedures and likely acts as a domain toolkit rather than a single thin wrapper.
- Owns domain vocabulary: the module declares data shapes in addition to executable helpers, so its types are part of the contract.
- Has tuning constants: part of the module behavior is controlled by named constants that document default precision, limits, or policy.
- Narrow dependency fan-in: a small set of imports suggests a focused collaboration surface.
Implementation profile
This profile is inferred directly from the source text. It does not replace reading the file, but it tells you quickly whether the module is mostly declarative, loop-heavy, branch-heavy, or organized around many small exits.
| Signal | Count | What it suggests |
|---|---|---|
if | 12 | Branching density and local decision-making. |
while | 6 | Loop-heavy or iterative implementation style. |
for | 2 | Collection-style traversal at source level. |
match | 0 | Variant-driven branching or grammar-style decoding. |
let | 10 | Local state and intermediate value density. |
give | 38 | Number of explicit exit points and result shaping. |
Top-level API inventory
| Surface | Items |
|---|---|
| Procedures | mutex_new, mutex_recursive_new, mutex_lock, mutex_try_lock, mutex_lock_timeout, mutex_unlock, mutex_get, mutex_set, with_lock, rwlock_new, rwlock_read_lock, rwlock_read_unlock |
| Forms | Mutex, RwLock, Condition, Semaphore |
| Picks | none declared at top level |
| Constants | MUTEX_ID_COUNTER, RWLOCK_ID_COUNTER, CONDITION_ID_COUNTER, SEMAPHORE_ID_COUNTER |
| Exports | none declared at top level |
Imported surfaces
vitte/stdlib/threading/thread // Mutex
Position in family
This file is module 1 of 3 in the threading family when ordered by path. By procedure count it ranks 2, and by line count it ranks 2. Those ranks are useful as rough signals of breadth, not as quality judgments.
Declaration map
The declaration map turns raw source into a scan-friendly catalog. It is useful when the file is large enough that a reader wants to orient by kinds of surfaces first.
| Line | Name | Kind | Role |
|---|---|---|---|
| 1 | vitte/stdlib/threading/mutex | space | Declares the namespace that anchors this file in the stdlib tree. |
| 3 | vitte/stdlib/threading/thread | use | Imports a sibling or supporting surface used by the module. |
| 7 | Mutex | form | Introduces a structured data shape that other procedures can exchange. |
| 18 | mutex_new | proc | Owns coordination, scheduling, or concurrency behavior. |
| 33 | mutex_recursive_new | proc | Owns coordination, scheduling, or concurrency behavior. |
| 48 | mutex_lock | proc | Owns coordination, scheduling, or concurrency behavior. |
| 72 | mutex_try_lock | proc | Owns coordination, scheduling, or concurrency behavior. |
| 85 | mutex_lock_timeout | proc | Owns coordination, scheduling, or concurrency behavior. |
| 101 | mutex_unlock | proc | Owns coordination, scheduling, or concurrency behavior. |
| 123 | mutex_get | proc | Owns coordination, scheduling, or concurrency behavior. |
| 128 | mutex_set | proc | Owns coordination, scheduling, or concurrency behavior. |
| 138 | with_lock | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 147 | RwLock | form | Introduces a structured data shape that other procedures can exchange. |
| 158 | rwlock_new | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 173 | rwlock_read_lock | proc | Owns byte movement or host I/O interaction. |
| 183 | rwlock_read_unlock | proc | Owns byte movement or host I/O interaction. |
| 189 | rwlock_write_lock | proc | Owns byte movement or host I/O interaction. |
| 204 | rwlock_write_unlock | proc | Owns byte movement or host I/O interaction. |
| 211 | rwlock_downgrade | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 224 | Condition | form | Introduces a structured data shape that other procedures can exchange. |
| 229 | condition_new | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 239 | condition_wait | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 256 | condition_signal | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 266 | condition_broadcast | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 273 | Semaphore | form | Introduces a structured data shape that other procedures can exchange. |
| 279 | semaphore_new | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 290 | semaphore_acquire | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 300 | semaphore_release | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 306 | semaphore_try_acquire | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 316 | MUTEX_ID_COUNTER | const | Defines a named constant reused across the module. |
| 317 | RWLOCK_ID_COUNTER | const | Defines a named constant reused across the module. |
| 318 | CONDITION_ID_COUNTER | const | Defines a named constant reused across the module. |
| 319 | SEMAPHORE_ID_COUNTER | const | Defines a named constant reused across the module. |
| 321 | get_next_mutex_id | proc | Owns coordination, scheduling, or concurrency behavior. |
| 326 | get_next_rwlock_id | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 331 | get_next_condition_id | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 336 | get_next_semaphore_id | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 341 | thread_current_id | proc | Owns coordination, scheduling, or concurrency behavior. |
| 345 | sleep_ms | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 349 | timestamp_now | proc | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
The table is exhaustive for top-level declarations of the selected kinds. This file declares 40 matching surfaces.
Representative signatures
These signatures are shown in source order so the page keeps the feel of a reference manual, not just a keyword cloud.
form Mutex<T> {(line 7)proc mutex_new<T>(value: T) -> Mutex<T> {(line 18)proc mutex_recursive_new<T>(value: T) -> Mutex<T> {(line 33)proc mutex_lock<T>(mutex: Mutex<T>) -> bool {(line 48)proc mutex_try_lock<T>(mutex: Mutex<T>) -> bool {(line 72)proc mutex_lock_timeout<T>(mutex: Mutex<T>, timeout_ms: int) -> bool {(line 85)proc mutex_unlock<T>(mutex: Mutex<T>) -> bool {(line 101)proc mutex_get<T>(mutex: Mutex<T>) -> T {(line 123)proc mutex_set<T>(mutex: Mutex<T>, value: T) -> bool {(line 128)proc with_lock<T>(mutex: Mutex<T>, closure: proc) -> bool {(line 138)form RwLock<T> {(line 147)proc rwlock_new<T>(value: T) -> RwLock<T> {(line 158)proc rwlock_read_lock<T>(rwlock: RwLock<T>) -> bool {(line 173)proc rwlock_read_unlock<T>(rwlock: RwLock<T>) -> bool {(line 183)proc rwlock_write_lock<T>(rwlock: RwLock<T>) -> bool {(line 189)proc rwlock_write_unlock<T>(rwlock: RwLock<T>) -> bool {(line 204)proc rwlock_downgrade<T>(rwlock: RwLock<T>) -> bool {(line 211)form Condition {(line 224)
The list is intentionally capped here; the source file declares 38 matching signatures in total.
How to use this module
Start by reading the file as an ownership boundary. Ask three questions: what enters this module, what stable types or procedures it exports, and what adjacent module should stay outside of it.
- Read
spaceand top-level imports first so the ownership boundary ofthreading/mutex.vitlis explicit. - Scan constants before procedures; they often encode precision, limits, or policy assumptions that explain later behavior.
- Read declared forms and picks before algorithms so the data vocabulary is stable in your head.
- Traverse procedures in source order; the early helpers usually explain the naming and numeric conventions used later.
- Only after that compare neighbor modules, because the right boundary choice matters more than memorizing one helper name.
User example
This example is generated from the actual stdlib module surface. Its job is not to be the smallest snippet possible; its job is to show a realistic consumer-shaped file that exercises the module and mirrors the language keywords the module itself relies on.
space demo/threading_mutex
use vitte/stdlib/threading/mutex
const SAMPLE_RETRIES: int = 1
const INITIAL_VALUE: int = 0
form CounterReport {
ok: bool,
value: int
}
proc run_example() -> CounterReport {
let guard = mutex_new<int>(INITIAL_VALUE)
let ready: bool = mutex_lock<int>(guard)
let wrote: bool = mutex_set<int>(guard, 1)
let value: int = mutex_get<int>(guard)
let released: bool = mutex_unlock<int>(guard)
let retries: int = 0
while retries < SAMPLE_RETRIES {
set retries = retries + 1
}
for worker in [1] {
let _seen: int = worker
}
let ok: bool = ready
if not ok {
give CounterReport { ok: false, value: value }
} else {
give CounterReport { ok: true, value: value }
}
}
Keyword coverage
This table makes the “all keywords of the module” requirement auditable. It compares the detected Vitte keywords in the source file with the generated consumer example above.
| Keyword | Present in module source | Used in generated user example |
|---|---|---|
space | yes | yes |
use | yes | yes |
const | yes | yes |
form | yes | yes |
proc | yes | yes |
let | yes | yes |
set | yes | yes |
if | yes | yes |
while | yes | yes |
for | yes | yes |
give | yes | yes |
true | yes | yes |
false | yes | yes |
The generated snippet exercises every detected Vitte keyword used by this module.
Source shape
space vitte/stdlib/threading/mutex
use vitte/stdlib/threading/thread
// Mutex - Mutual Exclusion Lock
form Mutex<T> {
id: int,
data: T,
locked: bool,
owner_thread: int,
lock_count: int, // For recursive mutex
waiters: [int], // Waiting thread IDs
The excerpt is not meant to replace the file. It exists to make the module recognizable at first glance, the same way a Wikipedia infobox helps the reader orient before reading the whole article.
Source landmarks
Large files are easier to retain when they have visible landmarks. When the source contains explicit section banners, they are surfaced here; otherwise the first major declarations are used as anchors.
- Line 1:
space vitte/stdlib/threading/mutex - Line 3:
use vitte/stdlib/threading/thread - Line 7:
form Mutex<T> { - Line 18:
proc mutex_new<T>(value: T) -> Mutex<T> { - Line 33:
proc mutex_recursive_new<T>(value: T) -> Mutex<T> { - Line 48:
proc mutex_lock<T>(mutex: Mutex<T>) -> bool { - Line 72:
proc mutex_try_lock<T>(mutex: Mutex<T>) -> bool { - Line 85:
proc mutex_lock_timeout<T>(mutex: Mutex<T>, timeout_ms: int) -> bool {
Source organization
When a file carries its own internal chaptering, those chapters usually reveal the intended reading order better than a flat symbol list. This section reconstructs that organization from the source itself.
File surfaces
Top-level items: 40. Procedures: 30. Data surfaces: 4. Constants: 4.
First visible names: vitte/stdlib/threading/mutex, vitte/stdlib/threading/thread, Mutex, mutex_new, mutex_recursive_new, mutex_lock, mutex_try_lock, mutex_lock_timeout, mutex_unlock, mutex_get
Complete API catalog
This catalog is the exhaustive file-level index for the module. It is intentionally closer to a generated encyclopedia appendix than to a tutorial summary.
Constants
| Line | Name | Signature | Role |
|---|---|---|---|
| 316 | MUTEX_ID_COUNTER | const MUTEX_ID_COUNTER: int = 0 | Defines a named constant reused across the module. |
| 317 | RWLOCK_ID_COUNTER | const RWLOCK_ID_COUNTER: int = 0 | Defines a named constant reused across the module. |
| 318 | CONDITION_ID_COUNTER | const CONDITION_ID_COUNTER: int = 0 | Defines a named constant reused across the module. |
| 319 | SEMAPHORE_ID_COUNTER | const SEMAPHORE_ID_COUNTER: int = 0 | Defines a named constant reused across the module. |
Data surfaces
| Line | Name | Signature | Role |
|---|---|---|---|
| 7 | Mutex | form Mutex<T> { | Introduces a structured data shape that other procedures can exchange. |
| 147 | RwLock | form RwLock<T> { | Introduces a structured data shape that other procedures can exchange. |
| 224 | Condition | form Condition { | Introduces a structured data shape that other procedures can exchange. |
| 273 | Semaphore | form Semaphore { | Introduces a structured data shape that other procedures can exchange. |
Procedures
| Line | Name | Signature | Role |
|---|---|---|---|
| 18 | mutex_new | proc mutex_new<T>(value: T) -> Mutex<T> { | Owns coordination, scheduling, or concurrency behavior. |
| 33 | mutex_recursive_new | proc mutex_recursive_new<T>(value: T) -> Mutex<T> { | Owns coordination, scheduling, or concurrency behavior. |
| 48 | mutex_lock | proc mutex_lock<T>(mutex: Mutex<T>) -> bool { | Owns coordination, scheduling, or concurrency behavior. |
| 72 | mutex_try_lock | proc mutex_try_lock<T>(mutex: Mutex<T>) -> bool { | Owns coordination, scheduling, or concurrency behavior. |
| 85 | mutex_lock_timeout | proc mutex_lock_timeout<T>(mutex: Mutex<T>, timeout_ms: int) -> bool { | Owns coordination, scheduling, or concurrency behavior. |
| 101 | mutex_unlock | proc mutex_unlock<T>(mutex: Mutex<T>) -> bool { | Owns coordination, scheduling, or concurrency behavior. |
| 123 | mutex_get | proc mutex_get<T>(mutex: Mutex<T>) -> T { | Owns coordination, scheduling, or concurrency behavior. |
| 128 | mutex_set | proc mutex_set<T>(mutex: Mutex<T>, value: T) -> bool { | Owns coordination, scheduling, or concurrency behavior. |
| 138 | with_lock | proc with_lock<T>(mutex: Mutex<T>, closure: proc) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 158 | rwlock_new | proc rwlock_new<T>(value: T) -> RwLock<T> { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 173 | rwlock_read_lock | proc rwlock_read_lock<T>(rwlock: RwLock<T>) -> bool { | Owns byte movement or host I/O interaction. |
| 183 | rwlock_read_unlock | proc rwlock_read_unlock<T>(rwlock: RwLock<T>) -> bool { | Owns byte movement or host I/O interaction. |
| 189 | rwlock_write_lock | proc rwlock_write_lock<T>(rwlock: RwLock<T>) -> bool { | Owns byte movement or host I/O interaction. |
| 204 | rwlock_write_unlock | proc rwlock_write_unlock<T>(rwlock: RwLock<T>) -> bool { | Owns byte movement or host I/O interaction. |
| 211 | rwlock_downgrade | proc rwlock_downgrade<T>(rwlock: RwLock<T>) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 229 | condition_new | proc condition_new() -> Condition { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 239 | condition_wait | proc condition_wait<T>(cond: Condition, mutex: Mutex<T>) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 256 | condition_signal | proc condition_signal(cond: Condition) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 266 | condition_broadcast | proc condition_broadcast(cond: Condition) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 279 | semaphore_new | proc semaphore_new(initial_count: int) -> Semaphore { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 290 | semaphore_acquire | proc semaphore_acquire(sem: Semaphore) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 300 | semaphore_release | proc semaphore_release(sem: Semaphore) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 306 | semaphore_try_acquire | proc semaphore_try_acquire(sem: Semaphore) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 321 | get_next_mutex_id | proc get_next_mutex_id() -> int { | Owns coordination, scheduling, or concurrency behavior. |
| 326 | get_next_rwlock_id | proc get_next_rwlock_id() -> int { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 331 | get_next_condition_id | proc get_next_condition_id() -> int { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 336 | get_next_semaphore_id | proc get_next_semaphore_id() -> int { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 341 | thread_current_id | proc thread_current_id() -> int { | Owns coordination, scheduling, or concurrency behavior. |
| 345 | sleep_ms | proc sleep_ms(ms: int) -> bool { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
| 349 | timestamp_now | proc timestamp_now() -> int { | Represents one top-level surface in the file contract and should be read as part of the module boundary. |
Imports
| Line | Name | Signature | Role |
|---|---|---|---|
| 3 | vitte/stdlib/threading/thread | use vitte/stdlib/threading/thread | Imports a sibling or supporting surface used by the module. |
Integration boundaries
Within threading, this file should remain focused. If a future helper changes the host boundary, scheduling boundary, or data-shape boundary, it probably belongs in a neighbor module instead of being added here by convenience.
- Family responsibility: Thread, mutex, and pool-based concurrency helpers.
- Family architecture role: Use `threading` when the program needs explicit concurrency coordination rather than single-threaded transformation.
Composition guidance
Choose this module when
- Choose
threading/mutex.vitlwhen the main question is owned by this module rather than by transport, storage, orchestration, or user-interface code. - A worker pool can process tasks in parallel while leaving task definition and result aggregation elsewhere.
Pause before extending it when
- Avoid extending this file when the new helper mostly changes the boundary to host I/O, runtime coordination, or foreign integration instead of staying inside
threading. - Check nearby modules such as
threading/thread.vitl,threading/threadpool.vitlbefore adding convenience wrappers here.
Relationship table
This table keeps the page closer to a real encyclopedia entry: a module is easier to understand when compared with its nearest alternatives in the same family.
| Neighbor | Procedures | Data surfaces | Why compare it |
|---|---|---|---|
threading/thread.vitl | 27 | 3 | Shares the same family boundary but carries a distinct slice of responsibility. |
threading/threadpool.vitl | 31 | 6 | Shares the same family boundary but carries a distinct slice of responsibility. |