global-functions: $ScriptLock: do not store but calculate job count

This should mitigate some more race conditions.
This commit is contained in:
Christian Hesse 2021-08-30 20:44:02 +02:00
parent 6b04fff3eb
commit d356d6f57c

View file

@ -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;
}