;       SCCSID = @(#)shutdown.inc       6.2 91/08/28
;      SCCSID = @(#)shutdown.inc        6.2 91/08/28
;
; put these in the right include files
;
;   Modification History:
; @0081 08/28/91  ROC  PTR    CP20 AB00081 Added a Shutdown flag to be used by
;                                          Shutdown and nampipes.
;       04/02/96  TPL              150101  Fix the above defect related to
;                                          usage of the ShutdownFlag


FSBlockIDHigh   equ     9999h           ; normal file system caller block ID
FSBlockIDLow    equ     8888h
SDBlockIDHigh   equ     7777h           ; shutdown thread block ID
SDBlockIDLow    equ     6666h

DoShutdownFc    equ     00h             ; function code for shutdown

StopCounters    equ     01h             ; bit 0
StopNonCount    equ     02h             ; bit 1
StopSFLocks     equ     04h             ; bit 2
StopAll         equ     StopCounters OR StopNonCount OR StopSFLocks
NotShutdown     equ     00h             ; all bits clear

ShutdownBegin   equ     00h             ; tell FSD/DEVs shutdown is starting
ShutdownEnd     equ     01h             ; tell FSD/DEVs shutdown is completing

;150101 ShutdownFlags   STRUC                   ;  @0081
;150101 ShutdownFlag    DW      0000h           ;  @0081
;150101 ShutdownFlags   ENDS                    ;  @0081
extrn           _ShutdownFlag:DWORD                                     ;150101


;**     ?GENLABEL - generate a label
;
;       The label generated has the form &name&_&cnt
;
;       ENTRY:  name = a string to be used as part of the label.
;               cnt  = a number to be used as part of the label.
;
;       EXIT:   a label generated
;
;       SEE ALSO: ?JMPLABEL

?GENLABEL macro name,cnt                  ;; generate a label
    &name&_&cnt:                          ;; create the label
endm

;**     ?JMPLABEL - jump to a generated label
;
;       The label jumped to has the form &name&_&cnt
;
;       ENTRY:  name = a string to be used as part of the label.
;               cnt  = a number to be used as part of the label.
;
;       EXIT:   a label generated
;
;       SEE ALSO: ?GENLABEL

?JMPLABEL macro jtype,name,cnt            ;; generate a jump to a label
    &jtype      &name&_&cnt               ;; jump to the label
endm

;;**     ShutdownGate - Generate a check for system shutdown
;;
;;       The macro is used to call DOS_ functions that do not
;;       always lock SFTs with SFLOCK.  It checks for system
;;       shutdown and if it finds it, it arrests all threads
;;       that should not access the FAT file system while in
;;       shutdown.
;;
;;       This macro sets up a call to ShutdownCheck, the routine
;;       that does the work of blocking/allowing access to the
;;       file system routines during shutdown.  It also sets up
;;       a call to WakeShutdownThread.
;;
;;       ENTRY:  dosfunc - the function to be callfar'ed
;;               outlbl - the exit label for the function
;;               fscseg - segment to use to access FSC segment
;;
;;       EXIT:   a system shutdown check point is created
;;
;;       USES:   flags
;;

?sdgate = 0         ; counter for ShutdownGate

ShutdownGate    macro   dosfunc, outlbl, fscseg

    ?sdgate = ?sdgate + 1
    ifb <fscseg>
        %out ShutdownGate, No segment specified
        .ERR
    else
        push    GSEL GDT_FSC
        pop     fscseg

        cmp     fscseg:[fss_ShutdownFlags],NotShutdown
        ?JMPLABEL <jne short>,<sdbegin>,%?sdgate

        ?GENLABEL <sdcont>,%?sdgate
        inc     fscseg:[fss_SDWaitCount]
        SaveReg <fscseg>
        CALLFAR dosfunc
        RestoreReg  <fscseg>
        pushf
        cmp     fscseg:[fss_ShutdownFlags],NotShutdown
        ?JMPLABEL <jne short>,<sdend>,%?sdgate

        dec     fscseg:[fss_SDWaitCount]
        ?GENLABEL <sdexit>,%?sdgate
        popf
        jmp     short outlbl

        ?GENLABEL <sdbegin>,%?sdgate
        invoke  ShutdownCheck
        ?JMPLABEL <jmp short>,<sdcont>,%?sdgate


        ?GENLABEL <sdend>,%?sdgate
        dec     fscseg:[fss_SDWaitCount]
        ?JMPLABEL <jnz short>,<sdexit>,%?sdgate

        invoke  WakeShutdownThread
        popf
        jmp     short outlbl

    endif
endm
