28 lines
501 B
GDScript3
28 lines
501 B
GDScript3
|
class_name NonReentrantCoroutineMutex extends RefCounted
|
||
|
|
||
|
signal relinquished
|
||
|
|
||
|
var _thread_mutex: Mutex
|
||
|
var _busy := false
|
||
|
|
||
|
func _init() -> void:
|
||
|
_thread_mutex = Mutex.new()
|
||
|
|
||
|
func acquire() -> void:
|
||
|
_thread_mutex.lock()
|
||
|
while _busy:
|
||
|
await relinquished
|
||
|
_busy = true
|
||
|
|
||
|
func relinquish() -> void:
|
||
|
if _busy:
|
||
|
_busy = false
|
||
|
relinquished.emit()
|
||
|
_thread_mutex.unlock()
|
||
|
|
||
|
func guard(thunk: Callable) -> Variant:
|
||
|
await acquire()
|
||
|
var result: Variant = await thunk.call()
|
||
|
relinquish()
|
||
|
return result
|