Skip to main content

Overview

The Mutex class implements a binary mutex (mutual exclusion lock) with a FIFO queue for managing blocked threads. It ensures that only one thread can access a critical section at a time, with fair ordering based on arrival time.

Constructor

Mutex
class
Creates a new mutex instance with no initial owner.
const mutex = new Mutex();
Initial State:
  • locked: false
  • owner: null
  • queue: []

Properties

locked
boolean
Indicates whether the critical section is currently taken. true when a thread owns the mutex.
owner
Thread | null
The thread that currently holds the mutex. null when the mutex is unlocked.
queue
Array<Thread>
FIFO queue of threads waiting to acquire the mutex, ordered by arrival time.

Methods

acquire(thread)

Attempts to acquire the mutex for the specified thread.
thread
Thread
required
The thread attempting to acquire the mutex.
return
boolean
  • true: Thread successfully acquired the mutex and can proceed
  • false: Thread is blocked and added to the waiting queue
Behavior:
  1. Already owns mutex: If the mutex transferred ownership to this thread via release(), returns true
  2. Mutex available: If unlocked, immediately grants ownership and returns true
  3. Mutex locked: Blocks the thread, sets its state to "blocked", and adds it to the queue
Source: mutex.js:10
// Example: Thread acquiring mutex
const acquired = mutex.acquire(thread1);
if (acquired) {
  // Thread entered critical section
  console.log('Thread has mutex');
} else {
  // Thread is blocked, waiting in queue
  console.log('Thread blocked');
}

release(thread)

Releases the mutex and transfers ownership to the next waiting thread if the queue is non-empty.
thread
Thread
required
The thread releasing the mutex. Must be the current owner.
Behavior:
  1. Validation: Throws error if the calling thread is not the current owner
  2. No waiters: Unlocks the mutex and clears the owner
  3. Has waiters: Directly transfers ownership to the first thread in the queue (FIFO), sets it to "ready" state
Source: mutex.js:34 Throws:
  • Error: “Solo el poseedor puede liberar el mutex” if non-owner attempts to release
// Example: Releasing mutex
try {
  mutex.release(thread1);
  // Mutex released, next thread (if any) now owns it
} catch (error) {
  // Thread doesn't own the mutex
  console.error('Cannot release mutex:', error.message);
}

Usage Example

import { Mutex } from './core/mutex.js';

const mutex = new Mutex();
const criticalSection = mutex;

// Thread 1 execution
function thread1Code() {
  if (mutex.acquire(thread1)) {
    // Critical section
    console.log('Thread 1 accessing shared resource');
    
    // Release when done
    mutex.release(thread1);
  }
}

// Thread 2 execution
function thread2Code() {
  if (!mutex.acquire(thread2)) {
    // Blocked - will be woken when thread1 releases
    console.log('Thread 2 waiting...');
  }
}

FIFO Guarantee

The mutex implements strict FIFO ordering:
  • Threads are added to the queue in arrival order
  • When released, ownership transfers to the first thread in the queue
  • No thread can skip ahead (prevents starvation)

Direct Transfer Optimization

When releasing with waiting threads, the mutex uses direct transfer instead of unlocking:
// Instead of: unlock → thread acquires
// Does: transfer ownership directly to next thread
const nextThread = this.queue.shift();
nextThread.state = "ready";
this.owner = nextThread;
// Mutex stays locked, ownership transferred
This prevents race conditions and ensures FIFO fairness.