From:	AITGW::"MACRO32@WKUVX1.BITNET" 20-MAR-1992 20:37:23.12
To:	macro32@WKUVX1.BITNET
CC:	
Subj:	Interesting trick with AST routines.

Received: by AITGW.DECnet (utk-mail11 v1.5) ; Fri, 20 Mar 92 20:36:31 EST
Received: from ukcc.uky.edu by aitgw.ge.com (5.65/GE Gateway 1.5)
	id AA04589; Fri, 20 Mar 92 20:36:26 -0500
Received: from ukcc.uky.edu by UKCC.uky.edu (IBM VM SMTP V2R2)
   with BSMTP id 2176; Fri, 20 Mar 92 20:29:48 EST
Received: from UKCC by ukcc.uky.edu (Mailer R2.08) with BSMTP id 4010; Fri, 20
 Mar 92 20:29:27 EST
Received: from WKUVX1.BITNET by ukcc.uky.edu (Mailer R2.08) with BSMTP id 3924;
 Fri, 20 Mar 92 20:28:22 EST
Errors-To: MacroMan@WKUVX1.BITNET
X-Listname: "VMS Internals, MACRO, and BLISS Discussions"
    <MACRO32@WKUVX1.BITNET>
Received: from NAUVAX (ACM) by WKUVX1 (MX V3.0A) with BSMTP; Fri, 20 Mar 1992
          19:20:47 CST
Received: from NAUVAX.UCC.NAU.EDU by NAUVAX.UCC.NAU.EDU (PMDF #12311) id
          <01GHVKHUY1DC000T9X@NAUVAX.UCC.NAU.EDU>; Fri, 20 Mar 1992 18:11 MST
Date: Fri, 20 Mar 1992 18:11 MST
From: "Anthony C. McCracken" <ACM@NAUVAX.BITNET>
Reply-To: MACRO32@WKUVX1.BITNET
Subject: Interesting trick with AST routines.
To: macro32@WKUVX1.BITNET
Message-Id: <01GHVKHUY1DC000T9X@NAUVAX.UCC.NAU.EDU>
X-Envelope-To: macro32@wkuvx1.bitnet
X-Vms-To: in%"macro32@wkuvx1.bitnet"
 
   After studying the VAX/VMS Internals manual in detail and combining that
with knowledge gleaned by poking around via the debugger in the system
executive I've discovered the following, totally unsupported, "feature" of
ASTs and AST routines.
   It is possible to code an AST routine that can be interupted by another AST
at the same access mode.  I've helped many a student programmer discover why
their programs hang by locating such bad practices as calling $HIBER from
within an AST or waiting for an event flag that gets set by another AST routine.
   The problem is that generally an AST can only be interupted by another AST
from a higher access mode.  It's possible to dismiss the AST from inside the
AST thus allowing interuptions by other ASTs in such a way that the final RET
instruction can properly clean up the stack.  A sample code segment follows.
 
        .TITLE  AST TEST
        .PSECT  DATA,RD,WRT,NOEXE
 
DELAY:  .LONG   -10*1000*1000*2 ; 2 second delay for $SETIMR
        .LONG   -1
MD:     .ASCID  /SYS$OUTPUT/    ; Should be your terminal
MCH:    .BLKW   1               ; I/O channel
MYBUFF: .BLKB   20              ; Buffer for I/O
MYSIZ = .-MYBUFF                ; Buffer size
 
        .PSECT  CODE,RD,NOWRT,EXE
        .ENTRY  MAIN,^M<>
        $ASSIGN_S       CHAN=MCH,DEVNAM=MD              ; Get the keyboard
        $SETIMR_S       DAYTIM=DELAY,ASTADR=AST1        ; Set a timer and
        $HIBER_S                                        ; Go to sleep
        $EXIT_S                                         ; That's all folks
 
;+
;  AST1 gets executed when the timer goes off.  It will make itself AST
;  interuptable in it's first two instructions.  Then it will queue a READ
;  to the terminal and then wait for an event flag to be set.  The event flag
;  gets set in the AST for the terminal read.  Finally $WAKE is called and
;  then AST1 returns.  The main thread gets control and exits.
;-
        .ENTRY  AST1,^M<R2,R3,R4,R5>
        MOVAB   MYASTRET,16(FP) ; Replace EXE$ASTRET with MYASTRET in the call
        CHMK    #0              ; frame and dismiss the AST now.
        $CLREF_S        #5
        $QIO_S  CHAN=MCH,FUNC=#IO$_READVBLK,ASTADR=AST2,P1=MYBUFF,P2=#MYSIZ
        $WAITFR_S       EFN=#5  ; AST2 will set event flag 5
        $WAKE_S
        RET             ; Not to EXE$ASTRET but to MYASTRET
 
;+
;  AST2 is called when the I/O request from AST1 completes.  It just sets an
;  event flag and returns to AST1 who is waiting on the flag.
;-
        .ENTRY  AST2,^M<>
        $SETEF_S        EFN=#5
        RET
 
;+
;  MYASTRET is a munged version of EXE$ASTRET.  The only difference is that a
;  call to EXE$ASTEXIT (via CHMK #0) isn't performed.  It's already been done
;  in the AST.  The stack cleanup instructions are duplicated exactly.
;-
MYASTRET:
        ADDL2   #8,SP           ; Remove number of args and the AST parameter
        MOVQ    (SP)+,R0        ; Restore R0 and R1
        REI                     ; Return to the main process thread (or AST)
 
        .END    MAIN
 
 
Tony McCracken
 
 
+----------------------------------------+-----------------------------------+
| Anthony C. McCracken                   |                                   |
| Northern Arizona University            | Boom boom boom,,, Still going !   |
| Flagstaff, Arizona                     |                                   |
|                                        | VMS just keeps going and going and|
| acm@nauvax                   BitNet    | going.  Nothing outlasts VAX/VMS. |
| acm@nauvax.ucc.nau.edu       Internet  |                                   |
| 602.523.9422.us.west         Noise net |                                   |
+----------------------------------------+-----------------------------------+
