home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 106 / EnigmaAmiga106CD.iso / www / afc / afc-dir / tasker_all.lha / Tasker_Examples.lha / Examples / Tasker_Example2.e < prev   
Text File  |  1998-04-11  |  3KB  |  102 lines

  1. /*
  2.  
  3.   Tasker example 2  -  by Andrea Galimberti  -  (C) Brighting Brain Brothers
  4.  
  5. */
  6.  
  7. MODULE 'AFC/tasker',
  8.        'AFC/porttools',
  9.        'exec/ports',
  10.        'exec/memory',
  11.        'exec/nodes'
  12.  
  13.  
  14. -> a complex message
  15. OBJECT complex
  16.   mnode:mn  ->BE CAREFUL: This IS the structure, NOT the pointer
  17.   wow:LONG
  18. ENDOBJECT
  19.  
  20. -> the tasker structure IS DEFined global because we use it also in the
  21. -> process' body.
  22. DEF myp:PTR TO tasker
  23.  
  24. PROC thread()
  25.   DEF message:PTR TO complex
  26.  
  27. -> get the global data pointer, previously stored by the main process.
  28. -> IMPORTANT: DO this before using global variables OR functioncalls.
  29.   geta4()
  30.  
  31. /*
  32.   IF you DEF'd your tasker ('myp' in this CASE) as a global variable,
  33.   you can access its port by the dosport() method. IF you need TO make it a
  34.   local variable THEN, TO get the port associated TO the process, you
  35.   follow the standard way:
  36.  
  37.     DEF thisthread:PTR TO process
  38.     thisthread:=FindTask(0)
  39.     dosport:=thisthread.msgport
  40.  
  41.   Warning: the use OF this port IS reserved TO AmigaDOS, so create your
  42.            own port IF you need one.
  43. */
  44.  
  45.   -> create a port associated TO the process (this method must be called
  46.   -> from the task that needs the port, not by the main program).
  47.   myp.buildport()
  48.  
  49.   WaitPort(myp.port())
  50.   message:=GetMsg(myp.port())
  51.  
  52.   PrintF('Hello, I''m a NEW thread. Wow IS:\d\n', message.wow)
  53.   Delay(50)
  54.  
  55. -> make sure there IS no taskswitching after we replied the message
  56.   Forbid()
  57.   ReplyMsg(message)
  58.   myp.endport()  -> remember TO close the port before ending.
  59. ENDPROC
  60.  
  61. PROC main() HANDLE
  62.   DEF mainp=NIL, mess=NIL:PTR TO complex
  63.  
  64. -> Store the global data pointer: DO this BEFORE any process IS started.
  65. -> Remember TO geta4() in the process before using any global variable OR
  66. -> functioncall!
  67.   storea4()
  68.  
  69.   NEW myp.tasker('simpleprocess')  -> name AND initialization
  70.   myp.code({thread})   -> the code TO be used
  71.  
  72.   IF (mainp:=buildPort())  -> we need a port in the main() process TO
  73.                            -> communicate with thread()
  74.  
  75.     WriteF('Starting the process...\n')
  76.     myp.process()  -> starting the process
  77.  
  78.     mess:=NewM(SIZEOF complex, MEMF_PUBLIC OR MEMF_CLEAR) -> alloc AND
  79.     setupMsg(mess, SIZEOF complex, mainp)             -> setup the message
  80.     mess.wow:=10    -> fill in your additional fields
  81.  
  82.     WriteF('Sending the message...\n')
  83.     REPEAT              -> wait FOR the process TO create its own port
  84.       Delay(5)          -> before sending anything TO it.
  85.     UNTIL myp.port()
  86.  
  87.     myp.send(mess)  -> send the message TO thread()
  88.  
  89.     WaitPort(mainp)  -> wait FOR an answer.
  90. -> IF process doesn't reply we can't Dispose the message memory 'cause
  91. -> the thread may be still using it.
  92.   ELSE
  93.     WriteF('well, no port!\n')
  94.   ENDIF
  95.  
  96. EXCEPT DO
  97.   IF mess THEN Dispose(mess)  -> remember TO free the memory
  98.   IF mainp THEN endPort(mainp)
  99.   END myp
  100. ENDPROC
  101.  
  102.