mtxlib.gms : Test basics of the mutex library

Description

Contributor: Clemens Westphal, November 2015


Small Model of Type : GAMS


Category : GAMS Test library


Main file : mtxlib.gms

$Title 'Test basics of the mutex library'  (MTXLIB,SEQ=689)

$ontext
Contributor: Clemens Westphal, November 2015
$offtext


*Library is available on windows and linux only
$if not %system.buildcode% == VS8 $if not %system.buildcode% == WEI $if not %system.buildcode% == LEG $if not %system.buildcode% == DEG $exit

$call rm -f critical.txt

*generate a random mutex ID
$eval mtxID round(frac(jnow)*24*60*60*1000)

$onecho > client.gms
$funclibin mtxlib mtxcclib
function Lock        / mtxlib.Lock /
         Unlock      / mtxlib.Unlock /
         TryLock     / mtxlib.TryLock /
         Delete      / mtxlib.Delete /
         TimedLock   / mtxlib.TimedLock /;

scalar s;
s = Lock(%mtxID%);
file f /'critical.txt'/;
f.ap=1;
put f;
put "client%number%: a" /;
putclose;
s = Sleep(1);
put f;
put "client%number%: b" /;
putclose;
s = Sleep(1);
put f;
put "client%number%: c" /;
putclose;
s = Unlock(%mtxID%);
$offecho

$call.async gams client.gms --number=1 lo=2 lf=log1.txt
$eval jh JobHandle
$call gams client.gms --number=2 lo=2 lf=log2.txt

$label l1
$eval x sleep(1)
$eval status JobStatus(%jh%)
$if %status% == 1 $goto l1

$onecho >criticalExpected1.txt
client1: a
client1: b
client1: c
client2: a
client2: b
client2: c
$offecho

$onecho >criticalExpected2.txt
client2: a
client2: b
client2: c
client1: a
client1: b
client1: c
$offecho

*clean up the mutex
$funclibin mtxlib mtxcclib
function Delete      / mtxlib.Delete /;
display$Delete(%mtxID%) 'Problems removing mutex';

$call diff criticalExpected1.txt critical.txt > %system.nullfile%
$if errorlevel 1 $call diff criticalExpected2.txt critical.txt > %system.nullfile%
$if errorlevel 1 $abort 'critical.txt not as expected'

$call rm -f critical.txt