Files
obitools4/autodoc/docmd/pkg/obilua/mutex.md
T

31 lines
1.3 KiB
Markdown
Raw Normal View History

2026-04-07 08:36:50 +02:00
# `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](https://github.com/yuin/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
```lua
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).