# limine-mutex - simple shell mutex using flock
#
# This shell library provides two functions: `mutex_lock` and `mutex_unlock`.
# They can be sourced and used by other scripts or tools to safely coordinate access
# to shared files like `limine.conf` or bootable files on the same boot partition, avoiding race conditions.
#
# Usage:
#   source /lib/limine/limine-mutex
#   mutex_lock "some-task-name"
#   # ... do critical work ...
#   mutex_unlock
#
# The lock uses a fixed global lock file at /tmp/limine-global.lock.
readonly LIMINE_LOCK_FILE="/tmp/limine-global.lock"

# Acquire a global lock to prevent concurrent access.
# Arguments:
#   $1 - name of the caller (used for error message)
#   $2 - optional timeout in seconds (default: 30)
mutex_lock() {
	local name=${1:-"limine-snapper-sync-mutex"}
	local timeout=${2:-30} # Default to 30 seconds if not provided

	# Open the lock file and associate it with file descriptor 200
	exec 200>${LIMINE_LOCK_FILE} || {
		# Try to delete and recreate the lock file if it fails
		rm -f ${LIMINE_LOCK_FILE}
		exec 200>${LIMINE_LOCK_FILE}
	}

	# Try to acquire the lock with timeout
	flock --timeout="$timeout" 200 || {
		echo "WARNING: Mutex lock timeout on ${name}." >&2
		return 1
	}
}

# Release the global lock.
mutex_unlock() {
	flock --unlock 200
}
