aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXu Cheng2016-05-02 17:52:19 +0800
committerXu Cheng2016-05-12 15:36:46 +0800
commitad8f50ec6df6d6160f1fcea895f4338233d19476 (patch)
tree4d810b6aa7750ae67a2d43c4233a8f3b4795df00
parent1087df1016f65e3b1cef9a11597086e56674a369 (diff)
downloadbrew-ad8f50ec6df6d6160f1fcea895f4338233d19476.tar.bz2
add utils/lock.sh
a Bash implementation of lock using `flock(2)`.
-rw-r--r--Library/Homebrew/utils/lock.sh38
1 files changed, 38 insertions, 0 deletions
diff --git a/Library/Homebrew/utils/lock.sh b/Library/Homebrew/utils/lock.sh
new file mode 100644
index 000000000..97cf28219
--- /dev/null
+++ b/Library/Homebrew/utils/lock.sh
@@ -0,0 +1,38 @@
+# 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_file="/tmp/homebrew${HOMEBREW_REPOSITORY//\//-}-${name}.lock"
+ # 200 is the file descriptor used in the lock.
+ # This FD should be used exclusively for lock purpose.
+ # Any value except 0(stdin), 1(stdout) and 2(stderr) can do the job.
+ # Noted, FD is unique per process but it will be shared to subprocess.
+ # It is recommended to choose a large number to avoid conflicting with
+ # other FD opened by the script.
+ #
+ # close FD first, this is required if parent process holds a different lock.
+ exec 200>&-
+ # open the lock file to FD, so the shell process can hold the lock.
+ exec 200>"$lock_file"
+ if ! _create_lock 200
+ then
+ odie <<EOS
+Another active Homebrew process is already in progress.
+Please wait for it to finish or terminate it to continue.
+EOS
+ fi
+}
+
+_create_lock() {
+ local lock_fd="$1"
+ if [[ -n "$(which ruby)" ]]
+ then
+ ruby -e "File.new($lock_fd).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
+ elif [[ -n "$(which flock)" ]]
+ then
+ flock -n "$lock_fd"
+ else
+ onoe "Cannot create lock, please avoid running brew in parallel."
+ fi
+}