ReentrantLock's hasQueuedThreads() can go back from false to true at any time. But if you implement your own reference counting, you can prevent another thread from incrementing the count once it got to 0, so if your thread got to 0 you know for certain that another thread won't try to lock on it.<br>
<br><br><div class="gmail_quote">On Wed, Oct 17, 2012 at 4:16 PM, Manik Surtani <span dir="ltr"><<a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">Actually, rather than a ref counter, I could rely on ReentrantLock's hasQueuedThreads(). But even this will not guard against edge cases - threads waiting, and then timing out, preventing the removal of unused locks and presenting a men leak.<div>
<br><div><div><div class="h5"><div>On 17 Oct 2012, at 07:47, Dan Berindei <<a href="mailto:dan.berindei@gmail.com" target="_blank">dan.berindei@gmail.com</a>> wrote:</div><br></div></div><blockquote type="cite"><div>
<div class="h5"><br><div class="gmail_quote">On Wed, Oct 17, 2012 at 3:13 AM, Jason Greene <span dir="ltr"><<a href="mailto:jgreene@redhat.com" target="_blank">jgreene@redhat.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
Sent from my iPhone<br>
<div><br>
On Oct 16, 2012, at 10:39 AM, Jason Greene <<a href="mailto:jason.greene@redhat.com" target="_blank">jason.greene@redhat.com</a>> wrote:<br>
<br>
>> from _get_ to lock on an eventually created new instance.<br>
><br>
> Yes if you get the ordering right, it can certainly be done. You might have to introduce a state-based CAS or secondary lock though for some scenarios (I haven't thought through them all) I think Manik's point though was just that getting it right is harder than just making the whole thing atomic.<br>
<br>
</div>Although there is no way out of the remove, you still have to recheck the lock is valid after every successful aquire, and then try to lock the new lock<br></blockquote></div><br><br>Right, you still need a retry loop on acquire, so why not use reference counting and actually reuse the lock?<br>
<br>I think all you need is a tryAddRef method, something like this:<br><br> public boolean tryAddRef() {<br> int newCount;<br> int oldCount;<br> do {<br> oldCount = refCount.get();<br> if (oldCount == 0)<br>
return false;<br> newCount = oldCount + 1;<br> } while (!refCount.compareAndSet(oldCount, newCount));<br> return true;<br> }<br><br>Then in your acquireLock you call tryAddRef in a loop and when it returns true you can go on and really acquire the lock - knowing that another thread can't remove it from the map.<br>
<br></div></div><div class="im">
_______________________________________________<br>infinispan-dev mailing list<br><a href="mailto:infinispan-dev@lists.jboss.org" target="_blank">infinispan-dev@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a></div>
</blockquote></div><br><div class="im"><div>
<span style="border-spacing:0px;text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:-webkit-auto;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<span style="border-spacing:0px;text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:-webkit-auto;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<span style="border-spacing:0px;text-indent:0px;letter-spacing:normal;font-variant:normal;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Helvetica;word-spacing:0px"><div style="word-wrap:break-word">
<div>--</div><div>Manik Surtani</div><div><a href="mailto:manik@jboss.org" target="_blank">manik@jboss.org</a></div><div><a href="http://twitter.com/maniksurtani" target="_blank">twitter.com/maniksurtani</a></div><div><br>
</div><div><div>Platform Architect, JBoss Data Grid</div><div><a href="http://red.ht/data-grid" target="_blank">http://red.ht/data-grid</a></div></div></div></span></div></span></div></span>
</div>
<br></div></div></div><br>_______________________________________________<br>
infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br></blockquote></div><br>