home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / forth040.zip / THREADS.4TH < prev   
Text File  |  1994-01-20  |  2KB  |  68 lines

  1. \ THREADS.4TH  -  Thread creation under Forth/2   03/18/93
  2. \ Copyright (c) 1993,1994  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. \ Warning: The thread does not start immediately, so be carefull of any
  12. \          variables used to pass parameters to the new thread.
  13. \
  14. \  i.e.    1 alarm 2 alarm 10 alarm
  15. \
  16. \  will PROBABLY not result in the correct results, but rather all
  17. \  three alarms going off in 10 seconds
  18.  
  19. decimal
  20.  
  21. variable ThreadInfo   \ Information to be passed to thread, USER area
  22.        4 allot        \   and execution address of thread
  23.  
  24. variable ThreadID
  25. variable ThreadArg              \ Allot parameter space after this
  26. 8192 constant ThreadStackSize
  27. variable ThreadStack  ThreadStackSize allot
  28.  
  29. : sleep  ( seconds -- )  1000 * MS ;
  30.  
  31. : StartThread ( code_addr -- )
  32.     ThreadInfo CELL+ !  'USER @ ThreadInfo !
  33.               ThreadInfo ThreadArg !
  34.               ThreadStackSize
  35.               0                \ Flags: 0=Start now, 1=Start suspended
  36.               ThreadArg
  37.               ['] ThreadProc
  38.               ThreadID           SYS$CREATETHREAD
  39.                                  SYSCALL
  40.               6 DROPS ;
  41.  
  42. : KillThread  ( pid -- )  SYS$KillThread SYSCALL 2drop ;
  43.  
  44. \ THREAD   Usage:  THREAD <name>  starts <name> running as a separate thread
  45.  
  46. : THREAD   State @ 0= If
  47.              ' StartThread
  48.            Else
  49.              ' Postpone Literal
  50.              Postpone StartThread
  51.            Then
  52.            ; IMMEDIATE
  53.  
  54. \ Examples
  55.  
  56. : Bunny            5 sleep ." It keeps going, and going..."
  57.            50 0 do 2 sleep cr   ." and going, and going..."
  58.                    loop ;
  59.  
  60. variable TimeLength
  61.  
  62. : Timer ( -- )      TimeLength @ sleep  ." It's time! " ;
  63.  
  64. : Alarm ( secs -- ) TimeLength ! Thread Timer ;
  65.  
  66. \ Try  THREAD bunny    or  10 Alarm    Do a bunch of them!
  67.  
  68.