In-order triggering of watches on mutable state

2013-07-31 Thread Michael Drogalis
Problem: I have a ref representing a queue of people in line. I add a watch to the ref to print out the contents of the queue whenever it changes. Naturally, and expected, the following can happen if queuing happens in rapid succession: Queue: [] console: "Queue is Mike, John" console: "Queu

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Timothy Baldridge
Code? It's hard to debug if we can't see what's going on. On Wed, Jul 31, 2013 at 9:00 AM, Michael Drogalis wrote: > Problem: > > I have a ref representing a queue of people in line. > I add a watch to the ref to print out the contents of the queue whenever > it changes. > Naturally, and expecte

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Aaron Cohen
A watcher fn has 4 parameters: key, reference, old-state, new-state If you use old-state and new-state rather than the reference, you should not see your problem. --Aaron On Wed, Jul 31, 2013 at 11:00 AM, Michael Drogalis wrote: > Problem: > > I have a ref representing a queue of people in lin

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Michael Drogalis
Aaron: Yep, I'm aware - and am using the value provided by the last parameter. This is going to be tough to show the problem without bringing more details of my concurrency set up. I'm not sure if this will exhibit the problem, but this is what it boils down to: https://gist.github.com/Michael

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Michael Drogalis
I can precisely exemplify the behavior here: https://gist.github.com/MichaelDrogalis/6123177 On Wednesday, July 31, 2013 11:13:17 AM UTC-4, Michael Drogalis wrote: > > Aaron: Yep, I'm aware - and am using the value provided by the last > parameter. > > This is going to be tough to show the probl

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Timothy Baldridge
The answer is here: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LockingTransaction.java#L361 Agents and watches are dispatched outside of the transaction locking. This means that multiple transactions could be executing watches in parallel, and hence execute out of order.

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Mike Drogalis
Thanks for the link. :) I understand that the behavior I'm seeing is correct. Any idea how to achieve the desired behavior, though? On Wed, Jul 31, 2013 at 12:06 PM, Timothy Baldridge wrote: > The answer is here: > > > https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LockingTr

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Timothy Baldridge
You might want to consider switching to agents (or something else) I don't think it's possible to do what you want with refs. Timothy Baldridge On Wed, Jul 31, 2013 at 10:08 AM, Mike Drogalis wrote: > Thanks for the link. :) I understand that the behavior I'm seeing is > correct. Any idea how t

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Mike Drogalis
I'll play around with agents for this when I get some more free time. I find it odd that, as the sole perceiver of an indentity, events are capable of being perceived out of order. If I were watching a line queue up in person, events are obviously "dispatched" in order through my visual perception

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Timothy Baldridge
Deadlocks? Imagine a watch that ended up running a transaction against the ref it was triggered from. Sounds insane, but it could be fairly easy to do in a situation like this: ref -> calls watchers -> calls fn1 -> calls fn 2 -> calls fn3 -> starts transaction on ref Since watchers are notified o

Re: In-order triggering of watches on mutable state

2013-07-31 Thread Mike Drogalis
Good reasoning; that makes a lot of sense -- even if intuitively it doesn't follow through. I'll do some more thinking about my concurrency needs and come back with a follow up question later. Thanks man! On Wed, Jul 31, 2013 at 12:51 PM, Timothy Baldridge wrote: > Deadlocks? Imagine a watch th

Re: In-order triggering of watches on mutable state

2013-08-01 Thread Cedric Greevey
Try using an agent send from inside a transaction. Such sends are only dispatched if the transaction commits, and successive sends to the same agent from a single thread are run by the agent in the same order the thread sent them. On Wed, Jul 31, 2013 at 1:12 PM, Mike Drogalis wrote: > Good rea

Re: In-order triggering of watches on mutable state

2013-08-01 Thread Mike Drogalis
Cedric: Will agents give me the guarantee that if I have something in Agent A, and I want to move it to agent B, and I do this inside a transaction: - perceivers will see it in A at read point - perceivers will see it in B at write point - perceivers will never see it in neither nor both? If that'

Re: In-order triggering of watches on mutable state

2013-08-01 Thread Timothy Baldridge
"Try using an agent send from inside a transaction. Such sends are only dispatched if the transaction commits, and successive sends to the same agent from a single thread are run by the agent in the same order the thread sent them." That's not the problem. The problem is that agent sends (just lik

Re: In-order triggering of watches on mutable state

2013-08-01 Thread Mike Drogalis
Unfortunately, my problem is not one where the operation is communitive. To be concrete, I am dealing with multiple threads plucking elements off the top of many different queues, and putting them into a new queue. So adding element A, then B results in line [A, B], whereas adding element B, then A

Re: In-order triggering of watches on mutable state

2013-08-01 Thread Timothy Baldridge
Ding! You said the magic words! For queues, can I recommend one of the following? http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html According to Rich "why doesn't Clojure provide queues? The Java ones are just fine! Use them" Also, if you have a large number of que