// TODO: either put ExternalMutex in JavaX // or make externalMutexMap local static IAutoCloseableF0 tempExternalMutexFor(O o) { var map = externalMutexMap(); synchronized(map) { ExternalMutex mutex = map.get(o); if (mutex == null) map.put(o, mutex = new ExternalMutex); var _mutex = mutex; mutex.incRefCount(); ret new IAutoCloseableF0 { public O get() { ret _mutex; } public void close { synchronized(map) { if (_mutex.decRefCount() == 0) map.remove(o); } } }; } }