home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / parallel / 2761 < prev    next >
Encoding:
Internet Message Format  |  1992-12-17  |  4.2 KB

  1. Path: sparky!uunet!olivea!spool.mu.edu!sdd.hp.com!ncr-sd!ncrcae!hubcap!fpst
  2. From: agn@bovic.Eng.Sun.COM (Andreas G. Nowatzyk)
  3. Newsgroups: comp.parallel
  4. Subject: Re: Wanted: efficient software lock
  5. Summary: Software Lock without atomic instructions
  6. Keywords: mutual exclusion, memory models, locks
  7. Message-ID: <liv2dcINN6c5@exodus.Eng.Sun.COM>
  8. Date: 16 Dec 92 19:55:24 GMT
  9. References: <1992Dec15.134502.7384@hubcap.clemson.edu>
  10. Sender: fpst@hubcap.clemson.edu (Steve Stevenson)
  11. Reply-To: agn@bovic.Eng.Sun.COM
  12. Organization: Sun Microsystems, Inc.
  13. Lines: 100
  14. Approved: parallel@hubcap.clemson.edu
  15. Nntp-Posting-Host: bovic
  16.  
  17. In article 7384@hubcap.clemson.edu, gottlieb@allan.ultra.nyu.edu (Allan Gottlieb) writes:
  18. In article <1992Dec14.133731.18997@hubcap.clemson.edu> engler@cs.arizona.edu (Dawson R. Engler) writes:
  19.  
  20. >>   I need the algorithm for an efficient software lock that does not use
  21. >>   atomic instructions (except perhaps assignment).  Fairness is unnecessary:
  22. >>   in the situation I need this for, ~90% of the accesses are by the same
  23. >>   process, and if the accesses are by someone else, they are allowed to
  24. >>   have extra overhead.
  25. >>   ...
  26. >
  27. > I would suggest peterson's algorithm.  For two processes it is simply
  28. > Code for process 1          Code for process 2
  29. > ------------------          ------------------
  30. > P1Wants <- TRUE                   P2Wants <- TRUE
  31. > Turn <- P2                        Turn <- P1
  32. > while P2Wants and Turn=P2        while P1Wants and Turn=P1
  33. >
  34. > critical section                  critical section
  35. >
  36. > P1Wants <- FALSE                  P2Wants <- FALSE
  37. >
  38. > (the while loops have empty bodies)
  39. >
  40. > For N processes (plus a proof and references) see Hofri's article in
  41. > Operating Systems Review January 90.
  42. >
  43. > Allan Gottlieb
  44.  
  45. It might be of interest to note that the above algorithm (and many similar ones)
  46. assume that memory operations are sequentially consistent (SC). There is a significant
  47. performance advantage for systems that provide SC memory only when needed. These
  48. weaker memory models generally require ordering or synchronization instructions
  49. to appear SC.
  50.  
  51. For example, if Peterson's algorithm were used on a Sparc based multiprocessor,
  52. without using an atomic instruction (swap, test-and-set or compare-and-swap), you
  53. would still need ordering instructions. In the current version 8, this means adding a store-barrier instruction *and* a "swap" that is used as a load-barrier (it is not possible to implement this particular algorithm without "swap" or "ldstub" on V8
  54. machines). Eventually, in the next version of the Sparc architecture, a generalized
  55. ordering directive (membar) can be used to implement Peterson's Algorithm correctly
  56. in all supported memory models (TSO, PSO and RMO).
  57.  
  58. [ The idealized Sparc assembly code below was verified with a tool that can prove
  59.   assertions for small pieces of code for all future Sparc based
  60.   multiprocessors. The critical section increments a shared variable [A] such
  61.   that lack of mutual exclusion would allow a sequence of events that results in
  62.   A != 2. Given that all possible execution paths are considered, A == 2 is a
  63.   usefull assertion that the critical section is properly guarded. ]
  64.  
  65. /*
  66.  * Peterson's Algorithms for mutual exclusion
  67.  */
  68.  
  69. Processor 1:
  70.     (0)        st    #1,[P1wants]
  71.             membar    #StoreStore    ! required in PSO and RMO only
  72.     (1)        st    #1,[Turn]
  73.             membar    #StoreLoad    ! required in all memory models
  74.  
  75.     (2)    retry:    ld    [Turn],%l0
  76.     (3)        cmp    #1,%l0
  77.             bne    ok
  78.     (4)        ld    [P2wants],%l0
  79.     (5)        cmp    #0,%l0
  80.             bne    retry
  81.  
  82.     (6)    ok:    ld    [A],%l1        ! *** Critical section:
  83.     (7)        add    %l1,#1,%l1    ! *** increments A
  84.     (8)        st    %l1,[A]        ! *** will fail with broken lock
  85.  
  86.             membar    #StoreStore    ! required in PSO and RMO only
  87.     (9)        st    #0,[P1wants]
  88.  
  89. Processor 2:
  90.     (10)        st    #1,[P2wants]
  91.             membar    #StoreStore
  92.     (11)        st    #0,[Turn]
  93.             membar    #StoreLoad
  94.  
  95.     (12)    retry:    ld    [Turn],%l0
  96.     (13)        cmp    #0,%l0
  97.             bne    ok
  98.     (14)        ld    [P1wants],%l0
  99.     (15)        cmp    #0,%l0
  100.             bne    retry
  101.  
  102.     (16)    ok:    ld    [A],%l1
  103.     (17)        add    %l1,#1,%l1
  104.     (18)        st    %l1,[A]
  105.  
  106.             membar    #StoreStore
  107.     (19)        st    #0,[P2wants]
  108.  
  109. Assertions:
  110. A1: A == 2
  111.  
  112. Possible values under all memory models:
  113. 1:l0  1:l1  2:l0  2:l1   P1wants  Turn  P2wants  A
  114.   0     1     0     2     0        0     0       2 
  115.   0     2     0     1     0        1     0       2   
  116.   0     2     1     1     0        1     0       2
  117.