! Test program for "Copy Subprocess Data"

! This is the main module, containing an external routine MAIN and
! an external variable MAINVAR, which are referred to from another
! module (which contains an external routine EXT and an external
! variable EXTVAR, which are referred to from the main module).

! MAIN prints out the addresses and contents of MAINVAR and EXTVAR,
! EXT does the same and also calls MAIN.

! Various values are assigned to MAINVAR and EXTVAR inbetween calls
! to MAIN and EXT to demonstrate the identity or non-identity of
! variables in these two modules, both in the parent and child process.

%option "-low-nons-nowarn-nocheck-nodiag"
%include "inc:util.imp"
%include "mouse.inc"
%systemroutinespec copy subprocess data

%externalinteger mainvar=16_11111111
%externalintegerspec extvar
%externalroutinespec ext

%externalroutine main
  printstring("Process "); phex(a5);
  printstring(": (Main) mv @"); phex(addr(mainvar))
  printsymbol('='); phex(mainvar)
  printstring(", ev @"); phex(addr(extvar))
  printsymbol('='); phex(extvar); newline
%end

%begin
%label x
%ownrecord(semaphorefm)%name s
%ownrecord(pcbfm)%name p

! Create the child process

  s == createsemaphore(0)
  p == createsubprocess(8000,addr(x),"")

! Show variables, change them, show again

  main; ext  {should show initial values 11111111 and 22222222}
  mainvar = 16_33333333; extvar = 16_44444444; main; ext

! Pass control to child

  semaphorewait(s)

! When back from child

  main; ext; mainvar = 16_99999999; extvar = 16_aaaaaaaa; main; ext

! Hand over to child again

  signalsemaphore(s); semaphorewait(s)

! When back

  main; ext
  deletesubprocess(p)
  %stop

! This is where the child's program starts

x:main; ext; mainvar = 16_55555555; extvar = 16_66666666; main; ext

! Now make private copies of own data

  copy subprocess data

! And show what's different

  main; ext; mainvar = 16_77777777; extvar = 16_88888888; main; ext

! Hand control back to parent process

  signalsemaphore(s); semaphorewait(s)

! When it hands back to us

  main; ext; mainvar = 16_bbbbbbbb; extvar = 16_cccccccc; main; ext

! Hand back to parent again

  signalsemaphore(s); semaphorewait(s) {->k in main}

! Should not get here

  %cycle; %repeat
%end
