summaryrefslogtreecommitdiff
path: root/pm
diff options
context:
space:
mode:
authorVictor Lowther <victor.lowther@gmail.com>2010-06-19 18:10:00 -0500
committerVictor Lowther <victor.lowther@gmail.com>2010-06-19 18:10:00 -0500
commit646a20af9b48d1bb4630f237a345e82ffb2921b5 (patch)
treeefe8cb8d850a3d79a45dbc714d236283f801203a /pm
parent1fd247d087ed73896d2b2156039dff2a353f6db6 (diff)
parent79dde6f2f7bb620b163981ec7a7b995c9c9d1b0e (diff)
Merge remote branch 'UPSTREAM/pm-utils-1.4' into pm-utils-1.4
Diffstat (limited to 'pm')
-rw-r--r--pm/HOWTO.hooks2
-rw-r--r--pm/functions.in26
2 files changed, 15 insertions, 13 deletions
diff --git a/pm/HOWTO.hooks b/pm/HOWTO.hooks
index ad7d4a7..ce9e8c4 100644
--- a/pm/HOWTO.hooks
+++ b/pm/HOWTO.hooks
@@ -99,7 +99,7 @@ the hook. This will make the following convenience functions available:
2: spin_lock
Wrapper around try_lock. Second parameter is the number of seconds
to wait for the lock before giving up. If no second parameter
- is passed, this function will wait forever.
+ is passed, this function will wait for 60 seconds.
3: release_lock
Release a previously acquired lock. First parameter is the name of the
lock.
diff --git a/pm/functions.in b/pm/functions.in
index 2c258e6..bd649db 100644
--- a/pm/functions.in
+++ b/pm/functions.in
@@ -22,13 +22,13 @@ is_set "${PM_DEBUG}" && set -x
try_lock()
{
# $1 = file to use as lockfile
- # $2 (optional) content to write to the lockfile,
- # extra newline will be appended
+ local lock="${LOCKDIR}/${1##*/}"
+
# make sure the directory where the lockfile should be exists
mkdir -p "${LOCKDIR}"
- local lock="${LOCKDIR}/${1##*/}"
- # we use noclobber to make sure there are no race conditions
- (set -o noclobber; echo "${2}" > "${lock}") 2> /dev/null || return 1
+ touch "${lock}"
+ exec 3<"${lock}"
+ flock -x -n 3 || return 1
return 0
}
@@ -37,13 +37,15 @@ try_lock()
spin_lock()
{
# $1 = lockfile
- # $2 = optional timeout
- local elapsed=0
- while ! try_lock $1; do
- [ "x$2" != "x" ] && [ $(( $elapsed == $2 )) -ne 0 ] && return 1
- elapsed=$(($elapsed + 1))
- sleep 1;
- done
+ # $2 = optional timeout (default is 60 secs)
+ local lock="${LOCKDIR}/${1##*/}"
+ local timeout="${2:-60}"
+
+ mkdir -p "${LOCKDIR}"
+ touch "${lock}"
+ exec 3<"${lock}"
+ flock -x -w "${timeout}" 3 || return 1
+ return 0
}
# release the lock