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.

Visual portrait of threading/mutex.vitl
Wiki-style portrait for 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

FieldValue
Paththreading/mutex.vitl
Familythreading
Kindpublic stdlib surface
Line count351
Declared procedures30
Declared forms/picks4

`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.

SignalCountWhat it suggests
if12Branching density and local decision-making.
while6Loop-heavy or iterative implementation style.
for2Collection-style traversal at source level.
match0Variant-driven branching or grammar-style decoding.
let10Local state and intermediate value density.
give38Number of explicit exit points and result shaping.

Top-level API inventory

SurfaceItems
Proceduresmutex_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
FormsMutex, RwLock, Condition, Semaphore
Picksnone declared at top level
ConstantsMUTEX_ID_COUNTER, RWLOCK_ID_COUNTER, CONDITION_ID_COUNTER, SEMAPHORE_ID_COUNTER
Exportsnone 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.

LineNameKindRole
1vitte/stdlib/threading/mutexspaceDeclares the namespace that anchors this file in the stdlib tree.
3vitte/stdlib/threading/threaduseImports a sibling or supporting surface used by the module.
7MutexformIntroduces a structured data shape that other procedures can exchange.
18mutex_newprocOwns coordination, scheduling, or concurrency behavior.
33mutex_recursive_newprocOwns coordination, scheduling, or concurrency behavior.
48mutex_lockprocOwns coordination, scheduling, or concurrency behavior.
72mutex_try_lockprocOwns coordination, scheduling, or concurrency behavior.
85mutex_lock_timeoutprocOwns coordination, scheduling, or concurrency behavior.
101mutex_unlockprocOwns coordination, scheduling, or concurrency behavior.
123mutex_getprocOwns coordination, scheduling, or concurrency behavior.
128mutex_setprocOwns coordination, scheduling, or concurrency behavior.
138with_lockprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
147RwLockformIntroduces a structured data shape that other procedures can exchange.
158rwlock_newprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
173rwlock_read_lockprocOwns byte movement or host I/O interaction.
183rwlock_read_unlockprocOwns byte movement or host I/O interaction.
189rwlock_write_lockprocOwns byte movement or host I/O interaction.
204rwlock_write_unlockprocOwns byte movement or host I/O interaction.
211rwlock_downgradeprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
224ConditionformIntroduces a structured data shape that other procedures can exchange.
229condition_newprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
239condition_waitprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
256condition_signalprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
266condition_broadcastprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
273SemaphoreformIntroduces a structured data shape that other procedures can exchange.
279semaphore_newprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
290semaphore_acquireprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
300semaphore_releaseprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
306semaphore_try_acquireprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
316MUTEX_ID_COUNTERconstDefines a named constant reused across the module.
317RWLOCK_ID_COUNTERconstDefines a named constant reused across the module.
318CONDITION_ID_COUNTERconstDefines a named constant reused across the module.
319SEMAPHORE_ID_COUNTERconstDefines a named constant reused across the module.
321get_next_mutex_idprocOwns coordination, scheduling, or concurrency behavior.
326get_next_rwlock_idprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
331get_next_condition_idprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
336get_next_semaphore_idprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
341thread_current_idprocOwns coordination, scheduling, or concurrency behavior.
345sleep_msprocRepresents one top-level surface in the file contract and should be read as part of the module boundary.
349timestamp_nowprocRepresents 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.

  1. Read space and top-level imports first so the ownership boundary of threading/mutex.vitl is explicit.
  2. Scan constants before procedures; they often encode precision, limits, or policy assumptions that explain later behavior.
  3. Read declared forms and picks before algorithms so the data vocabulary is stable in your head.
  4. Traverse procedures in source order; the early helpers usually explain the naming and numeric conventions used later.
  5. 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.

KeywordPresent in module sourceUsed in generated user example
spaceyesyes
useyesyes
constyesyes
formyesyes
procyesyes
letyesyes
setyesyes
ifyesyes
whileyesyes
foryesyes
giveyesyes
trueyesyes
falseyesyes

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

LineNameSignatureRole
316MUTEX_ID_COUNTERconst MUTEX_ID_COUNTER: int = 0Defines a named constant reused across the module.
317RWLOCK_ID_COUNTERconst RWLOCK_ID_COUNTER: int = 0Defines a named constant reused across the module.
318CONDITION_ID_COUNTERconst CONDITION_ID_COUNTER: int = 0Defines a named constant reused across the module.
319SEMAPHORE_ID_COUNTERconst SEMAPHORE_ID_COUNTER: int = 0Defines a named constant reused across the module.

Data surfaces

LineNameSignatureRole
7Mutexform Mutex<T> {Introduces a structured data shape that other procedures can exchange.
147RwLockform RwLock<T> {Introduces a structured data shape that other procedures can exchange.
224Conditionform Condition {Introduces a structured data shape that other procedures can exchange.
273Semaphoreform Semaphore {Introduces a structured data shape that other procedures can exchange.

Procedures

LineNameSignatureRole
18mutex_newproc mutex_new<T>(value: T) -> Mutex<T> {Owns coordination, scheduling, or concurrency behavior.
33mutex_recursive_newproc mutex_recursive_new<T>(value: T) -> Mutex<T> {Owns coordination, scheduling, or concurrency behavior.
48mutex_lockproc mutex_lock<T>(mutex: Mutex<T>) -> bool {Owns coordination, scheduling, or concurrency behavior.
72mutex_try_lockproc mutex_try_lock<T>(mutex: Mutex<T>) -> bool {Owns coordination, scheduling, or concurrency behavior.
85mutex_lock_timeoutproc mutex_lock_timeout<T>(mutex: Mutex<T>, timeout_ms: int) -> bool {Owns coordination, scheduling, or concurrency behavior.
101mutex_unlockproc mutex_unlock<T>(mutex: Mutex<T>) -> bool {Owns coordination, scheduling, or concurrency behavior.
123mutex_getproc mutex_get<T>(mutex: Mutex<T>) -> T {Owns coordination, scheduling, or concurrency behavior.
128mutex_setproc mutex_set<T>(mutex: Mutex<T>, value: T) -> bool {Owns coordination, scheduling, or concurrency behavior.
138with_lockproc 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.
158rwlock_newproc 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.
173rwlock_read_lockproc rwlock_read_lock<T>(rwlock: RwLock<T>) -> bool {Owns byte movement or host I/O interaction.
183rwlock_read_unlockproc rwlock_read_unlock<T>(rwlock: RwLock<T>) -> bool {Owns byte movement or host I/O interaction.
189rwlock_write_lockproc rwlock_write_lock<T>(rwlock: RwLock<T>) -> bool {Owns byte movement or host I/O interaction.
204rwlock_write_unlockproc rwlock_write_unlock<T>(rwlock: RwLock<T>) -> bool {Owns byte movement or host I/O interaction.
211rwlock_downgradeproc 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.
229condition_newproc condition_new() -> Condition {Represents one top-level surface in the file contract and should be read as part of the module boundary.
239condition_waitproc 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.
256condition_signalproc condition_signal(cond: Condition) -> bool {Represents one top-level surface in the file contract and should be read as part of the module boundary.
266condition_broadcastproc condition_broadcast(cond: Condition) -> bool {Represents one top-level surface in the file contract and should be read as part of the module boundary.
279semaphore_newproc 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.
290semaphore_acquireproc semaphore_acquire(sem: Semaphore) -> bool {Represents one top-level surface in the file contract and should be read as part of the module boundary.
300semaphore_releaseproc semaphore_release(sem: Semaphore) -> bool {Represents one top-level surface in the file contract and should be read as part of the module boundary.
306semaphore_try_acquireproc 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.
321get_next_mutex_idproc get_next_mutex_id() -> int {Owns coordination, scheduling, or concurrency behavior.
326get_next_rwlock_idproc get_next_rwlock_id() -> int {Represents one top-level surface in the file contract and should be read as part of the module boundary.
331get_next_condition_idproc get_next_condition_id() -> int {Represents one top-level surface in the file contract and should be read as part of the module boundary.
336get_next_semaphore_idproc get_next_semaphore_id() -> int {Represents one top-level surface in the file contract and should be read as part of the module boundary.
341thread_current_idproc thread_current_id() -> int {Owns coordination, scheduling, or concurrency behavior.
345sleep_msproc sleep_ms(ms: int) -> bool {Represents one top-level surface in the file contract and should be read as part of the module boundary.
349timestamp_nowproc timestamp_now() -> int {Represents one top-level surface in the file contract and should be read as part of the module boundary.

Imports

LineNameSignatureRole
3vitte/stdlib/threading/threaduse vitte/stdlib/threading/threadImports 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.vitl when 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.vitl before 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.

NeighborProceduresData surfacesWhy compare it
threading/thread.vitl273Shares the same family boundary but carries a distinct slice of responsibility.
threading/threadpool.vitl316Shares the same family boundary but carries a distinct slice of responsibility.

Neighbor modules