home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ARM Club 3
/
TheARMClub_PDCD3.iso
/
hensa
/
misc
/
tornado
/
t2demo011a1
/
Guidelines
< prev
next >
Wrap
Text File
|
1996-12-17
|
5KB
|
97 lines
(Note that some of this refers to features not implemented as yet in the
support module)
Guidelines for writing multithreading applications:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
* Do NOT share handle-based resources between threads eg; one thread opens a
handle and then passes that handle to another thread and then terminates. If
you MUST do this, pass the handle to the other thread, and *enter* a wait
cycle on that thread after tidying up. Should that thread ever terminate
unnaturally, the thread that opened the handle will be exit that loop and
then should close the handle if it's still open. Conversely, should the other
thread successfully finish its work and no longer need the handle, it then
should terminate and let the thread that created it close it.
In other words, ALWAYS open and close a handle with the same instance of
thread. This is future-proofing as a future version of this support module
may shut handles for a thread if it is ever forcibly terminated.
* Do NOT linearly program. Mutexs and semaphores are there to be used - make
the main message handler *dispatch* the message by starting up a thread -
don't have the message handler handle the message itself where possible. This
allows maximum responsiveness to be retained.
* Make sure you 17, 18 & 19 poll code handler executes quickly as it is not
preempted and thus both your code and the entire RISC-OS Wimp is held up
until it finishes. Similarly to the point above, *dispatch* tasks into
threads whenever possible and signal your handler. You should NOT attempt to
call Wimp_Poll yourself during the handler.
* Do NOT take long to render your window changes/redraws ie; make the code
that redraws whatever between Tornado_RedrawWindow and Tornado_GetRectangle
as fast as possible as this code is not preempted
* Do note that almost all SWI calls cannot be preempted by this support
module and as a result any SWI that takes some time will hang the computer
for that time
* Do NOT call Wimp_Poll yourself. Also make sure anything you do won't end
up floating ie; if you send a wimp message, make sure your 17, 18 & 19
handler can handle it correctly (eg; notify you).
An example of this is:
<thread sends wimp message>
<thread now suspends itself *with* timeout>
<when wimp message returns into 17, 18 & 19 handler, handler resumes the
thread>
<all okay - unless wimp message is never replied to - in which case timeout
occurs and then thread should take whatever action required>
This is how you should handle wimp messages that require replies. However,
should the message exchange require no Wimp_Poll in between (eg; RAM
transfers), then you should have your 17, 18 & 19 handler do the arbitration
as it is the only part of your code that won't get preempted.
A nice effect of doing this is that suspending a thread leaves more time
for other threads too so it's best all ways you look at it.
* Do NOT call any of Wimp_RedrawWindow, Wimp_GetRectangle and
Wimp_UpdateWindow yourself. Use Tornado_RedrawWindow and Tornado_GetRectangle
instead (see their APIs for details).
* Do NOT write into memory areas outside your own slot (except your own
dynamic areas). Future versions of the support module may trap this and shut
down your application.
If you really must access memory in other areas eg; the RMA, switch into
supervisor first. This is difficult within C to do without an assembler
wrapper to help so this is the approach you should take - don't do it unless
you have to!
* Do NOT use Timer1 on the IOC/IOMB as it will be used by future support
module versions
* Do NOT have code within your slot (ie; application space) be called under
interrupts/events/RO handlers as the support module does not cater for it.
* Note that errors that occur within the preemption handlers get dumped
(when possible - it isn't always) onto the current environment's error
handler ie; onto the current application usually. When this happens, it will
appear to your code that an error has happened out of nowhere (it has
effectively) and also preemption is now *stopped*. Now the tornado support
module is in an inconsistant state.
Your code starting preemption again is possible - but it requires shutting
down preemption (via Tornado_Closedown) and restarting it again
(Tornado_Initialise). Remember this is not particularly advisable at ANY time
but this situation can be deemed sufficiently bad to warrent it. The effects
of doing this are to shut down all running threads without calling their exit
handlers and thus you risk losing a great deal of resources unless you're
careful. But it's unlikely you have much of a choice.
However, do NOT take this as an excuse to automatically restart tornado on
occurance of an error. You can test to see if it is a preemption handler
error using Tornado_NeedReset and if so take appropriate action. See that
SWI's entry for more details.
Memory full errors cause the preemptor routine to dump whatever
information it was trying to store in the block it was trying to claim ie;
when memory runs out applications using this module simply stop responding
and miss window redraw requests. This is preferable to attempting a
preemption reset which usually causes a vast memory leak.