* ======================================================================
*                                ValidWait
*                          William S. Hawes (1988)
*                       >>> Freely Distributable <<<
* ======================================================================
* Checks the system TaskReady and TaskWait lists for the Validator process,
* and waits for it to finish.  Use this program in your startup-sequence
* before executing any programs that may write to a disk being validated.
* For example, starting WorkBench with the LoadWB command may write a file
* to your system disk, possibly causing an extended Validation Adventure.

         INCLUDE  "exec/types.i"
         INCLUDE  "exec/macros.i"
         INCLUDE  "exec/tasks.i"
         INCLUDE  "exec/interrupts.i"
         INCLUDE  "exec/execbase.i"

         XREF     _AbsExecBase

         XLIB     CloseLibrary
         XLIB     Disable
         XLIB     Enable
         XLIB     FindName
         XLIB     OpenLibrary
         XLIB     Permit

         XLIB     Delay

start    movea.l  _AbsExecBase,a6

         lea      DOSLib(pc),a1
         moveq    #LIBRARY_VERSION,d0
         CALLSYS  OpenLibrary
         move.l   d0,d5                ; "guaranteed"

         ; Wait for a few ticks

Wait     moveq    #3,d1                ; 60 milliseconds
         exg      d5,a6
         CALLSYS  Delay
         exg      d5,a6

         ; Check for the validator process

         CALLSYS  Disable              ; no interrupts
         lea      TaskReady(a6),a0     ; ready list
         lea      Validate(pc),a1      ; validator process
         CALLSYS  FindName
         move.l   d0,d2                ; found?
         bne.s    Check                ; yes

         lea      TaskWait(a6),a0      ; waiting list
         lea      Validate(pc),a1
         CALLSYS  FindName
         move.l   d0,d2                ; save node

Check    CALLSYS  Enable               ; reenable switching
         tst.l    d2                   ; task node found?
         bne.s    Wait                 ; yes

         movea.l  d5,a1
         CALLSYS  CloseLibrary

         moveq    #0,d0                ; no errors
         rts

DOSLib   dc.b     'dos.library',0
Validate dc.b     'Validator',0        ; the validator process

         END
