norswap

Lamport Clock in Oz

Feb 8th, 2012
1,046
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. declare
  2. proc {Loop S Clock UpdatePort P1 P2}
  3.    NewClock Response
  4. in
  5.    case S of H|T then
  6.       if H.clock > Clock then NewClock = H.clock + 1
  7.       else NewClock = Clock + 1 end
  8.       {Send UpdatePort NewClock#H}
  9.       Response = msg(clock:NewClock value:H.value+1)
  10.       {Send P1 Response} {Send P2 Response}
  11.       {Loop T NewClock UpdatePort P1 P2}
  12.    end
  13. end
  14. fun {ClockFilter L Clock DisplayPort}
  15.    case L of H|T then
  16.       if Clock > H.clock then
  17.          {Send DisplayPort H.value}
  18.          {ClockFilter T Clock DisplayPort}
  19.       else  H|{ClockFilter T Clock DisplayPort} end
  20.    [] nil then nil
  21.    end
  22. end
  23. proc {Display Updates Stack DisplayPort}
  24.    case Updates of (Clock#Msg)|T then
  25.       {Display T {ClockFilter Msg|Stack Clock DisplayPort} DisplayPort}
  26.    end
  27. end
  28. proc {DumbDisplay Updates Stack DisplayPort}
  29.    for _#Msg in Updates do {Send DisplayPort Msg.value} end
  30. end
  31. proc {Process Name S1 S2 P1 P2}
  32.    Msgs MsgPort = {NewPort Msgs}
  33.    Updates UpdatePort = {NewPort Updates}
  34.    Displayed DisplayPort = {NewPort Displayed}
  35. in
  36.    thread for X in S1 do {Send MsgPort X} end end
  37.    thread for X in S2 do {Send MsgPort X} end end
  38.    thread {Loop Msgs 0 UpdatePort P1 P2} end
  39.    % Replacing Display by DumbDisplay breaks the garantee that causal order
  40.    % will be respected.
  41.    thread {Display Updates nil DisplayPort} end
  42.    {Browse Name(Displayed)}
  43. end
  44. fun {DelayStream S}
  45.    thread
  46.       case S of H|T then {Delay {OS.rand} mod 2000 + 500} H|{DelayStream T} end
  47.    end
  48. end
  49. proc {MakeDelayPort Port Stream} Tmp in
  50.    Port = {NewPort Tmp} Stream = {DelayStream Tmp}
  51. end
  52. S12 P12 {MakeDelayPort P12 S12}
  53. S13 P13 {MakeDelayPort P13 S13}
  54. S21 P21 {MakeDelayPort P21 S21}
  55. S23 P23 {MakeDelayPort P23 S23}
  56. S31 P31 {MakeDelayPort P31 S31}
  57. S32 P32 {MakeDelayPort P32 S32}
  58. C12 = {Connection.offer S12}
  59. C13 = {Connection.offer S13}
  60. C21 = {Connection.offer S21}
  61. C23 = {Connection.offer S23}
  62. C31 = {Connection.offer S31}
  63. C32 = {Connection.offer S32}
  64. {Process n1 S21 S31 P12 P13}
  65. {Process n2 S12 S32 P21 P23}
  66. {Process n3 S13 S23 P31 P32}
  67. {Send P21 msg(clock:0 value:0)}
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×