|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.rdf4j.common.concurrent.locks; |
| 12 | + |
| 13 | +import java.lang.invoke.VarHandle; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.concurrent.atomic.LongAdder; |
| 16 | +import java.util.concurrent.locks.StampedLock; |
| 17 | + |
| 18 | +/** |
| 19 | + * A lightweight read/write lock manager that avoids allocating per-lock objects. Readers are tracked using two |
| 20 | + * {@link LongAdder LongAdders} while writers rely on a {@link StampedLock}. The read-lock method returns a constant |
| 21 | + * stamp ({@link #READ_LOCK_STAMP}) and writers receive the stamp produced by the underlying {@link StampedLock}. |
| 22 | + */ |
| 23 | +public class StampedLongAdderLockManager { |
| 24 | + |
| 25 | + /** |
| 26 | + * Stamp returned to callers holding a read lock. Passing any other value to {@link #unlockRead(long)} is considered |
| 27 | + * an illegal monitor state. |
| 28 | + */ |
| 29 | + public static final long READ_LOCK_STAMP = Long.MIN_VALUE; |
| 30 | + |
| 31 | + private final StampedLock stampedLock = new StampedLock(); |
| 32 | + private final LongAdder readersLocked = new LongAdder(); |
| 33 | + private final LongAdder readersUnlocked = new LongAdder(); |
| 34 | + |
| 35 | + // milliseconds to wait when trying to acquire the write lock interruptibly |
| 36 | + private final int tryWriteLockMillis; |
| 37 | + |
| 38 | + // Number of spin attempts before temporarily releasing the write lock when readers are active. |
| 39 | + private final int writePreference; |
| 40 | + |
| 41 | + public StampedLongAdderLockManager() { |
| 42 | + this(1, 100); |
| 43 | + } |
| 44 | + |
| 45 | + public StampedLongAdderLockManager(int writePreference, int tryWriteLockMillis) { |
| 46 | + this.writePreference = Math.max(1, writePreference); |
| 47 | + this.tryWriteLockMillis = Math.max(1, tryWriteLockMillis); |
| 48 | + } |
| 49 | + |
| 50 | + public boolean isWriterActive() { |
| 51 | + return stampedLock.isWriteLocked(); |
| 52 | + } |
| 53 | + |
| 54 | + public boolean isReaderActive() { |
| 55 | + return readersUnlocked.sum() != readersLocked.sum(); |
| 56 | + } |
| 57 | + |
| 58 | + public void waitForActiveWriter() throws InterruptedException { |
| 59 | + while (stampedLock.isWriteLocked() && !isReaderActive()) { |
| 60 | + spinWait(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void waitForActiveReaders() throws InterruptedException { |
| 65 | + while (isReaderActive()) { |
| 66 | + spinWait(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public long readLock() throws InterruptedException { |
| 71 | + readersLocked.increment(); |
| 72 | + while (stampedLock.isWriteLocked()) { |
| 73 | + try { |
| 74 | + spinWaitAtReadLock(); |
| 75 | + } catch (InterruptedException e) { |
| 76 | + readersUnlocked.increment(); |
| 77 | + throw e; |
| 78 | + } |
| 79 | + } |
| 80 | + return READ_LOCK_STAMP; |
| 81 | + } |
| 82 | + |
| 83 | + public long tryReadLock() { |
| 84 | + readersLocked.increment(); |
| 85 | + if (!stampedLock.isWriteLocked()) { |
| 86 | + return READ_LOCK_STAMP; |
| 87 | + } |
| 88 | + readersUnlocked.increment(); |
| 89 | + return 0L; |
| 90 | + } |
| 91 | + |
| 92 | + public void unlockRead(long stamp) { |
| 93 | + if (stamp != READ_LOCK_STAMP) { |
| 94 | + throw new IllegalMonitorStateException("Trying to release a stamp that is not a read lock"); |
| 95 | + } |
| 96 | + |
| 97 | + VarHandle.acquireFence(); |
| 98 | + readersUnlocked.increment(); |
| 99 | + } |
| 100 | + |
| 101 | + public long writeLock() throws InterruptedException { |
| 102 | + long writeStamp = writeLockInterruptibly(); |
| 103 | + boolean lockAcquired = false; |
| 104 | + |
| 105 | + try { |
| 106 | + int attempts = 0; |
| 107 | + do { |
| 108 | + if (Thread.interrupted()) { |
| 109 | + throw new InterruptedException(); |
| 110 | + } |
| 111 | + |
| 112 | + if (!hasActiveReaders()) { |
| 113 | + lockAcquired = true; |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + if (attempts++ > writePreference) { |
| 118 | + attempts = 0; |
| 119 | + |
| 120 | + stampedLock.unlockWrite(writeStamp); |
| 121 | + writeStamp = 0; |
| 122 | + |
| 123 | + yieldWait(); |
| 124 | + |
| 125 | + writeStamp = writeLockInterruptibly(); |
| 126 | + } else { |
| 127 | + spinWait(); |
| 128 | + } |
| 129 | + |
| 130 | + } while (!lockAcquired); |
| 131 | + } finally { |
| 132 | + if (!lockAcquired && writeStamp != 0) { |
| 133 | + stampedLock.unlockWrite(writeStamp); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + VarHandle.releaseFence(); |
| 138 | + return writeStamp; |
| 139 | + } |
| 140 | + |
| 141 | + public long tryWriteLock() { |
| 142 | + long writeStamp = stampedLock.tryWriteLock(); |
| 143 | + if (writeStamp == 0) { |
| 144 | + return 0L; |
| 145 | + } |
| 146 | + |
| 147 | + if (!hasActiveReaders()) { |
| 148 | + VarHandle.releaseFence(); |
| 149 | + return writeStamp; |
| 150 | + } |
| 151 | + |
| 152 | + stampedLock.unlockWrite(writeStamp); |
| 153 | + return 0L; |
| 154 | + } |
| 155 | + |
| 156 | + public void unlockWrite(long stamp) { |
| 157 | + if (stamp == 0) { |
| 158 | + throw new IllegalMonitorStateException("Trying to release a write lock that is not locked"); |
| 159 | + } |
| 160 | + stampedLock.unlockWrite(stamp); |
| 161 | + } |
| 162 | + |
| 163 | + private boolean hasActiveReaders() { |
| 164 | + return readersUnlocked.sum() != readersLocked.sum(); |
| 165 | + } |
| 166 | + |
| 167 | + private long writeLockInterruptibly() throws InterruptedException { |
| 168 | + long writeStamp; |
| 169 | + do { |
| 170 | + if (Thread.interrupted()) { |
| 171 | + throw new InterruptedException(); |
| 172 | + } |
| 173 | + writeStamp = stampedLock.tryWriteLock(tryWriteLockMillis, TimeUnit.MILLISECONDS); |
| 174 | + } while (writeStamp == 0); |
| 175 | + return writeStamp; |
| 176 | + } |
| 177 | + |
| 178 | + private void spinWait() throws InterruptedException { |
| 179 | + Thread.onSpinWait(); |
| 180 | + if (Thread.interrupted()) { |
| 181 | + throw new InterruptedException(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private void spinWaitAtReadLock() throws InterruptedException { |
| 186 | + Thread.onSpinWait(); |
| 187 | + if (Thread.interrupted()) { |
| 188 | + throw new InterruptedException(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private void yieldWait() throws InterruptedException { |
| 193 | + Thread.yield(); |
| 194 | + if (Thread.interrupted()) { |
| 195 | + throw new InterruptedException(); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments