# 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 <= '1.8.7')" then "$ruby" -e "File.new($lock_fd).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)" elif [[ -x "$(type -P flock)" ]] then flock -n "$lock_fd" elif [[ -x "$python" ]] then "$python" -c "import fcntl; fcntl.flock($lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)" else onoe "Cannot create $name lock, please avoid running Homebrew in parallel." fi }