tags: mutual-exclusion semaphores

Mutex

mutex is a special kind of semaphore for a specific purpose to ensure mutual-exclusion. It is similar to lock variable. It can only contain one of two values, 0 or 1. 0 means unlocked and 1 means locked.

Implementation of mutex for threads

mutex_lock:
    TSL REG, MUTEX
    CMP REG, #0
    JE RET
    CALL thread_yield
    JMP mutex_lock

mutex_unlock:
    MOVE MUTEX, #0
    RET

This implementation may seem similar to enter_region and exit_region. However, it is not. There is a subtle difference between them.

enter_region and exit_region involves busy waiting. This one though doesn’t do that.

See also

  1. Futex
  2. Mutex in Pthreads
  3. condition-variable