Skip to content

Commit

Permalink
Tiny fixes in lib/ipc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Reale committed Mar 9, 2018
1 parent 4ed38d3 commit 0474177
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/ipc/flock
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function bashlets_ipc_flock_acquire()
{
local lockfile="$1"
exec 9> "$lockfile"
flock -n 9 || echo "cannot acquire lock"
[[ -f $lockfile ]] && echo "lock acquired" >&2
flock -n 9 || { echo "cannot acquire lock"; return 1; }
[[ -f $lockfile ]]
}

#@method
Expand All @@ -55,7 +55,7 @@ function bashlets_ipc_flock_release()
function bashlets_ipc_flock_exists()
{
local lockfile="$1"
return [[ -e $lockfile ]]
[[ -e $lockfile ]]
}


Expand Down
20 changes: 11 additions & 9 deletions lib/ipc/plock
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
#
################################################################################

#
# TODO: remove dependency on bc
# TODO: fix trap clause

# the maximum lifetime of a lock file, in seconds; when expired, the lock
# is assumed to be a stale one
#
Expand Down Expand Up @@ -100,17 +102,17 @@ function bashlets_ipc_plock_acquire()
# we record in the lock file itself our pid, our name
# and the timestamp
#
echo "$$:`basename $0`:`date +%s`" > "$lockfile" )
echo "$$:$(basename "$0"):$(date +%s)" > "$lockfile" )
then
#
# if we fail, the lock must be already held by another process;
# let us look at its contents, however
#
lock_contents=`cat $lockfile 2>/dev/null`
pid=`echo $lock_contents | sed 's/^\([0-9]*\):.*/\1/'`
cmd=`echo $lock_contents | cut -d':' -f2`
timestamp=`echo $lock_contents | sed 's/^.*:.*:\([0-9]*\)$/\1/'`
now=`date +%s`
lock_contents=$(cat "$lockfile" 2>/dev/null)
pid=$(echo $lock_contents | sed 's/^\([0-9]*\):.*/\1/')
cmd=$(echo $lock_contents | cut -d':' -f2)
timestamp=$(echo $lock_contents | sed 's/^.*:.*:\([0-9]*\)$/\1/')
now=$(date +%s)
#
# the pid and name recorded must correspond to a running
# process; moreover, the timestamp must not be too
Expand All @@ -120,8 +122,8 @@ function bashlets_ipc_plock_acquire()
# that the lock is a stale one
#
if [[ -z $pid || -z $cmd || -z $timestamp ||
-z `ps -C $cmd -o pid= | grep $pid` ||
`echo $now - $timestamp | bc` -ge $__bashlets_ipc_plock_max_lifetime ]]
-z $(ps -C $cmd -o pid= | grep $pid) ||
$(echo $now - $timestamp | bc) -ge $__bashlets_ipc_plock_max_lifetime ]]
then
rm -f $lockfile
may_start=1
Expand Down

0 comments on commit 0474177

Please sign in to comment.