home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / UTILS / HSBASIC2.DMS / in.adf / HB2Examples1.3.Lha / Examples / Tasks / SimpleTask.bas < prev   
Encoding:
BASIC Source File  |  1994-04-14  |  3.1 KB  |  105 lines

  1. ''
  2. '' $Id: SimpleTask.bas,v 1.2 1994/03/16 15:07:24 alex Rel $
  3. ''
  4. '' Task creation and co-ordination from BASIC
  5. ''
  6. '' Derived from RKM example (c) Copyright 1992 Commodore-Amiga, Inc.
  7. ''
  8.  
  9. DEFINT A-Z
  10.  
  11. ' multi-threaded code runs in several contexts, so the normal checks may
  12. ' crash correct programs - lets turn everything off
  13. REM $NOAUTODIM
  14. REM $NOARRAY
  15. REM $NOBREAK
  16. REM $NOOVERFLOW
  17. REM $NOEVENT
  18. REM $NOSTACK
  19.  
  20. 'REM $INCLUDE Exec.bh
  21. 'REM $INCLUDE Utility.bc
  22.  
  23. REM $INCLUDE BLib/ExecSupport.bas
  24. REM $INCLUDE BLib/HookEntryTask.bas
  25.  
  26. LIBRARY OPEN "exec.library", LIBRARY_MINIMUM&
  27.  
  28. CONST STACK_SIZE& = 1000
  29.  
  30. DIM SHARED sharedvar&, junk&
  31.  
  32. '
  33. ' The sub-task, the range of things this can do are pretty limited: it absolutely
  34. ' must not-ever call DOS (since you must be a process to do that), this means that
  35. ' many BASIC commands are out. It must not do anything which may cause non-reentrant
  36. ' BASIC runtimes to be reentered (e.g. string handling, dynamic array usage).
  37. '
  38. ' Basically you can call the OS do simple BASIC stuff and little else.
  39. '
  40. SUB simpletask
  41.     SHARED parent&, sigf_quitchild&
  42.     STATIC sigb_quitchild
  43.  
  44.     sigf_quitchild& = -1    ' so the parent knows we had a problem
  45.     sigb_quitchild = AllocSignal&(-1)
  46.     IF sigb_quitchild <> -1 THEN
  47.         sigf_quitchild& = 1& << sigb_quitchild
  48.         Signal parent&, SIGF_SINGLE&    ' signal our parent we're initialised
  49.         DO WHILE sharedvar& < &h7fffffff
  50.             INCR sharedvar&
  51.             IF SetSignal&(0, sigf_quitchild&) AND sigf_quitchild& THEN EXIT LOOP
  52.         LOOP
  53.         FreeSignal sigb_quitchild
  54.     END IF
  55.     Forbid        ' forcibly run us to completion
  56.     Signal parent&, SIGF_SINGLE&    ' signal our parent we're about to quit
  57. END SUB
  58.  
  59. SUB main
  60.     SHARED parent&, sigf_quitchild&
  61.     STATIC task&
  62.     
  63.     sharedvar& = 0
  64.     
  65.     parent& = FindTask&(NULL&)    ' so the child nows who its parent was
  66.     task& = CreateHookEntryTask&(SADD("SimpleTask" + CHR$(0)), 0, _
  67.       VARPTRS(simpletask), STACK_SIZE&)
  68.     IF task& <> NULL& THEN
  69.         junk& = SetSignal&(0, SIGF_SINGLE&)    ' ensure signal is clear
  70.         junk& = xWait&(SIGF_SINGLE&)        ' wait until task has started
  71.         IF sigf_quitchild& <> -1 THEN
  72.             PRINT "This program initialised a variable to zero, then started a"
  73.             PRINT "separate task which is incrementing that variable right now,"
  74.             PRINT "while this program waits for an event."
  75.             PRINT "Cause an event now (click in the window!)"
  76.             
  77.             SLEEP
  78.             
  79.             PRINT "The shared variable now equals"; sharedvar&
  80.             
  81.             ' Tasks created via CreateHookEntryTask must not be DeleteTask'd or
  82.             ' RemTask'd (since not all of the memory is attached to the MemLists)
  83.             ' So we first ask the child to terminate by giving it a signal which
  84.             ' it allocated, then borrow the SIGF_SINGLE signal to co-ordinate our
  85.             ' actions with our child task
  86.             
  87.             Signal task&, sigf_quitchild&        ' ask the child to terminate
  88.             
  89.             ' Whilst the RKMs say say you can't use any of the systems signal bits
  90.             ' SIGF_SINGLE can be legitimately used in this way; you clear the signal,
  91.             ' then immediately wait on it
  92.             
  93.             junk& = SetSignal&(0, SIGF_SINGLE&)    ' ensure signal is clear
  94.             junk& = xWait&(SIGF_SINGLE&)        ' and wait until it does quit
  95.         ELSE
  96.             PRINT "Child couldn't allocate a termination signal"
  97.         END IF
  98.     ELSE
  99.         PRINT "Can't create task"
  100.     END IF
  101. END SUB
  102.  
  103. main
  104. END
  105.