private bool AddClientHoldingLock(ForsetiClient client) { while (true) { for (int i = 0; i < _clientsHoldingThisLock.Length; i++) { AtomicReferenceArray <ForsetiClient> holders = _clientsHoldingThisLock[i]; if (holders == null) { holders = AddHolderArray(i); } for (int j = 0; j < holders.length(); j++) { ForsetiClient c = holders.get(j); if (c == null) { // TODO This means we do CAS on each entry, very likely hitting a lot of failures until we // TODO find a slot. We should look into better strategies here. // TODO One such strategy could be binary searching for a free slot, and then linear scan // TODO after that if the CAS fails on the slot we found with binary search. if (holders.compareAndSet(j, null, client)) { return(true); } } } } } }
public FxOptionMarketData scenario(int scenarioIndex) { FxOptionMarketData current = cache.get(scenarioIndex); if (current != null) { return(current); } return(cache.updateAndGet(scenarioIndex, v => v != null ? v : lookup.marketDataView(marketData.scenario(scenarioIndex)))); }
public LegalEntityDiscountingMarketData scenario(int scenarioIndex) { LegalEntityDiscountingMarketData current = cache.get(scenarioIndex); if (current != null) { return(current); } return(cache.updateAndGet(scenarioIndex, v => v != null ? v : lookup.marketDataView(marketData.scenario(scenarioIndex)))); }
private ThreadStart Updater <T1, T2>(AtomicReferenceArray <T1> lastBatches, System.Threading.CountdownEvent insertersDone, ReadWriteLock updateLock, ICollection <T2> updates) where T1 : Org.Neo4j.Kernel.Api.Index.IndexEntryUpdate <T1> { return(throwing(() => { // Entity ids that have been removed, so that additions can reuse them IList <long> removed = new List <long>(); RandomValues randomValues = RandomValues.create(new Random(Random.seed() + THREADS)); while (insertersDone.CurrentCount > 0) { // Do updates now and then Thread.Sleep(10); updateLock.writeLock().@lock(); try { using (IndexUpdater updater = _populator.newPopulatingUpdater(_nodePropertyAccessor)) { for (int i = 0; i < THREADS; i++) { IList <IndexEntryUpdate <object> > batch = lastBatches.get(i); if (batch != null) { IndexEntryUpdate <object> update = null; switch (randomValues.Next(3)) { case 0: // add if (!removed.Empty) { long?id = removed.remove(randomValues.Next(removed.size())); update = add(id, Descriptor, ValueGenerator.apply(randomValues)); } break; case 1: // remove IndexEntryUpdate <object> removal = batch.get(randomValues.Next(batch.size())); update = remove(removal.EntityId, Descriptor, removal.values()); removed.add(removal.EntityId); break; case 2: // change removal = batch.get(randomValues.Next(batch.size())); change(removal.EntityId, Descriptor, removal.values(), toArray(ValueGenerator.apply(randomValues))); break; default: throw new System.ArgumentException(); } if (update != null) { updater.process(update); updates.Add(update); } } } } } finally { updateLock.writeLock().unlock(); } } })); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldBeAbleToSnapshotDuringHeavyLoad() throws Throwable //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldBeAbleToSnapshotDuringHeavyLoad() { // GIVEN //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final KernelTransactions transactions = newKernelTransactions(); KernelTransactions transactions = NewKernelTransactions(); Race race = new Race(); const int threads = 50; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.concurrent.atomic.AtomicBoolean end = new java.util.concurrent.atomic.AtomicBoolean(); AtomicBoolean end = new AtomicBoolean(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.concurrent.atomic.AtomicReferenceArray<KernelTransactionsSnapshot> snapshots = new java.util.concurrent.atomic.AtomicReferenceArray<>(threads); AtomicReferenceArray <KernelTransactionsSnapshot> snapshots = new AtomicReferenceArray <KernelTransactionsSnapshot>(threads); // Representing "transaction" threads for (int i = 0; i < threads; i++) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int threadIndex = i; int threadIndex = i; race.AddContestant(() => { ThreadLocalRandom random = ThreadLocalRandom.current(); while (!end.get()) { try { using (KernelTransaction transaction = GetKernelTransaction(transactions)) { KernelTransactionsSnapshot snapshot = null; try { parkNanos(MILLISECONDS.toNanos(random.Next(3))); if (snapshots.get(threadIndex) == null) { requireNonNull(transactions, "transactions is null"); snapshot = requireNonNull(transactions.Get(), "transactions.get() returned null"); snapshots.set(threadIndex, snapshot); parkNanos(MILLISECONDS.toNanos(random.Next(3))); } } catch (Exception e) { StringBuilder sb = (new StringBuilder("Gotcha!\n")).Append("threadIndex=").Append(threadIndex).Append('\n').Append("transaction=").Append(transaction).Append('\n').Append("snapshots=").Append(snapshots).Append('\n').Append("snapshot=").Append(snapshot).Append('\n').Append("end=").Append(end); throw new Exception(sb.ToString(), e); } } } catch (TransactionFailureException e) { throw new Exception(e); } } }); } // Just checks snapshots race.AddContestant(() => { ThreadLocalRandom random = ThreadLocalRandom.current(); int snapshotsLeft = 1_000; while (snapshotsLeft > 0) { int threadIndex = random.Next(threads); KernelTransactionsSnapshot snapshot = snapshots.get(threadIndex); if (snapshot != null && snapshot.AllClosed()) { snapshotsLeft--; snapshots.set(threadIndex, null); } } // End condition of this test can be described as: // when 1000 snapshots have been seen as closed. // setting this boolean to true will have all other threads end as well so that race.go() will end end.set(true); }); // WHEN race.Go(); }