WorkerThreadsTest.mesa
Copyright Ó 1993 by Xerox Corporation. All rights reserved.
Created by Christian Jacobi, April 2, 1993
Christian Jacobi, April 20, 1993 6:37 pm PDT
This is used to debug the WorkerThreads package.
DIRECTORY
Commander,
CommanderOps,
Convert,
IO,
Process,
Rope,
TerminalIO,
WorkerThreads;
WorkerThreadsTest: CEDAR MONITOR
IMPORTS Commander, CommanderOps, Convert, IO, Process, Rope, TerminalIO, WorkerThreads ~
BEGIN
lastNumber: INT ¬ 0;
GetNumber: ENTRY PROC [] RETURNS [n: REF INT] = {
n ¬ NEW[INT ¬ lastNumber];
lastNumber ¬ lastNumber+1;
};
mustAbort: BOOL ¬ FALSE;
MustAbort: ENTRY PROC [] RETURNS [b: BOOL] = {
b ¬ mustAbort;
mustAbort ¬ FALSE
};
pool: WorkerThreads.Pool ¬ WorkerThreads.CreatePool[3];
pauseLength: INT ¬ 5000; --milliseconds
activities: LIST OF WorkerThreads.Activity ¬ NIL;
CreatePool: Commander.CommandProc ~ {
ENABLE Convert.Error => CommanderOps.Failed["needs integer percentage"];
i: INT;
i ¬ Convert.IntFromRope[CommanderOps.NextArgument[cmd]];
pool ¬ WorkerThreads.CreatePool[percentage: i];
TerminalIO.PutF1["Pool created: %g\n", IO.int[i]];
};
SetMSec: Commander.CommandProc ~ {
ENABLE Convert.Error => CommanderOps.Failed["needs integer milliseconds"];
i: INT;
i ¬ Convert.IntFromRope[CommanderOps.NextArgument[cmd]];
pauseLength ¬ i;
TerminalIO.PutF1["Pause set to: %g milliseconds\n", IO.int[i]];
};
ResetActivities: Commander.CommandProc ~ {
activities ¬ NIL;
mustAbort ¬ FALSE;
TerminalIO.PutRope["Activities reset\n"];
};
Abort: Commander.CommandProc ~ {
TerminalIO.PutRope["Set abort flag\n"];
mustAbort ¬ TRUE;
};
ForkOne: Commander.CommandProc ~ {
activity: WorkerThreads.Activity;
number: REF INT ¬ GetNumber[];
TerminalIO.PutF1["Fork no: %g\n", IO.int[number­]];
activity ¬ WorkerThreads.Fork[pool, Forkee, number];
activities ¬ CONS[activity, activities];
};
Forkee: PROC [x: REF] ~ {
number: REF INT ¬ NARROW[x];
TerminalIO.PutF1["Start perform: %g\n", IO.int[number­]];
Process.PauseMsec[pauseLength];
IF MustAbort[] THEN {
TerminalIO.PutF1["Will abort: %g\n", IO.int[number­]];
ERROR ABORTED
};
TerminalIO.PutF1["Finish perform: %g\n", IO.int[number­]];
};
Help1: Commander.CommandProc ~ {
al: LIST OF WorkerThreads.Activity ¬ activities;
IF al=NIL THEN IO.PutRope[cmd.out, "Failed Help1\n"] ELSE {
activities ¬ al.rest;
TerminalIO.PutRope["Start Help1\n"];
WorkerThreads.Perform[al.first];
TerminalIO.PutRope["Finish Help1\n"];
};
};
Wait: Commander.CommandProc ~ {
al: LIST OF WorkerThreads.Activity ¬ activities;
TerminalIO.PutRope["Start Wait\n"];
activities ¬ NIL;
WorkerThreads.Wait[al];
TerminalIO.PutRope["Finish Wait\n"];
};
Comment: Commander.CommandProc ~ {
r: Rope.ROPE ¬ CommanderOps.NextArgument[cmd];
r ¬ Rope.Cat["-- ", r, "\n"];
TerminalIO.PutRope[r];
};
Commander.Register["WorkerThreadsCreatePool", CreatePool, "Test of WorkerThreads"];
Commander.Register["WorkerThreadsSetMSec", SetMSec, "Test of WorkerThreads"];
Commander.Register["WorkerThreadsResetActivities", ResetActivities, "Test of WorkerThreads"];
Commander.Register["WorkerThreadsForkOne", ForkOne, "Test of WorkerThreads"];
Commander.Register["WorkerThreadsHelp1", Help1, "Test of WorkerThreads"];
Commander.Register["WorkerThreadsWait", Wait, "Test of WorkerThreads"];
Commander.Register["WorkerThreadsComment", Comment, "Test of WorkerThreads"];
Commander.Register["WorkerThreadsAbort", Abort, "Test of WorkerThreads"];
END.