@Test @Ignore("in the sequence causes SIGSEGV in java machine in sqlite module") public void lock() throws Exception { for( int z=0; z<10; z++ ) { HashId id = HashId.createRandom(); count = 0; ItemLock il = new ItemLock(); il.synchronize(id, (__) -> count++); il.synchronize(id, (__) -> count++); il.synchronize(id, (__) -> count++); assertEquals(3, count); assertEquals(1, il.size()); id = null; for (int i = 0; i < 10; i++) { System.gc(); System.runFinalization(); if (il.size() == 0) break; Thread.sleep(100); } assertEquals(0, il.size()); } }
itemLock.synchronize(hashId, lock -> { StateRecord newRecord = ledger.findOrCreate(hashId); newRecord.setState(committingState)
/** * Execute a callable acquiring a unique lock (mutex) for a given {@link HashId}. Locks is released upon callable * return. * * @param id ot get a lock to * @param callable lamda to execute exclusively for the id * @param <T> is type * * @return whatever the callable returns * * @throws Exception whatever callable throws */ public <T> T synchronize(HashId id, Function<Object, T> callable) throws Exception { ItemLock lock = null; // short exclusive mutex: obtaning a lock synchronized (monitors) { lock = monitors.get(id); if (lock == null) { lock = new ItemLock(); monitors.put(id, lock); } } // now we only lock the item: synchronized (lock) { return (T) callable.apply(lock); } }
r.setState(ItemState.PENDING); try { itemLock.synchronize(r.getId(), lock -> { r.save(); synchronized (cache) {
Map<HashId,StateRecord> finished = new HashMap<>(); Map<HashId,StateRecord> failed = new HashMap<>(); ItemLock itemLock = new ItemLock(); Map<HashId,StateRecord> nullContarcts = new HashMap<>(); System.out.println(">> " + recordsToSanitate.size());
itemLock.synchronize(record.getId(), lock -> { record.save(); synchronized (cache) { itemLock.synchronize(record.getId(), lock -> { record.destroy(); return null;
itemLock.synchronize(revokingItem.getId(), lock -> { StateRecord r = record.lockToRevoke(revokingItem.getId()); if (r == null) {
setState(ItemState.UNDEFINED); try { itemLock.synchronize(record.getId(), lock -> { record.destroy();
if (r.getLockedByRecordId() == record.getRecordId()) { try { itemLock.synchronize(r.getId(), lock -> { if (record.getState() == ItemState.APPROVED) {
for (StateRecord r : lockedToRevoke) { try { itemLock.synchronize(r.getId(), lock -> { r.unlock().save(); synchronized (cache) { itemLock.synchronize(r.getId(), lock -> { r.unlock().save(); synchronized (cache) {
itemLock.synchronize(newItem.getId(), lock -> { StateRecord r = record.createOutputLockRecord(newItem.getId()); if (r == null) {
return itemLock.synchronize(itemId, (lock) -> { ItemProcessor ip = processors.get(itemId); if (ip != null) {
itemLock.synchronize(revokingItem.getId(), lock -> { StateRecord r = ledger.findOrCreate(revokingItem.getId()); r.setState(ItemState.REVOKED); itemLock.synchronize(newItem.getId(), lock -> { StateRecord r = ledger.findOrCreate(newItem.getId()); r.setState(ItemState.APPROVED);