# #
###############################################################################
-function lock_acquire() {
- local lockfile="${1}"
- assert isset lockfile
+__lock_path() {
+ local name=${1}
+
+ if [ "${name:0:1}" = "/" ]; then
+ echo "${name}"
+ else
+ echo "${LOCK_DIR}/network-${name}"
+ fi
+}
+
+lock_exists() {
+ local name=${1}
+ assert isset name
+
+ local lockfile=$(__lock_path ${name})
+
+ if [ -e "${lockfile}" ]; then
+ return ${EXIT_TRUE}
+ else
+ return ${EXIT_FALSE}
+ fi
+}
+
+lock_acquire() {
+ local name=${1}
+ assert isset name
# timeout value in seconds
- local timeout=60
- timeout=$(( ${timeout} * 10 ))
+ local timeout=${2}
+
+ if ! isset timeout; then
+ timeout=0
+ fi
- log DEBUG "Acquiring lock '${lockfile}'."
+ local lockfile=$(__lock_path ${name})
- local free="false"
- while [ ${timeout} -gt 0 ]; do
- if [ ! -e "${lockfile}" ]; then
- free="true"
- break
- fi
+ timeout=$(( ${timeout} * 4 ))
+ log DEBUG "Acquiring lock '${name}'"
+
+ # Wait until lock is available
+ while [ ${timeout} -gt 0 ] && [ -e "${lockfile}" ]; do
timeout=$(( ${timeout} - 1 ))
- sleep 0.1
+ sleep 0.25
done
- assert ${free} "Could not acquire lock '${lockfile}'."
+ # If another lock still exists, we return an error
+ if [ -e "${lockfile}" ]; then
+ error "Could not acquire lock '${name}'"
+ return ${EXIT_ERROR}
+ fi
# Write out pid to the lockfile and make sure that
# nobody else can access it.
chmod 600 ${lockfile}
}
-function lock_release() {
- local lockfile="${1}"
+lock_release() {
+ local name=${1}
+ assert isset name
+
+ local lockfile=$(__lock_path ${name})
- log DEBUG "Releasing lock '${lockfile}'."
+ log DEBUG "Releasing lock '${name}'"
# Remove the lockfile (okay if it does not exist).
rm -f ${lockfile}