These are chat archives for jdubray/sam

4th
Aug 2017
Jean-Jacques Dubray
@jdubray
Aug 04 2017 16:14
Looks like this is all it would take to synchronize the model:
 let ticket = []

let entering = []

function present(int actionId, proposal) {// action ID request processing a proposal 
      entering[actionId] = true
      int max = 0;
      for (int a = 0 ; a < ACTION_ID_MAX; a++)
      {
          max = Math.max(max, a)
      }
      ticket[actionId] = 1 + max 
      entering[actionId] =  false
      for (int i = 0; i < ticket.length; ++i)
      {
          if (i != actionId)
          {
             while (entering[i]) { setTimeout( 0, null) } // wait while other thread picks a ticket
             while (ticket[i] != 0 && ( ticket[actionId] > ticket[i]  ||
                      (ticket[actionId] == ticket[i] && (actionId > i))))
                              { setTimeout(0,null)}
          }
      }
      // processing of the proposal go here
}

public void unlock(int actionId) {
    ticket[actionId] = 0
}
Fred Daoud
@foxdonut
Aug 04 2017 17:16

I don't get it. What is the purpose of this?

      int max = 0;
      for (int a = 0 ; a < ACTION_ID_MAX; a++)
      {
          max = Math.max(max, a)
      }
      ticket[actionId] = 1 + max

Isn't that just a bunch code of that ultimately only does this?

ticket[actionId] = ACTION_ID_MAX;

?

Also what is the point of setting entering[actionId] to true and then to false? JS is not multithreaded in the sense that the code running in that function will not get interrupted mid-way in favour of another thread entering the function.
Jean-Jacques Dubray
@jdubray
Aug 04 2017 17:58
concurrency is part of JS
devin ivy
@devinivy
Aug 04 2017 18:00
fred is right, though! those lines of code are guaranteed to run without any other code running
Jean-Jacques Dubray
@jdubray
Aug 04 2017 18:00
sorry, this line should be: max = Math.max(max, tickets[a])
devin ivy
@devinivy
Aug 04 2017 18:00
ah, i see
Jean-Jacques Dubray
@jdubray
Aug 04 2017 18:28
@devinivy setTimeout?
devin ivy
@devinivy
Aug 04 2017 18:34
that wont interrupt synchronous code, though
it's be scheduled to occur when a block of synchronous code ends
that's why setTimeout() timing isn't accurate, e.g. if your process is compute-intensive
if you do a setTimeout() for 1ms then while-loop for 10ms, your setTimeout() will fire after the loop
Jean-Jacques Dubray
@jdubray
Aug 04 2017 19:50

that wont interrupt synchronous code, though

yes I know :smile: ! As you know I like to process updates to the back-end, in the model (synchronously with the processing of the corresponding proposal). I am trying to avoid starting processing a new action's proposal. The whole point of setTimeout here is pass control (say to processing an update response). At that point the model continues synchronously until it unlocks the step and the bakery algorithm starts the processing of the next proposal.

I think vuex has a similar mechanism in place.
devin ivy
@devinivy
Aug 04 2017 19:53
so that above is the bakery algorithm?
in the context of SAM
Jean-Jacques Dubray
@jdubray
Aug 04 2017 19:57
This code is just the bakery algorithm, all you need to add is a proposal queue and wrap the model with it.
I just wanted to illustrate how "simple" it is to achieve a synchronized processing leveraging the full concurrency of JavaScript. It may be time to create a true SAM library!
And put an end to "effects" as data.
Jean-Jacques Dubray
@jdubray
Aug 04 2017 20:02
"simple" assuming you can rely on the brain power of Dr. Lamport. In the video he explains that one of the senior researcher working with him doubted it would work, but could not find a flaw. He was apparently a big Petri Net fan, but could not reason about this algorithm with Petri Nets. That's probably when Dr. Lamport parted with Petri Nets.
I would not be surprised if the US and Europe branched out on Petri Nets around the same time. With events like that and a small community of developers/researchers it would not be too hard to cast a shadow on the concept and never look back. Europe didn't have a strong computer industry and focused more on Automata where Petri Nets were a better fit.
Fascinating!
devin ivy
@devinivy
Aug 04 2017 20:09
:D
i watched some of that lamport interview :thumbsup:
Jean-Jacques Dubray
@jdubray
Aug 04 2017 21:06
much better than GoT!
Marcus Feitoza
@mfeitoza
Aug 04 2017 23:19

I just wanted to illustrate how "simple" it is to achieve a synchronized processing leveraging the full concurrency of JavaScript. It may be time to create a true SAM library!

:thumbsup: