home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / dev / gcc / libnixV0_8.lha / gnu / libnix-sources.lha / sources / amiga / misc / CreateTask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  3.1 KB  |  146 lines

  1. #include <stabs.h>
  2. #if 0
  3. /*
  4.    compiles with inline only in large data! AddTask() uses too many
  5.    nonscratch registers :( use of assembler version is recommended
  6. */
  7. #include <exec/tasks.h>
  8. #include <exec/memory.h>
  9. #include <exec/execbase.h>
  10. #ifdef __GNUC__
  11. #define BASE_NAME *(struct ExecBase **)4
  12. #include <inline/exec.h>
  13. #else
  14. #include <clib/exec_protos.h>
  15. #endif
  16. #include <clib/alib_protos.h>
  17.  
  18. extern struct ExecBase *SysBase;
  19.  
  20. struct newMemList
  21. {
  22.   struct Node nml_Node;
  23.   UWORD nme_NumEntries;
  24.   struct MemEntry nml_ME[2];
  25. };
  26.  
  27. const struct newMemList MemTemplate =
  28. { {0,},
  29.   2,
  30.   { {MEMF_CLEAR|MEMF_PUBLIC, sizeof(struct Task)},
  31.     {MEMF_CLEAR, 0} }
  32. };
  33.  
  34. extern inline void NewList(struct List *list)
  35. {
  36.    LONG *p;
  37.  
  38.    list->lh_TailPred=(struct Node*)list;
  39.    ((LONG *)list)++;
  40.    p=(LONG *)list; *--p=(LONG)list;
  41. }
  42.  
  43. struct Task *CreateTask(STRPTR name, LONG pri, APTR initpc, ULONG stacksize)
  44. {
  45.   struct Task *newtask,*task2;
  46.   struct newMemList nml;
  47.   struct MemList *ml;
  48.  
  49.   stacksize=(stacksize+3)&~3;
  50.   {
  51.     long *p1,*p2;
  52.     int i;
  53.  
  54.     for (p1=(long *)&nml,p2=(long*)&MemTemplate,i=7; i; *p1++=*p2++,i--) ;
  55.     *p1=stacksize;
  56.   }
  57.   if (!(((unsigned int)ml=AllocEntry((struct MemList *)&nml)) & (1<<31)))
  58.   {
  59.     newtask=ml->ml_ME[0].me_Addr;
  60.     newtask->tc_Node.ln_Type=NT_TASK;
  61.     newtask->tc_Node.ln_Pri=pri;
  62.     newtask->tc_Node.ln_Name=name;
  63.     newtask->tc_SPReg=(APTR)((ULONG)ml->ml_ME[1].me_Addr+stacksize);
  64.     newtask->tc_SPLower=ml->ml_ME[1].me_Addr;
  65.     newtask->tc_SPUpper=newtask->tc_SPReg;
  66.     NewList(&newtask->tc_MemEntry);
  67.     AddHead(&newtask->tc_MemEntry,(struct Node *)ml);
  68.     task2=(struct Task *)AddTask(newtask,initpc,0);
  69.     if (SysBase->LibNode.lib_Version>36 && !task2)
  70.     {
  71.       FreeEntry(ml); newtask=NULL;
  72.     }
  73.   }
  74.   else
  75.     newtask=NULL;
  76.  
  77.   return newtask;
  78. }
  79.  
  80. #else
  81. asm("
  82.         .globl    _CreateTask
  83.  
  84. _CreateTask:    moveml    d2/d3/a2/a3/a4/a6,sp@-
  85.         link    a5,#-32
  86.         movel    a5@(12+8*4:W),d0
  87.         addql    #3,d0
  88.         moveq    #-4,d3
  89.         andl    d0,d3            | stack adjusted
  90.         lea    pc@(Lmemlist-.+2),a0
  91.         movel    sp,a1
  92.         moveq    #6,d0
  93. L3:        movel    a0@+,a1@+        | copy memlist
  94.         dbra    d0,L3
  95.         movel    d3,a1@
  96.         movel    sp,a0
  97.         movel    4:W,a6
  98.         jsr    a6@(-222:W)        | AllocEntry()
  99.         movel    d0,a2
  100.         movel    a2,d2
  101.         bmis    L2
  102.         movel    a2@(16:W),a4
  103.         moveb    #1,a4@(8:W)        | ln_Type
  104.         moveb    a5@(7+8*4:W),a4@(9:W)    | ln_Pri
  105.         movel    a5@(0+8*4:W),a4@(10:W)    | ln_Name
  106.         movel    a2@(24:W),d0
  107.         addl    d0,d3
  108.         movel    d3,a4@(54:W)        | tc_SPReg
  109.         movel    d0,a4@(58:W)        | tc_SPLower
  110.         movel    d3,a4@(62:W)        | tc_SPUpper
  111.         lea    a4@(74:W),a0        | tc_MemList
  112.         movel    a0,a0@(8:W)
  113.         addql    #4,a0
  114.         movel    a0,a0@-
  115.         movel    a2,a1
  116.         jsr    a6@(-240:W)        | AddHead()
  117.         movel    a4,a1
  118.         movel    a5@(8+8*4:W),a2
  119.         subal    a3,a3
  120.         jsr    a6@(-282:W)        | AddTask()
  121.         cmpw    #37,a6@(20:W)
  122.         bcss    L1            | kick2.0+ ?
  123.         tstl    d0
  124.         bnes    L1            | task added
  125.         movel    d2,a0
  126.         jsr    a6@(-228:W)        | FreeEntry()
  127. L2:        subal    a4,a4
  128. L1:        movel    a4,d0            | new task
  129.         unlk    a5
  130.         moveml    sp@+,d2/d3/a2/a3/a4/a6
  131.         rts
  132.  
  133. Lmemlist:    .long    0,0            | Succ,Pred
  134.         .byte    0,0            | Pri,Type
  135.         .long    0            | Name
  136.         .word    2            | NumEntries
  137.         .long    0x10001            | MemType
  138.         .long    92            | Length
  139.         .long    0x10000            | MemType
  140. |        .long    0            | Length
  141.  
  142. ");
  143. #endif
  144.  
  145. ALIAS (DeleteTask,RemTask);
  146.