home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / exec / tasks / simpletask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  3.5 KB  |  113 lines

  1. ;/* simpletask.c - Execute me to compile me with Lattice 5.04
  2. LC -b1 -cfistq -v -y -j73 simpletask.c
  3. Blink FROM LIB:c.o,simpletask.o TO simpletask LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /* SimpleTask.c - simple task creation example
  8.  *
  9.  * Copyright (c) 1990 Commodore-Amiga, Inc.
  10.  *
  11.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  12.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  13.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  14.  * information on the correct usage of the techniques and operating system
  15.  * functions presented in this example.  The source and executable code of
  16.  * this example may only be distributed in free electronic form, via bulletin
  17.  * board or as part of a fully non-commercial and freely redistributable
  18.  * diskette.  Both the source and executable code (including comments) must
  19.  * be included, without modification, in any copy.  This example may not be
  20.  * published in printed form or distributed with any commercial product.
  21.  * However, the programming techniques and support routines set forth in
  22.  * this example may be used in the development of original executable
  23.  * software products for Commodore Amiga computers.
  24.  * All other rights reserved.
  25.  * This example is provided "as-is" and is subject to change; no warranties
  26.  * are made.  All use is at your own risk.  No liability or responsibility
  27.  * is assumed.
  28.  */
  29.  
  30. #include <exec/types.h>
  31. #include <exec/memory.h>
  32. #include <exec/tasks.h>
  33. #include <libraries/dos.h>
  34. #ifdef LATTICE
  35. #include <proto/all.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. int CXBRK(void) { return(0); }        /* Disable Lattice CTRL/C handling */
  39. int chkabort(void) { return(0); }    /* really */
  40. #endif
  41.  
  42. #define STACK_SIZE 1000L
  43.  
  44. /* Task name, pointers for allocated task struct and stack */
  45. APTR stack = NULL;
  46. struct Task *tc = NULL;
  47. char *simpletaskname = "MySimpleTask";
  48.  
  49. ULONG sharedvar;
  50.  
  51. void simpletask(void);
  52. void cleanup(void);
  53. void cleanexit(UBYTE *,LONG);
  54.  
  55.  
  56. void main(int argc,char **argv)
  57.     {
  58.     if((stack = AllocMem(STACK_SIZE, MEMF_CLEAR)) == NULL)
  59.         cleanexit("Not enough memory for task stack\n",RETURN_FAIL);
  60.  
  61.     if ((tc = (struct Task *)
  62.         AllocMem(sizeof(struct Task),MEMF_CLEAR | MEMF_PUBLIC)) == NULL)
  63.             cleanexit("Not enough memory for task structure\n",RETURN_FAIL);
  64.  
  65.     /* Initialize necessary fields, others were cleared by MEMF_CLEAR */
  66.     tc->tc_Node.ln_Type = NT_TASK;
  67.     tc->tc_Node.ln_Name = simpletaskname;
  68.     tc->tc_SPLower = (APTR)stack;
  69.     tc->tc_SPUpper = (APTR)(STACK_SIZE + (ULONG)stack);
  70.     tc->tc_SPReg   = tc->tc_SPUpper;
  71.  
  72.     sharedvar = 0L;
  73.     AddTask(tc, simpletask, 0L);
  74.  
  75.     printf("This program initialized a variable to zero, then started a\n");
  76.     printf("separate task which is incrementing that variable right now,\n");
  77.     printf("while this program waits for you to press RETURN.\n");
  78.     printf("Press RETURN now: ");
  79.     getchar();
  80.  
  81.     printf("The shared variable now equals %ld\n",sharedvar);
  82.     /* We can simply remove the task we added because our simpletask
  83.      * does not make any system calls which could cause it to be awakened
  84.      * or signalled later.
  85.      */
  86.     RemTask(tc);
  87.  
  88.     cleanup();
  89.     exit(RETURN_OK);
  90.     }
  91.  
  92. void simpletask()
  93.     {
  94.     while(sharedvar < 0x8000000) sharedvar++;
  95.     /* Wait forever because main() is going to RemTask() us */
  96.     Wait(0L);
  97.     }
  98.  
  99. void cleanexit(s,e)
  100. UBYTE *s;
  101. LONG e;
  102.     {
  103.     if(*s) printf(s);
  104.     cleanup();
  105.     exit(e);
  106.     }
  107.  
  108. void cleanup()
  109.     {
  110.     if(tc) FreeMem(tc,sizeof(struct Task));
  111.     if(stack) FreeMem(stack,STACK_SIZE);
  112.     }
  113.