# HG changeset patch # User Jaroslav Tulach # Date 1462940569 -7200 # Node ID 9329156bb5d422d7879eebfe773f6c8838af6e64 # Parent 9c7413573b986667c050fba0ebf55cc22ecd6d8f #259132: Ready for multi-threaded access to observables diff -r 9c7413573b98 -r 9329156bb5d4 json/src/main/java/org/netbeans/html/json/spi/Observers.java --- a/json/src/main/java/org/netbeans/html/json/spi/Observers.java Wed May 11 06:21:52 2016 +0200 +++ b/json/src/main/java/org/netbeans/html/json/spi/Observers.java Wed May 11 06:22:49 2016 +0200 @@ -73,7 +73,9 @@ synchronized (GLOBAL) { for (Watcher w : GLOBAL) { if (w.proto == p) { - throw new IllegalStateException("Re-entrant attempt to access " + p); + if (w.owner == Thread.currentThread()) { + throw new IllegalStateException("Re-entrant attempt to access " + p); + } } } } @@ -91,13 +93,21 @@ static void finishComputing(Proto p) { synchronized (GLOBAL) { - Watcher w = GLOBAL.pop(); - if (w.proto != p) { - throw new IllegalStateException("Inconsistency: " + w.proto + " != " + p); + boolean found = false; + Iterator it = GLOBAL.iterator(); + while (it.hasNext()) { + Watcher w = it.next(); + if (w.proto == p && w.owner == Thread.currentThread()) { + if (w.prop != null) { + Observers mine = p.observers(true); + mine.add(w); + } + found = true; + it.remove(); + } } - if (w.prop != null) { - Observers mine = p.observers(true); - mine.add(w); + if (!found) { + throw new IllegalStateException("Cannot find " + p + " in " + GLOBAL); } } } @@ -205,10 +215,12 @@ } private static final class Watcher { + final Thread owner; final Proto proto; final String prop; Watcher(Proto proto, String prop) { + this.owner = Thread.currentThread(); this.proto = proto; this.prop = prop; }