Files
obitools4/autodoc/docmd/pkg/obilua/mutex.md
T
Eric Coissac 8c7017a99d ⬆️ version bump to v4.5
- Update obioptions.Version from "Release 4.4.29" to "/v/ Release v5"
- Update version.txt from 4.29 → .30
(automated by Makefile)
2026-04-13 13:34:53 +02:00

1.3 KiB
Raw Blame History

obilua.Mutex: Thread-Safe Synchronization in Lua via Go's sync.Mutex

This package exposes Gos sync.Mutex to the Lua environment using gopher-lua, enabling safe concurrent access from Lua scripts.

Key Features

  • Custom userdata type: Registers a new metatable "Mutex" in the Lua state.
  • Constructor function:
    • Mutex.new() → mutex userdata
      Creates and returns a new Go-backed mutex instance.
  • Instance methods:
    • mutex:lock() — Acquires the lock (blocks until available).
    • mutex:unlock() — Releases the lock.
  • Type safety: Validates that only valid mutex userdatas are passed to lock/unlock.
  • Integration: Designed for embedding Lua in Go applications requiring synchronization (e.g., multi-threaded scripting).

Usage Example

local m = Mutex.new()
m:lock()   -- Acquire lock (safe across goroutines)
-- critical section
m:unlock()

Implementation Notes

  • Mutex state is stored in a Go *sync.Mutex inside Lua userdata.
  • No reference counting or finalizers — user must manually manage lock/unlock lifecycle to avoid deadlocks.
  • Thread-safe from Go side only; Lua calls must respect goroutine safety (e.g., avoid calling from multiple VMs concurrently).