home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff339.lzh / PCQ / Runtime.lzh / Runtime / Extras / CreateTask.p < prev    next >
Text File  |  1989-10-21  |  2KB  |  81 lines

  1. External;
  2.  
  3. {*
  4.  *  Create a task with given name, priority, and stack size.
  5.  *  It will use the default exception and trap handlers for now.
  6.  *}
  7.  
  8.  
  9. {$I "Include/Exec.i"}
  10. {$I "Include/Ports.i"}
  11.  
  12. {
  13.   This type is also defined in Tasks.i.  I redefined it here since Tasks.i
  14.   also declares CreateTask and DeleteTask as external routines.
  15. }
  16.  
  17. TYPE
  18.     Task = RECORD
  19.     tcNode        : Node;
  20.     tcFlags        : Byte;
  21.     tcState        : Byte;
  22.     tcIDNestCnt    : Byte;
  23.     tcTDNestCnt    : Byte;
  24.     tcSigAlloc    : Integer;
  25.     tcSigWait    : Integer;
  26.     tcSigRecvd    : Integer;
  27.     tcSigExcept    : Integer;
  28.     tcTrapAlloc    : Short;
  29.     tcTrapAble    : Short;
  30.     tcExceptData    : Address;
  31.     tcExceptCode    : Address;
  32.     tcTrapData    : Address;
  33.     tcTrapCode    : Address;
  34.     tcSPReg        : Address;
  35.     tcSPLower    : Address;
  36.     tcSPUpper    : Address;
  37.     tcSwitch    : Address;
  38.     tcLaunch    : Address;
  39.     tcMemEntry    : List;
  40.     tcUserData    : Address;
  41.     end;
  42.     TaskPtr = ^Task;
  43.  
  44. Function CreateTask(name : String; pri : Byte;
  45.             initPC : Address; stackSize : Integer) : TaskPtr;
  46. var
  47.     NewTask : TaskPtr;
  48.     DataSize: Integer;
  49. begin
  50.     DataSize := (stackSize and $fffffc) + 1;
  51.  
  52.     {
  53.     This should be broken into two allocations: task of PUBLIC
  54.     and stack of PRIVATE
  55.     }
  56.  
  57.     newTask := AllocMem(SizeOf(Task) + dataSize, MemClear + MemPublic);
  58.     if NewTask = Nil then
  59.     CreateTask := Nil;
  60.  
  61.     with NewTask^ do begin
  62.     tcSPLower := Address(Integer(newTask) + SizeOf(Task));
  63.     tcSPUpper := Address((Integer(tcSPLower) + dataSize) and $fffffe);
  64.     tcSPReg   := tcSPUpper;
  65.     with tcNode do begin
  66.         lnType := Ord(NTTask);
  67.         lnPri := pri;
  68.         lnName := name;
  69.     end;
  70.     end;
  71.     AddTask(newTask, initPC, Nil);
  72.     CreateTask := NewTask;
  73. end;
  74.  
  75.  
  76. Procedure DeleteTask(tc : TaskPtr);
  77. begin
  78.     RemTask(tc);  { does not handle self deletion properly }
  79.     FreeMem(tc, Integer(tc^.tcSPUpper) - Integer(tc));
  80. end;
  81.