# create a lock using `flock(2)`. A name is required as first argument. # the lock will be automatically unlocked when the shell process quits. # Noted due to the fixed FD, a shell process can only create one lock. lock() { local name="$1" local lock_dir="$HOMEBREW_PREFIX/var/homebrew/locks" local lock_file="$lock_dir/$name" [[ -d "$lock_dir" ]] || mkdir -p "$lock_dir" if ! [[ -w "$lock_dir" ]] then odie <&- # open the lock file to FD, so the shell process can hold the lock. exec 200>"$lock_file" if ! _create_lock 200 "$name" then odie <