### Create, Await, Unblock, and Block a Barrier in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Demonstrates the usage of a synchronization barrier in Coalton. It shows how to create a new barrier, block threads until it's unblocked, and then re-block it. This is useful for coordinating the simultaneous start of multiple threads. ```lisp (coalton-toplevel ;; Create a barrier (starts in blocking state) (define barrier (barrier:new)) ;; Await: blocks until barrier is unblocked (barrier:await barrier) ;; Thread blocks here ;; Unblock: releases all waiting threads and allows future awaits to pass (barrier:unblock! barrier) ;; Block: re-enable blocking for future awaits (barrier:block! barrier) ;; Coordinated start example: all threads start work simultaneously (let ((barrier (barrier:new)) (atomic (atomic:new 0))) ;; Spawn 100 threads that wait at barrier (for _ in (iter:range-increasing 1 0 100) (thread:spawn (fn () (barrier:await barrier) (atomic:incf! atomic 1)))) ;; All threads are waiting (atomic:read atomic) ;; => 0 ;; Release all threads at once (barrier:unblock! barrier) (sleep 1) (atomic:read atomic)) ;; => 100 (all threads proceeded simultaneously)) ``` -------------------------------- ### Getting Thread Information in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions to retrieve information about threads. CURRENT-THREAD returns the calling thread's object, and ALL-THREADS returns a list of all currently running threads. ```coalton CURRENT-THREAD :: *(UNIT → LISPTHREAD) ALL-THREADS :: *(UNIT → (LIST LISPTHREAD)) ``` -------------------------------- ### Thread Creation and Management in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions for creating new threads, joining them to retrieve results, and checking their status. SPAWN creates a new thread, JOIN waits for its completion, and ALIVE? checks if a thread is still running. ```coalton SPAWN :: ∀ A. ((UNIT → A) → (THREAD A)) JOIN :: ∀ A. ((THREAD A) → (RESULT LISPCONDITION A)) ALIVE? :: ∀ A. INTO A LISPTHREAD ⇒ (A → BOOLEAN) ``` -------------------------------- ### Mutex Operations API Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md This section details the operations available for Mutexes, including updating, writing, swapping, reading, and creating new Mutexes. ```APIDOC ## UPDATE! /mutex ### Description Swap the value held in a Mutex with a transforming function. ### Method POST ### Endpoint /mutex ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **mutex** (MUTEX A) - Required - The mutex to update. - **transform_fn** (A -> A) - Required - The function to transform the mutex value. ### Request Example ```json { "mutex": "", "transform_fn": "" } ``` ### Response #### Success Response (200) - **value** (A) - The new value held in the mutex. #### Response Example ```json { "value": "" } ``` ## WRITE! /mutex ### Description Set the value held in a Mutex, returning the new value. ### Method POST ### Endpoint /mutex ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **mutex** (MUTEX A) - Required - The mutex to write to. - **new_value** (A) - Required - The new value to set. ### Request Example ```json { "mutex": "", "new_value": "" } ``` ### Response #### Success Response (200) - **value** (A) - The new value held in the mutex. #### Response Example ```json { "value": "" } ``` ## SWAP! /mutex ### Description Replace the value held in a Mutex, returning the old value. ### Method POST ### Endpoint /mutex ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **mutex** (MUTEX A) - Required - The mutex to swap. - **new_value** (A) - Required - The new value to set. ### Request Example ```json { "mutex": "", "new_value": "" } ``` ### Response #### Success Response (200) - **old_value** (A) - The old value that was held in the mutex. #### Response Example ```json { "old_value": "" } ``` ## READ /mutex ### Description Access the value held in a Mutex. ### Method GET ### Endpoint /mutex ### Parameters #### Path Parameters None #### Query Parameters - **mutex** (MUTEX A) - Required - The mutex to read from. ### Request Example ``` GET /mutex?mutex= ``` ### Response #### Success Response (200) - **value** (A) - The current value held in the mutex. #### Response Example ```json { "value": "" } ``` ## NEW /mutex ### Description Create a new Mutex. ### Method POST ### Endpoint /mutex ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **initial_value** (A) - Required - The initial value for the mutex. ### Request Example ```json { "initial_value": "" } ``` ### Response #### Success Response (200) - **mutex** (MUTEX A) - The newly created mutex object. #### Response Example ```json { "mutex": "" } ``` ``` -------------------------------- ### Barrier Operations API Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md This section details the operations available for Barriers, including unblocking, blocking, awaiting, and creating new Barriers. ```APIDOC ## UNBLOCK! /barrier ### Description Unblocks a barrier. ### Method POST ### Endpoint /barrier/unblock ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **barrier** (BARRIER) - Required - The barrier to unblock. ### Request Example ```json { "barrier": "" } ``` ### Response #### Success Response (200) - **status** (UNIT) - Indicates successful operation. #### Response Example ```json { "status": "" } ``` ## BLOCK! /barrier ### Description Blocks a barrier. ### Method POST ### Endpoint /barrier/block ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **barrier** (BARRIER) - Required - The barrier to block. ### Request Example ```json { "barrier": "" } ``` ### Response #### Success Response (200) - **status** (UNIT) - Indicates successful operation. #### Response Example ```json { "status": "" } ``` ## AWAIT /barrier ### Description Awaits a barrier. ### Method POST ### Endpoint /barrier/await ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **barrier** (BARRIER) - Required - The barrier to await. ### Request Example ```json { "barrier": "" } ``` ### Response #### Success Response (200) - **status** (UNIT) - Indicates successful operation. #### Response Example ```json { "status": "" } ``` ## NEW /barrier ### Description Creates a new barrier. ### Method POST ### Endpoint /barrier ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json {} ``` ### Response #### Success Response (200) - **barrier** (BARRIER) - The newly created barrier object. #### Response Example ```json { "barrier": "" } ``` ``` -------------------------------- ### Condition Variable Operations in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Provides condition variables for thread coordination, allowing threads to wait for specific conditions to be met. Supports creation, awaiting (releasing a lock and waiting), notifying a single thread, and broadcasting to all waiting threads. ```lisp (coalton-toplevel ;; Create a condition variable (define cv (condition-variable:new)) (define lock (lock:new)) ;; Await: atomically release lock and wait for notification ;; Must be called while holding the lock (lock:with-lock-held lock (fn () (condition-variable:await cv lock))) ;; Thread blocks until notified ;; Notify one waiting thread (condition-variable:notify cv) ;; Broadcast: notify ALL waiting threads (condition-variable:broadcast cv) ;; Ordered counter example using condition variable (let ((atomic (atomic:new 0)) (target 30) (cv (condition-variable:new)) (lock (lock:new))) ;; Worker waits until atomic equals its assigned number (let ((worker (fn (i) (lock:with-lock-held lock (fn () (while (not (== i (atomic:read atomic))) (condition-variable:await cv lock)) (atomic:incf! atomic 1))) (condition-variable:broadcast cv)))) ;; Spawn workers that must execute in reverse order (for x in (range target 1) (thread:spawn (fn () (worker (- target x))))) ;; Wait for all to complete (lock:with-lock-held lock (fn () (while (not (== target (atomic:read atomic))) (condition-variable:await cv lock)))) (atomic:read atomic))) ;; => 30) ) ``` -------------------------------- ### Condition Variable Operations in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions for condition variables. NEW creates a condition variable, AWAIT waits on the variable, NOTIFY wakes one waiting thread, and BROADCAST wakes all waiting threads. ```coalton NEW :: *(UNIT → CONDITIONVARIABLE) AWAIT :: *(CONDITIONVARIABLE → LOCK → UNIT) NOTIFY :: *(CONDITIONVARIABLE → UNIT) BROADCAST :: *(CONDITIONVARIABLE → UNIT) ``` -------------------------------- ### Thread Management in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Manages threads by spawning, checking liveness, joining for results, and interrupting/destroying them. Threads return a Result type for error handling. ```lisp (in-package #:coalton-user) (named-readtables:in-readtable coalton:coalton) (coalton-toplevel ;; Spawn a new thread that computes a value (define my-thread (thread:spawn (fn () (sleep 1) (make-list 42)))) ;; Check if thread is still running (define is-running (thread:alive? my-thread)) ;; => True (while thread is computing) ;; Wait for thread to complete and get result (define result (thread:join my-thread)) ;; => (Ok (42)) ;; Get current thread and all running threads (define current (thread:current-thread)) (define all (thread:all-threads)) ;; Interrupt a thread to execute code in its context (define interrupted-thread (thread:spawn (fn () (sleep 40)))) (thread:interrupt interrupted-thread (fn () Unit)) ;; => (Ok ) ;; Destroy/terminate a thread (thread:destroy interrupted-thread) ;; => (Ok ) or (Err ) if already dead)) ``` -------------------------------- ### Semaphore Operations in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions for managing semaphores. NEW creates a semaphore, AWAIT decrements the semaphore count (blocking if zero), and SIGNAL increments the count and wakes waiting threads. ```coalton NEW :: *(UNIT → SEMAPHORE) AWAIT :: *(SEMAPHORE → UNIT) SIGNAL :: *(SEMAPHORE → UFIX → UNIT) ``` -------------------------------- ### Semaphore Operations in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Implements counting semaphores for controlling access to limited resources or for inter-thread signaling. Supports creation, signaling (incrementing), and awaiting (decrementing) operations. ```lisp (coalton-toplevel ;; Create a semaphore with initial count 0 (define sem (semaphore:new)) ;; Signal (increment) the semaphore by count ;; Wakes up to 'count' waiting threads (semaphore:signal sem 2) ;; Await (decrement) the semaphore ;; Blocks if count is 0, proceeds if count > 0 (semaphore:await sem) (semaphore:await sem) ;; Producer-consumer pattern example (let ((sem (semaphore:new)) (count (atomic:new 0))) ;; Producer: signal after delay (thread:spawn (fn () (sleep 1) (semaphore:signal sem 4))) ;; Allow 4 consumers to proceed ;; Consumers: wait for signal then increment counter (for x in (range 1 5) (thread:spawn (fn () (semaphore:await sem) (atomic:incf! count 1)))) (sleep 2) (atomic:read count)) ;; => 4 (only 4 of 5 consumers could proceed)) ) ``` -------------------------------- ### Atomic Integer Operations in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Provides lock-free atomic operations on machine-word-sized unsigned integers. Supports creation, reading, incrementing, decrementing, and compare-and-swap operations, ensuring thread safety without explicit locking. ```lisp (coalton-toplevel ;; Create an atomic integer with initial value (define atomic-counter (atomic:new 0)) ;; Read current value (atomic:read atomic-counter) ;; => 0 ;; Atomically increment, returns new value (atomic:incf! atomic-counter 10) ;; => 10 ;; Atomically decrement, returns new value (atomic:decf! atomic-counter 3) ;; => 7 ;; Compare-and-swap: if current == old, set to new (atomic:cas! atomic-counter 7 100) ;; => True (swap succeeded, value is now 100) (atomic:cas! atomic-counter 7 200) ;; => False (current value was 100, not 7, no change) ;; Concurrent increment/decrement example (let ((atomic (atomic:new 4000)) (incer (thread:spawn (fn () (for x in (range 1 1000) (atomic:incf! atomic 1))))) (decer (thread:spawn (fn () (for x in (range 1 1000) (atomic:decf! atomic 1)))))) (thread:join incer) (thread:join decer) (atomic:read atomic)) ;; => 4000 (increments and decrements balance out)) ) ``` -------------------------------- ### Non-Recursive Lock Operations in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions for managing non-recursive locks. NEW creates a lock, ACQUIRE attempts to acquire it, ACQUIRE-NO-WAIT attempts acquisition without blocking, and RELEASE frees the lock. ```coalton NEW :: *(UNIT → LOCK) ACQUIRE :: *(LOCK → BOOLEAN) ACQUIRE-NO-WAIT :: *(LOCK → BOOLEAN) RELEASE :: *(LOCK → (RESULT LISPCONDITION LOCK)) ``` -------------------------------- ### Non-Recursive Lock Usage in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Provides a non-recursive mutex for mutual exclusion, allowing threads to acquire and release locks. Includes blocking and non-blocking acquire attempts, and a utility for executing code with the lock held. ```lisp (coalton-toplevel ;; Create a new lock (define my-lock (lock:new)) ;; Acquire lock (blocks until available) (lock:acquire my-lock) ;; => True ;; Release the lock (lock:release my-lock) ;; => (Ok ) ;; Try to acquire without blocking (lock:acquire-no-wait my-lock) ;; => True if acquired, False if lock is held by another thread ;; Execute code with lock held (automatic acquire/release) (lock:with-lock-held my-lock (fn () ;; Critical section code here (+ 1 2))) ;; => 3 (lock automatically released after thunk returns)) ``` -------------------------------- ### Recursive Lock Operations in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions for managing recursive locks. NEW creates a recursive lock, ACQUIRE attempts to acquire it, ACQUIRE-NO-WAIT attempts acquisition without blocking, and RELEASE frees the lock. ```coalton NEW :: *(UNIT → RECURSIVELOCK) ACQUIRE :: *(RECURSIVELOCK → BOOLEAN) ACQUIRE-NO-WAIT :: *(RECURSIVELOCK → BOOLEAN) RELEASE :: *(RECURSIVELOCK → (RESULT LISPCONDITION RECURSIVELOCK)) ``` -------------------------------- ### Thread-Safe Mutex (Cell) in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Provides a thread-safe mutable cell (mutex) for concurrent read/write access. Operations include creating, reading, writing, swapping, and updating the cell's value, all while ensuring thread safety. ```lisp (coalton-toplevel ;; Create a mutex holding an initial value (define counter (mutex:new 0)) ;; Read the current value (acquires lock internally) (mutex:read counter) ;; => 0 ;; Write a new value, returns the new value (mutex:write! counter 10) ;; => 10 ;; Swap value, returns the old value (mutex:swap! counter 20) ;; => 10 (old value) ;; Update with a function, returns the new value (mutex:update! counter (fn (x) (+ x 1))) ;; => 21) ``` -------------------------------- ### Atomic Integer Operations in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions for atomic integer manipulation. NEW creates an atomic integer, READ retrieves its value, INCF! and DECF! atomically increment/decrement, and CAS! performs a compare-and-swap. ```coalton NEW :: *(U64 → ATOMICINTEGER) READ :: *(ATOMICINTEGER → U64) INCF! :: *(ATOMICINTEGER → U64 → U64) DECF! :: *(ATOMICINTEGER → U64 → U64) CAS! :: *(ATOMICINTEGER → U64 → U64 → BOOLEAN) ``` -------------------------------- ### Thread Interruption and Destruction in Coalton Source: https://github.com/garlic0x1/coalton-threads/blob/master/REFERENCE.md Functions for interrupting and destroying threads. INTERRUPT allows calling a thunk within the interrupted thread's context, while DESTROY forcefully terminates a thread. ```coalton INTERRUPT :: *∀ A. INTO A LISPTHREAD ⇒ (A → (UNIT → UNIT) → (RESULT LISPCONDITION A)) DESTROY :: *∀ A. INTO A LISPTHREAD ⇒ (A → (RESULT LISPCONDITION A)) ``` -------------------------------- ### Recursive Lock Operations in Coalton Source: https://context7.com/garlic0x1/coalton-threads/llms.txt Implements a recursive lock that can be acquired multiple times by the same thread, requiring a corresponding release for each acquisition. Supports non-blocking attempts and execution within a locked context. ```lisp (coalton-toplevel ;; Create a recursive lock (define rec-lock (recursive-lock:new)) ;; Same thread can acquire multiple times (recursive-lock:acquire rec-lock) ;; => True (recursive-lock:acquire rec-lock) ;; => True (same thread, allowed) ;; Must release same number of times (recursive-lock:release rec-lock) (recursive-lock:release rec-lock) ;; Non-blocking acquire attempt (recursive-lock:acquire-no-wait rec-lock) ;; => True if acquired, False otherwise ;; Execute with lock held (recursive-lock:with-lock-held rec-lock (fn () ;; Can safely call functions that also acquire this lock "result"))) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.