home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d079 / task.lha / Task / Task.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-03  |  2.3 KB  |  122 lines

  1. /*
  2.  * Task.c - a cheap sub-task example by cs
  3.  *   Cheap =  shared data for communication rather than MsgPorts
  4.  *   If using Aztec, #define AZTEC_C
  5.  *
  6.  *   Forgot to tell you - If you are using Lattice, you must use the -v
  7.  *   flag on LC2 to disable Lattice's insertion of stack-checking code.
  8.  *   That code will generate an alert if you have inline code which runs
  9.  *   as a separate task, process, handler, etc.
  10.  *
  11.  *   If you are using Manx, compile long (-l ?) and link with the 32-bit
  12.  *   library.  With a bit of work on the example, this might not be necessary.
  13.  *
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/tasks.h>
  18.  
  19. extern VOID subTaskRtn();
  20.  
  21. struct Task *CreateTask(), *subTaskPtr = 0L;
  22. char *subTaskName = "SubTask";
  23.  
  24. /* Data shared by main and subTaskRtn */
  25. ULONG  GfxBase = 0L;
  26. ULONG  Counter = 0L;
  27. LONG   PrepareToDie;
  28.  
  29. /* If using Aztec, #define AZTEC_C 
  30.  *
  31.  * Note: Aztec C v3.4a has built in functions that do the following
  32.  *  for you: geta4() and sava4().
  33.  */
  34.  
  35. #ifdef AZTEC_C
  36. #asm
  37.         cseg
  38.         public  _a4sav
  39. _a4sav
  40.         lea.l    _a4tmp,a0
  41.         move.l  a4,(a0)
  42.         rts
  43.  
  44.         public  _a4get
  45. _a4get
  46.         lea.l   _a4tmp,a0
  47.         move.l  (a0),a4
  48.         rts
  49.  
  50.         public  _a4tmp
  51.     
  52. _a4tmp   dc.l    0
  53.         
  54. #endasm
  55. #endif
  56.  
  57.  
  58. main()
  59.    {
  60.    int k;
  61.    ULONG ct;
  62.  
  63.    if(!(GfxBase=OpenLibrary("graphics.library",0)))
  64.       cleanexit("Can't open graphics.library");
  65.  
  66.    PrepareToDie = FALSE;
  67.  
  68. #ifdef AZTEC_C
  69.    a4sav();
  70. #endif
  71.  
  72.    subTaskPtr = CreateTask(subTaskName,0,subTaskRtn,2000);
  73.    if (!subTaskPtr)  cleanexit("Can't create subTask");
  74.  
  75.    for (k=0; k<10; k++)
  76.       {
  77.       Delay(50);  /* main is a process and can call Delay() */
  78.       ct = Counter;
  79.       printf("Counter = %ld\n",ct);
  80.       }
  81.  
  82.    cleanup();
  83.    }
  84.  
  85. cleanexit(s)
  86. char *s;
  87.    {
  88.    if(*s)  printf("%s\n",s);
  89.    cleanup();
  90.    exit(0);
  91.    }
  92.  
  93. cleanup()
  94.    {
  95.    if(subTaskPtr)
  96.       {
  97.       PrepareToDie = TRUE;
  98.       while(PrepareToDie);
  99.       DeleteTask(subTaskPtr);
  100.       }
  101.    if(GfxBase)  CloseLibrary(GfxBase);
  102.    }
  103.  
  104.  
  105. /* subTaskRtn increments Counter every 1/60 second */
  106. VOID subTaskRtn()
  107.    {
  108. #ifdef AZTEC_C
  109.    a4get();
  110. #endif
  111.  
  112.    while(!PrepareToDie)
  113.       {
  114.       WaitTOF();
  115.       Counter++;
  116.       }
  117.    PrepareToDie = FALSE;  /* Signal ready to die */
  118.    Wait(0L);              /* Wait while ax falls */
  119.    }
  120.  
  121.  
  122.