From d356d6f57c3cd35b078811110a79802f49ed9529 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 30 Aug 2021 20:44:02 +0200 Subject: global-functions: $ScriptLock: do not store but calculate job count This should mitigate some more race conditions. --- global-functions | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/global-functions b/global-functions index bbea143..f2b8ea1 100644 --- a/global-functions +++ b/global-functions @@ -925,6 +925,12 @@ :set ScriptLockOrder [ :toarray "" ]; } + :local JobCount do={ + :local Script [ :tostr $1 ]; + + :return [ :len [ / system script job find where script=$Script ] ]; + } + :local AddTicket do={ :local Script [ :tostr $1 ]; :local Add [ :tostr $2 ]; @@ -963,13 +969,11 @@ $LogPrintExit2 error $0 ("A script named '" . $Script . "' does not exist!") true; } - :local JobCount [ :len [ / system script job find where script=$Script ] ]; - - :if ($JobCount = 0) do={ + :if ([ $JobCount $Script ] = 0) do={ $LogPrintExit2 error $0 ("No script '" . $Script . "' is running!") true; } - :if ([ :len ($ScriptLockOrder->$Script) ] >= $JobCount) do={ + :if ([ :len ($ScriptLockOrder->$Script) ] >= [ $JobCount $Script ]) do={ $LogPrintExit2 error $0 ("More tickets than running scripts '" . $Script . "', resetting!") false; :set ($ScriptLockOrder->$Script); / system script job remove [ find where script=$Script ]; @@ -979,13 +983,12 @@ $AddTicket $Script $MyTicket; :local WaitCount 0; - :while ($WaitMax > $WaitCount && (($ScriptLockOrder->$Script->0) != $MyTicket || [ :len ($ScriptLockOrder->$Script) ] < $JobCount)) do={ + :while ($WaitMax > $WaitCount && (($ScriptLockOrder->$Script->0) != $MyTicket || [ :len ($ScriptLockOrder->$Script) ] < [ $JobCount $Script ])) do={ :delay 100ms; :set WaitCount ($WaitCount + 1); - :set JobCount [ :len [ / system script job find where script=$Script ] ]; } - :if ([ :len ($ScriptLockOrder->$Script) ] = $JobCount && ($ScriptLockOrder->$Script->0) = $MyTicket) do={ + :if (($ScriptLockOrder->$Script->0) = $MyTicket && [ :len ($ScriptLockOrder->$Script) ] = [ $JobCount $Script ]) do={ $RemoveTicket $Script $MyTicket; :return false; } -- cgit v1.2.3-70-g09d2