home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 199.lha / LWP_v1.02 / lwptest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-27  |  4.5 KB  |  181 lines

  1.  
  2. /*
  3.  *  This routine test the light-weight process system.
  4.  *
  5.  *  This ought to work compiled with 16 or 32 bit ints
  6.  *
  7.  *  Note:
  8.  *    The link register, A5, MUST be setup C-Style if you are using LWPs
  9.  *    from assembly.    The LWP procedure should NOT modify ANY registers
  10.  *    A2-A6/D2-D7 before calling ForkLWP() BECAUSE ForkLWP() will use
  11.  *    A5 to skip the procedure's normal return function and do a double
  12.  *    return to its parent.
  13.  *
  14.  *    Aztec does this.  I am sure Lattice does this too.  The only
  15.  *    allowed register for the data base pointer is A4.
  16.  *
  17.  *  Operation: Initializing a LWP.  Read the LWP docs. Essentially, you
  18.  *  create a subroutine which is to be the LWP:
  19.  *
  20.  *  long
  21.  *  xx(arg, arg ...) {
  22.  *    <local variable declarations>
  23.  *    ForkLWP(stacksize, argsize)
  24.  *
  25.  *  }
  26.  *
  27.  *  And then call xx().  Calling xx() 'forks' off an LWP.  xx() will call
  28.  *  which returns directly to main() (i.e. the caller of xx()) after copying
  29.  *  xx()'s local variables and arguments to a new stack which is large
  30.  *  enough to hold them.  This new stack will be allocated.  Additional
  31.  *  space may be specified by specifying a stacksize in ForkLWP().  If the
  32.  *  subroutine makes no calls, 0 may be specified (unless the compiler
  33.  *  uses the stack for temporaries).  The 'argsize' is the number of bytes
  34.  *  taken up by arguments to xx(), which ForkLWP() cannot figure out on
  35.  *  its own.
  36.  *
  37.  *  So, ForkLWP() copies the stack areas and sets up a run context for the
  38.  *  subroutine.  When light weight processing is begun by a RunLWP() command,
  39.  *  the initial context is just after a 'return' from ForkLWP() so the rest
  40.  *  of the subroutine is run under the new stack as a light weight process.
  41.  *
  42.  */
  43.  
  44. #include <exec/types.h>
  45.  
  46. extern APTR ThisLWP;
  47. extern APTR ForkLWP();
  48.  
  49. #define INTSIZE sizeof(int)
  50.  
  51. main()
  52. {
  53.     int x = 43;
  54.  
  55.     puts("testing run ability");
  56.     xx("abc4", 4);
  57.     xx("def8", 8);
  58.     xx("ghi6", 6);
  59.     RunLWP();
  60.     puts("successful run test");
  61.     puts("");
  62.     puts("now testing waiting ability");
  63.     master(4);
  64.     RunLWP();
  65.     puts("Successfull signal test");
  66.     puts("");
  67.     puts("Now testing inter-subroutine stack-resize");
  68.     TestForkLWP("Awesome");
  69.     RunLWP();
  70.     puts("inter-subroutine stack-resize test done");
  71. }
  72.  
  73. xx(str, n)
  74. char *str;
  75. int n;
  76. {
  77.     short i;
  78.  
  79.     if (ForkLWP(4096L, (long)(4+INTSIZE))) {
  80.     printf("nonzero return");
  81.     return;
  82.     }
  83.     puts("zero go");
  84.     for (i = 0; i <= n; ++i) {
  85.     printf("%s %d\n", str, i);
  86.     SwitchLWP();
  87.     }
  88. }
  89.  
  90. master(n)
  91. int n;
  92. {
  93.     char quit = 0;
  94.     short i;
  95.     short j;
  96.     APTR slave();
  97.     APTR slave_lwp;
  98.  
  99.     if (ForkLWP(4096L, (long)INTSIZE)) {
  100.     puts("non zero return");
  101.     return;
  102.     }
  103.     puts("zero go");
  104.     slave_lwp = slave(ThisLWP, &quit);
  105.     for (i = 0; i < n; ++i) {
  106.     printf("Signal slave %d %08lx\n", i, slave_lwp);
  107.     AlertLWP(slave_lwp);
  108.     WaitLWP();
  109.     puts("slave should not run START");
  110.     for (j = 0; j < 10; ++j)
  111.         SwitchLWP();        /*  make sure slave doesn't run */
  112.     puts("slave should not run END");
  113.     }
  114.     quit = 1;
  115.     puts("Signal slave, which should now exit");
  116.     AlertLWP(slave_lwp);
  117.     puts("Master waits");
  118.     WaitLWP();
  119.     puts("Master exits");
  120. }
  121.  
  122. APTR
  123. slave(master_lwp, quit)
  124. APTR master_lwp;
  125. char *quit;
  126. {
  127.     short j;
  128.     APTR lwp;
  129.  
  130.     if (lwp = ForkLWP(4096L, 8L)) {
  131.     puts("nonzero ret");
  132.     return(lwp);
  133.     }
  134.     puts("zero go");
  135.  
  136.     for (;;) {
  137.     puts("slave: wait");
  138.     WaitLWP();
  139.     puts("master should not run START");
  140.     for (j = 0; j < 10; ++j)
  141.         SwitchLWP();        /*  make sure slave doesn't run */
  142.     puts("master should not run END");
  143.     if (*quit)
  144.         break;
  145.     printf("slave alerts master %08lx\n", master_lwp);
  146.     AlertLWP(master_lwp);
  147.     }
  148.     puts("Slave flagged to quit, slave alerting master and exiting");
  149.     AlertLWP(master_lwp);
  150. }
  151.  
  152. TestForkLWP(str)
  153. char *str;
  154. {
  155.     long i = 1000000L;
  156.     register long j;        /*    cannot init. reg vars before ForkLWP()  */
  157.  
  158.     if (ForkLWP(2048L, 4L)) /*  initial stack big enough for stdio  */
  159.     return;
  160.     j = 0;
  161.     printf("string %s\n", str);
  162.     printf("LWP %08lx local i @ %08lx,  1000000 == %ld\n", ThisLWP, &i, i);
  163.     if (ForkLWP(2048L, 4L)) /*  prove locals are not destroyed      */
  164.     return;
  165.     printf("string %s\n", str);
  166.     printf("LWP %08lx local i @ %08lx,  1000000 == %ld\n", ThisLWP, &i, i);
  167.     puts("long wait...");
  168.     if (ForkLWP(0L, 0L))      /*  passed argument 'str' now invalid   */
  169.     return;
  170.  
  171.     while (i--)
  172.     ++j;
  173.  
  174.     if (ForkLWP(2048L, 0L)) /* NOTE: 'str' WOULD STILL BE INVALID HERE  */
  175.     return;         /* even if you specified '4'                */
  176.  
  177.     printf("LWP %08lx local i @ %08lx,  -1 == %ld\n", ThisLWP, &i, i);
  178.     printf("j == 1000000: %d\n", j);
  179. }
  180.  
  181.