home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 10.ddi / CHESS.ZIP / TASKMGR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  3.6 KB  |  150 lines

  1. {************************************************}
  2. {                                                }
  3. {   Chess - Shared DLL Example                   }
  4. {   CHESS.DLL Task managment.                    }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. { This unit allocates and manages a separate stack that is used to
  10.   preform the game searches.   It is *very* important that, when running
  11.   under Windows, Windows calls are made while in the child task.  Many
  12.   Windows calls use the stack to determine the task handle and the stack
  13.   must be the DLL's client's stack or Windows will become confused. }
  14.  
  15. unit TaskMgr;
  16.  
  17. {$R-,Q-,G+,S-,W-}
  18.  
  19. interface
  20.  
  21. type
  22.   { Stores the state of the stack   }
  23.   TTaskInfo = record
  24.     SPtr, SSeg, Size: Word;
  25.   end;
  26.  
  27.   TSpawnProc = procedure;
  28.  
  29.  
  30. { Allocate the stack used by the game task }
  31. procedure AllocateTask(Size: Word);
  32.  
  33. { Dispose of the stack allocated by AllocateTask }
  34. procedure DisposeTask;
  35.  
  36. { Spawn a mini process which will call the given function upon start-up. }
  37. procedure Spawn(Proc: TSpawnProc);
  38.  
  39. { Suspend the current task waiting for a message from another task.  The
  40.   return value of this function is the Msg paramter of the Message call
  41.   perfomed by the other task. Each task will alternate (coopertivly
  42.   multitask) calling Message. }
  43. function Message(Msg: Word): Word;
  44.  
  45. implementation
  46.  
  47. {$IFDEF WINDOWS}
  48. uses GameRec, OMemory;
  49. {$ELSE}
  50. uses GameRec, Memory;
  51. {$ENDIF}
  52.  
  53. procedure AllocateTask(Size: Word);
  54. var
  55.   P : Pointer;
  56. begin
  57.   P := MemAllocSeg(Size);
  58.   if P = nil then Exit;
  59.   with CC do
  60.   begin
  61.     GameStack.SSeg := Seg(P^);
  62.     GameStack.Size := Size;
  63.     GameStack.SPtr := Size - 2;
  64.     AppStack.SSeg  := SSeg;
  65.   end;
  66. end;
  67.  
  68.  
  69. procedure DisposeTask;
  70. begin
  71.   with CC.GameStack do
  72.     if SSeg <> 0 then FreeMem(Ptr(SSeg, 0), Size);
  73.   FillChar(CC.GameStack, 0, SizeOf(CC.GameStack));
  74. end;
  75.  
  76.  { TerminateTask kills the game stack and RETurns to the ret addr  }
  77.  { already on App stack, presumeably, the last caller of Resume.   }
  78.  { The Game stack only shares a common starting point with the App }
  79.  { stack - after that, it runs to completion without trying to     }
  80.  { splice its execution back into the App stack.                   }
  81.  
  82. procedure TerminateTask(Dummy: Word); far; assembler;
  83. asm
  84.         { Switch to the App's stack }
  85.     MOV    SS, CC.AppStack.SSeg
  86.     MOV    SP, CC.AppStack.SPtr
  87.         MOV    BP, SP
  88. end;
  89.  
  90. function Message(Msg: Word): Word; assembler;
  91. asm
  92.     MOV    AX, Msg
  93.     MOV    BX, SS
  94.     CMP    BX, CC.AppStack.SSeg
  95.     JNE    @@1
  96.  
  97.         { Switch to game stack }
  98.     MOV    CC.AppStack.SPtr, SP
  99.  
  100.     MOV    SS, CC.GameStack.SSeg   { Now on the new stack }
  101.     MOV    SP, CC.GameStack.SPtr
  102.     JMP    @@2
  103.  
  104.         { Back to the App stack }
  105. @@1:    MOV    CC.GameStack.SPtr, SP
  106.  
  107.     MOV    SS, CC.AppStack.SSeg     { Now on the new stack }
  108.     MOV    SP, CC.AppStack.SPtr
  109. @@2:
  110.     MOV    BP, SP
  111. end;
  112.  
  113.  
  114.  
  115. procedure Spawn( Proc: TSpawnProc); assembler;
  116. asm
  117.     MOV    AX, SS
  118.  
  119.     { only an App can Spawn a game }
  120.     CMP    AX, CC.AppStack.SSeg
  121.     JNE    @@1
  122.  
  123.         { Setup task stack }
  124.         LES    DI,DWORD PTR CC.GameStack.TTaskInfo.SPtr
  125.         STD
  126.  
  127.         {  Returning out will terminate task }
  128.         MOV    AX, SEG TerminateTask
  129.         STOSW
  130.         MOV    AX, OFFSET TerminateTask
  131.         STOSW
  132.  
  133.         { Setup ret to "call" proc }
  134.         MOV    AX, Proc.Word[2]
  135.         STOSW
  136.         MOV    AX, Proc.Word[0]
  137.         STOSW
  138.         MOV    AX, DI            { Fake BP }
  139.         STOSW
  140.         CLD
  141.         MOV    CC.GameStack.TTaskInfo.SPtr,AX
  142.  
  143.         PUSH    0
  144.         CALL    Message
  145. @@1:
  146. end;
  147.  
  148. end.
  149.  
  150.