]> git.ipfire.org Git - people/jschlag/network.git/commitdiff
lock: refactoring functions
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Wed, 21 Jun 2017 12:07:12 +0000 (14:07 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Jun 2017 14:30:59 +0000 (16:30 +0200)
lock_acquire:
Refactor function to set the timeout manually
and to avoid the sleep of 0,25s when the timeout value is zero.

lock_exists:
Provides an easier to check if a lock exists

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.device
src/functions/functions.lock
src/udev/network-hotplug-rename

index fc88fecc53638d5088f4968d05a89170c513b3ed..09848e27f8f5bb54b40e73fd3600bdccf2395023 100644 (file)
@@ -910,7 +910,7 @@ device_auto_configure_smp_affinity() {
 
        local device=${1}
 
-       if lock_acquire "smp-affinity"; then
+       if lock_acquire "smp-affinity" 60; then
                device_set_smp_affinity ${device} auto
 
                lock_release "smp-affinity"
index fac86bddc336a6d1dd13d0e95f8c6ad683ccbaad..d9c3acfc11075576e9ca1b124238712686859dcf 100644 (file)
@@ -29,30 +29,47 @@ __lock_path() {
        fi
 }
 
-lock_acquire() {
+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=120
+       local timeout=${2}
+
+       if ! isset timeout; then
+               timeout=0
+       fi
+
+       local lockfile=$(__lock_path ${name})
+
        timeout=$(( ${timeout} * 4 ))
 
        log DEBUG "Acquiring lock '${name}'"
 
-       local free="false"
-       while [ ${timeout} -gt 0 ]; do
-               if [ ! -e "${lockfile}" ]; then
-                       free="true"
-                       break
-               fi
-
+       # Wait until lock is available
+       while [ ${timeout} -gt 0 ] && [ -e "${lockfile}" ]; do
                timeout=$(( ${timeout} - 1 ))
                sleep 0.25
        done
 
-       assert ${free} "Could not acquire lock '${name}'"
+       # 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.
index c56c70d1a4ff06ff48811173cb9d12d77da7f7a5..2d474ef55ce7f026c74bf1792741e1017f392270 100644 (file)
@@ -45,7 +45,7 @@ log DEBUG "Called for interface '${INTERFACE}'."
 device_exists ${INTERFACE} || exit ${EXIT_ERROR}
 
 # Acquiring lock for this operation.
-lock_acquire ${LOCKFILE}
+lock_acquire ${LOCKFILE} 120
 
 # Check if the device is already in use and
 # prevent the script to touch it in any way.