[Pm-utils] [patch commit] [000] POSIXification of pm-utils

Dan Nicholson dbn.lists at gmail.com
Sun Jan 27 17:47:24 PST 2008


On Jan 27, 2008 12:25 PM, Victor Lowther <victor.lowther at gmail.com> wrote:
> Adds a generic locking mechanism built around directory creation/removal.

The timeout support in spin_lock doesn't seem like it would work.

+# spin waiting for the lock with optional timeout.
+# return once we have it, or the timeout has expired
+spin_lock()
+{
+	# $1 = directory to use as the lock directory
+	# $2 = optional timeout
+	local elapsed=0
+	while ! try_lock $1; do
+		sleep 1;
+		[ "x$2" != "x" ] && [ $(( $elapsed == $2 )) -ne 0 ] && return 1
+	done
+}

$elapsed never seems to be incremented. I also don't believe that you
want to sleep before checking the timeout. This results in a `sleep 1'
even if you haven't specified a timeout; i.e., you want to return
immediately. Personally, I would write it like this (trying not to let
too much personal style seep in):

spin_lock()
{
  local elapsed=0
  local timeout=${2:-0}

  while ! try_lock $1; do
    [ $timeout -le $elapsed ] && return 1
    elapsed=$(( $elapsed + 1 ))
    sleep 1
  done
}

What do you think?

--
Dan


More information about the Pm-utils mailing list