From: Shawn Hargreaves Newsgroups: comp.os.msdos.djgpp Subject: Re: Interrupt Handling in C Date: Thu, 12 Dec 1996 20:10:34 +0000 Organization: None Lines: 31 Distribution: world Message-ID: References: NNTP-Posting-Host: talula.demon.co.uk MIME-Version: 1.0 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Peter Berdeklis writes: > >The DJGPP FAQ suggest that "bullet proof" interrupt handlers must be >written in ASM. What is it that prevents DJGPP from outputing bullet >proof code in C? Could someone give me some hints. As far as I am aware, the only major gotcha is the need to lock all the memory touched by the interrupt handler. This is an unfortunate result of the non-reentrancy of DOS. Say you have a timer interrupt handler which modifies a bit of memory, but that memory is currently swapped out to disk. The DPMI provider will attempt to swap it back in, but to do this it has to issue DOS file access commands. The trouble is that the program could have been already in the middle of executing a DOS function when the timer interrupt went off, in which case there will be two nested calls to DOS, and it will get very upset and trash your hard disk. A bad thing :-) To avoid this you need to use DPMI functions to lock _all_ the memory, both code and data, that you touch inside your handler. This is a big pain, particularly with complicated functions that call library routines, and it's much easier to see exactly what memory is used by asm code. However, it is possible to do write robust interrupt handlers in C if you are very careful. I wrote most of Allegro's interrupt code in C (I didn't fancy the idea of a 100% asm MIDI player :-) and it seems stable. You just have to litter the code with memory locking functions... /* * Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/ * Ghoti: 'gh' as in 'enough', 'o' as in 'women', and 'ti' as in 'nation'. */