[comp.sys.mac.programmer] Multifinder and Interrupts

mikeoro@hubcap.clemson.edu (Michael K O'Rourke) (10/11/89)

I have a MF compatible, or rather it should be, program that i am writing.
It uses appletalk and has completion routines.  My question is this :

  If i am in the background and enter the iocompletion routine, is it
  possible to get interrupted or swapped out halfway thru the completion
  routine?  I am having some funky errors when running in the background
  under multifinder and can't seem to figure out what is going on.

  Symtoms :  program is running fine in background, accepts a few packets
             then suddenly the program comes to foreground for a second
              and then goes backt o background.  I know this ain't much to
             go on, but it ain't giving me much.  If anyone has had this happen
             i'd love to know what caused it.

Thanx,

Michael O'rourke

rmh@apple.com (Rick Holzgrafe) (10/12/89)

In article <6732@hubcap.clemson.edu> mikeoro@hubcap.clemson.edu (Michael K 
O'Rourke) writes:
>   If i am in the background and enter the iocompletion routine, is it
>   possible to get interrupted or swapped out halfway thru the completion
>   routine?  I am having some funky errors when running in the background
>   under multifinder and can't seem to figure out what is going on.

Your completion routine may be called from a higher-than-normal interrupt 
level, but not at the highest possible level. A higher-level interrupt 
can, um, interrupt you. But this shouldn't bother you any more than *your* 
interrupt bothers whoever you interrupted. MultiFinder in particular won't 
swap processes without being deliberately called: don't make any "Event" 
calls (e.g. WaitNextEvent, GetNextEvent) and you won't be yielding control 
to MultiFinder.

I don't know what your specific problem is, but here's some general hints:

+ Keep completion routines SHORT. Best is to quickly store results 
someplace where your main-line code can find them, and get out.

+ Minimize your stack usage in completion routines. You don't know whose 
stack you're running in, or whose heap you'll trash if you overflow.

+ You can't call the Memory Manager from a completion routine. This means 
you can't call ANY routine which may move or purge memory: see the 
appendix in Inside Mac for a list of these routines. (I bet you knew that 
one already. :-)

+ You can make new asynchronous calls from a completion routine. (But 
beware of the pitfalls mentioned in these tips.) You cannot make 
synchronous calls from completion routines.

+ Be aware that your completion routine can (in some cases) be called 
BEFORE your async PBControl call returns! Don't rely on having any time 
after the PBControl to get ready for the completion. And if you're making 
new async calls in your completion routines, beware of "recursion" and 
stack overflow.

+ Remember that your completion routine can be interrupted by a 
higher-priority interrupt, and that it will itself be interrupting
your main-line code. You may need to protect critical code or data.
If you do this (typically by temporarily forcing the interrupt level
high), do it as rarely and as quickly as you can.

+ Be sure your code segment containing the completion routine is loaded,
locked, and unpurgeable. Don't unload it!

+ Be careful about A5 (access to global variables, and calls to routines
outside the segment). A5 is not guaranteed correct in a completion routine.

+ Don't allocate your parameter blocks as local variables of routines
which may return before the completion.

Hope this helps.

==========================================================================
Rick Holzgrafe              |    {sun,voder,nsc,mtxinu,dual}!apple!rmh
Software Engineer           | AppleLink HOLZGRAFE1          rmh@apple.com
Apple Computer, Inc.        |  "All opinions expressed are mine, and do
20525 Mariani Ave. MS: 27-O |    not necessarily represent those of my
Cupertino, CA 95014         |        employer, Apple Computer Inc."

Hueras@cup.portal.com (Jon F Hueras) (10/12/89)

Your completion routine may possibly be interrupted, but no MF swaps should
occur unless you are calling GetNextEvent/WaitNextEvent/EventAvail from
within the completion routine (which you definitely do *not* want to be
doing).