home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / FORTH035.ZIP / THREADS.4TH < prev   
Text File  |  1993-05-21  |  2KB  |  54 lines

  1. \ THREADS.4TH  -  Thread creation under Forth/2   04/22/93
  2. \ Copyright (c) 1993  BLUE STAR SYSTEMS
  3. \
  4. \ These are some preliminary words to run threads under Forth/2 using
  5. \ the OS/2 function CreateThread.  Any output they do shows up in the 
  6. \ Forth/2 screen, but the output is harmless.
  7. \
  8. \ You can THREAD just about any word which operates independently and
  9. \ does not require any stack parameters.
  10.  
  11. decimal
  12.  
  13. variable ThreadInfo   \ Information to be passed to thread, USER area
  14.        4 allot        \   and execution address of thread
  15.  
  16. variable ThreadID
  17. variable ThreadArg              \ Allot parameter space after this
  18. 8192 constant ThreadStackSize
  19.  
  20. : usleep ( msec    -- )  SYS$SLEEP SYSCALL 2drop ;  \ Put thread to sleep
  21. : sleep  ( seconds -- )  1000 * usleep ;
  22.  
  23.  
  24. : StartThread ( tick_addr -- ) CFA @  ThreadInfo CELL+ !  'USER @ ThreadInfo !
  25.               ThreadInfo ThreadArg !
  26.               ThreadStackSize
  27.               0                \ Flags: 0=Start now, 1=Start suspended
  28.               ThreadArg
  29.           ['] ThreadProc CFA @
  30.               ThreadID           SYS$CREATETHREAD SYSCALL
  31.               6 DROPS ;
  32.  
  33.  
  34. : KillThread  ( pid -- )  SYS$KillThread SYSCALL 2drop ;
  35.  
  36. \ THREAD   Usage:  THREAD <name>  starts <name> running as a separate thread
  37.  
  38. : THREAD   State @ IF  POSTPONE [']  POSTPONE StartThread
  39.                  ELSE  '  StartThread  THEN ;  IMMEDIATE
  40.  
  41.  
  42. \ Examples
  43.  
  44. : Bunny            5 sleep ." It keeps going, and going..."
  45.            50 0 do 2 sleep cr   ." and going, and going..."
  46.                    loop ;
  47.  
  48. variable TimeLength
  49. : Timer ( -- )      TimeLength @ sleep  ." It's time! " ;
  50. : Alarm ( secs -- ) TimeLength !  THREAD Timer ;
  51.  
  52. \ Try  THREAD bunny    or  10 Alarm    Do a bunch of them!
  53.  
  54.