Path: utzoo!attcan!uunet!steinmetz!ge-dab!peora!rtmvax!johnc From: johnc@rtmvax.UUCP (John Connin) Newsgroups: comp.os.minix Subject: critical sections Keywords: Minix, interrupts, lock, restore Message-ID: <1804@rtmvax.UUCP> Date: 19 Jul 88 02:34:02 GMT Organization: Robert Talley (PRIVATE), Orlando, FL Lines: 73 I thought the following "critial section" code from Thomas Wagner's excellent public domain "Ctask" package, might be of interest. In addition to the clean notation, note that the 'lock' and 'restore' routines only operate on the psw interrupt bit -- leaving all other flags unaltered. Though restoring the whole psw as done currently in Minix and Xinu seems to work, only changing the interrupt flag seems to me like a better way of doing business. #define CRITICAL int crit_save #define C_ENTER crit_save = lock() #define C_LEAVE restore(crit_save) foo() { CRTIICAL; C_ENTER; while( .... ) { .... if ( .... ) { C_LEAVE: return; .... } C_LEAVE; } ;-------------------------------------------------------------------- ; ; int lock(void) ; ; Returns current state of the interrupt flag (1 if ints were ; enabled), then disables interrupts. ; _lock proc ; pushf ; cli ; pop ax ; extract and return the flag mov cl,9 ; interrupt bit. shr ax,cl ; and ax,1 ; ret ; ; ; _lock endp ; ;-------------------------------------------------------------------- ; ; void restore(int state) ; ; Enables interrupts if 'state' is nonzero. ; _restore proc ; push bp ; mov bp,sp ; mov ax,4[bp] ; ax - state pop bp ; or ax,ax ; if (state) sti jz _rstend ; sti ; _rstend: ; ret ; ; _restore endp ; BTW: Ctask is a public domain software package which enables multi-tasking within MSDOS programs. Ctask is available on BIX and is authored by Thomas Wagner, Patschkauer Weg 31, D-1000 Berlin 33, West Germany.