home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / Vector18.lha / ParNetExample / lock.asm < prev    next >
Encoding:
Assembly Source File  |  1993-12-18  |  3.1 KB  |  135 lines

  1. ;
  2. ; $Header: DH0:src/asm/parallel/parnet/RCS/lock.asm,v 1.1 92/08/27 17:15:38 Barnard Exp $
  3. ;
  4. ;
  5.  
  6. ;/*
  7. ; * This code was originally written by Matthew Dillon and put into Public Domain
  8. ; *
  9. ; * All changes concerning the adaption of Matt's original code to the
  10. ; * Vector Connection I/O board are © 1991-1993 by Henning Schmiedehausen
  11. ; * All rights for this changes are reserved. The original code is Public Domain
  12. ; *
  13. ; * This code is distributed with the expressed written permission of Matthew
  14. ; * Dillon (Thank you very much, Matt)
  15. ; *
  16. ; */
  17.  
  18.  
  19.         ;   long var[2] = { 0, 0 };
  20.         ;
  21.         ;   These routines provide fast exclusive
  22.         ;   locks.
  23.         ;
  24.         ;   LockAddr(&var[0]:A0)
  25.         ;   UnlockAddr(&var[0]:A0)
  26.         ;   TryLockAddr(&var[0]:A0)
  27.  
  28.         section __MERGED,DATA
  29.  
  30.         xref    _SysBase
  31.  
  32.         section text,CODE
  33.  
  34.         include "exec/types.i"
  35.         include "exec/ports.i"
  36.         include "exec/tasks.i"
  37.         include "exec/execbase.i"
  38.         include "exec/ables.i"
  39.         include "exec/memory.i"
  40.  
  41.         xref    _LVOWait
  42.         xref    _LVOForbid
  43.         xref    _LVOPermit
  44.         xref    _LVOSignal
  45.  
  46.         xdef    _LockAddr
  47.         xdef    _UnlockAddr
  48.         xdef    _TryLockAddr
  49.  
  50. _TryLockAddr:
  51.         move.l    4(sp),A0
  52.         moveq.l #0,D0
  53.  
  54.         bset.b    D0,4(A0)                ; attempt to gain lock
  55.         bne    .tla10            ; failure
  56.         moveq.l #1,D0
  57.         rts                ; success, return 1
  58. .tla10        moveq.l #-1,D0            ; failure, return -1
  59.         rts
  60.  
  61. _LockAddr:                    ; bit: 0     lock: A0
  62.         move.l    4(sp),A0
  63.         moveq.l #0,D0
  64.  
  65.         bset.b    D0,4(A0)                ; attempt to gain lock
  66.         bne    .la10            ; failure
  67.         rts                ; success, done, rts (FAST)
  68.  
  69. .la10        movem.l A2/A6,-(sp)             ; failure (SLOW) (BLOCK)
  70.         move.l    _SysBase,A6        ; A6 = SYSBase
  71.         FORBID
  72.         bset.b    D0,4(A0)                ; try again after FORBID
  73.         beq    .la20            ; got it!
  74.  
  75.         ;   Put linked list structure on stack
  76.  
  77.         move.w    D0,-(sp)                ; bit#    12(sp)
  78.         move.l    ThisTask(A6),-(sp)      ; task#    8(sp)
  79.         move.l    A0,-(sp)                ; &var     4(sp)
  80.         move.l    (A0),-(sp)              ; Next      (sp)
  81.         move.l    sp,(A0)                 ; (put at head of list)
  82.  
  83.         ;   Loop Wait/re-attempt lock
  84.  
  85. .la15        moveq.l #$10,D0         ; wait (semaphore signal)
  86.         jsr    _LVOWait(A6)
  87.  
  88.         move.w    12(sp),D0               ; try for lock
  89.         move.l    4(sp),A0
  90.         bset.b    D0,4(A0)
  91.         bne    .la15            ; loop until get it
  92.  
  93. .la16        cmp.l    (A0),sp                 ; unlink, find our node!
  94.         beq    .la18
  95.         move.l    (A0),A0
  96.         bra    .la16
  97. .la18        move.l    (sp),(A0)
  98.         add.w    #14,sp
  99. .la20
  100.         PERMIT
  101.         movem.l (sp)+,A2/A6
  102.         rts
  103.  
  104. _UnlockAddr:    move.l    4(sp),A0
  105.         moveq.l #0,D0
  106.  
  107.         bclr.b    D0,4(A0)                ; clear lock bit
  108.         move.l    (A0),D1                 ; anybody waiting?
  109.         beq    .ulrts            ; no, rts
  110.  
  111.         movem.l D2/A2/A6,-(sp)          ; yes, wake 'm all up
  112.         move.b    D0,D2            ; D2 = bit#
  113.         move.l    _SysBase,A6        ; A6 = SYSBase
  114.         FORBID
  115.  
  116.         move.l    (A0),D1                 ; get pointer again after FORBID
  117.         beq    .ul20            ; no, rts (close a window)
  118.  
  119. .ul10        move.l    D1,A2            ; A2 = node
  120.         cmp.b    13(A2),D2               ; waiting on our bit #?
  121.         bne    .ul18            ; no
  122.         move.l    8(A2),A1                ; yes, signal the node
  123.         moveq.l #$10,D0
  124.         jsr    _LVOSignal(A6)          ; signal EVERYONE waiting
  125. .ul18        move.l    (A2),D1                 ; next
  126.         bne    .ul10
  127.  
  128. .ul20
  129.         PERMIT
  130.         movem.l (sp)+,D2/A2/A6
  131. .ulrts        rts
  132.  
  133.         END
  134.  
  135.