forked from LMAX-Exchange/disruptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/main/java/com/lmax/disruptor/LiteTimeoutBlockingWaitStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.lmax.disruptor; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* Variation of the {@link TimeoutBlockingWaitStrategy} that attempts to elide conditional wake-ups | ||
* when the lock is uncontended. | ||
*/ | ||
public class LiteTimeoutBlockingWaitStrategy implements WaitStrategy | ||
{ | ||
private final Lock lock = new ReentrantLock(); | ||
private final Condition processorNotifyCondition = lock.newCondition(); | ||
private final AtomicBoolean signalNeeded = new AtomicBoolean(false); | ||
private final long timeoutInNanos; | ||
|
||
public LiteTimeoutBlockingWaitStrategy(final long timeout, final TimeUnit units) | ||
{ | ||
timeoutInNanos = units.toNanos(timeout); | ||
} | ||
|
||
@Override | ||
public long waitFor( | ||
final long sequence, | ||
final Sequence cursorSequence, | ||
final Sequence dependentSequence, | ||
final SequenceBarrier barrier) | ||
throws AlertException, InterruptedException, TimeoutException | ||
{ | ||
long nanos = timeoutInNanos; | ||
|
||
long availableSequence; | ||
if (cursorSequence.get() < sequence) | ||
{ | ||
lock.lock(); | ||
try | ||
{ | ||
while (cursorSequence.get() < sequence) | ||
{ | ||
signalNeeded.getAndSet(true); | ||
|
||
barrier.checkAlert(); | ||
nanos = processorNotifyCondition.awaitNanos(nanos); | ||
if (nanos <= 0) | ||
{ | ||
throw TimeoutException.INSTANCE; | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
while ((availableSequence = dependentSequence.get()) < sequence) | ||
{ | ||
barrier.checkAlert(); | ||
} | ||
|
||
return availableSequence; | ||
} | ||
|
||
@Override | ||
public void signalAllWhenBlocking() | ||
{ | ||
if (signalNeeded.getAndSet(false)) | ||
{ | ||
lock.lock(); | ||
try | ||
{ | ||
processorNotifyCondition.signalAll(); | ||
} | ||
finally | ||
{ | ||
lock.unlock(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "LiteTimeoutBlockingWaitStrategy{" + | ||
"processorNotifyCondition=" + processorNotifyCondition + | ||
'}'; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/com/lmax/disruptor/LiteTimeoutBlockingWaitStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.lmax.disruptor; | ||
|
||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.junit.Assert.*; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.jmock.Expectations; | ||
import org.jmock.Mockery; | ||
import org.jmock.integration.junit4.JMock; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(JMock.class) | ||
public class LiteTimeoutBlockingWaitStrategyTest | ||
{ | ||
private final Mockery mockery = new Mockery(); | ||
|
||
@Test | ||
public void shouldTimeoutWaitFor() throws Exception | ||
{ | ||
final SequenceBarrier sequenceBarrier = mockery.mock(SequenceBarrier.class); | ||
|
||
long theTimeout = 500; | ||
LiteTimeoutBlockingWaitStrategy waitStrategy = new LiteTimeoutBlockingWaitStrategy(theTimeout, TimeUnit.MILLISECONDS); | ||
Sequence cursor = new Sequence(5); | ||
Sequence dependent = cursor; | ||
|
||
mockery.checking( | ||
new Expectations() | ||
{ | ||
{ | ||
allowing(sequenceBarrier).checkAlert(); | ||
} | ||
}); | ||
|
||
long t0 = System.currentTimeMillis(); | ||
|
||
try | ||
{ | ||
waitStrategy.waitFor(6, cursor, dependent, sequenceBarrier); | ||
fail("TimeoutException should have been thrown"); | ||
} | ||
catch (TimeoutException e) | ||
{ | ||
} | ||
|
||
long t1 = System.currentTimeMillis(); | ||
|
||
long timeWaiting = t1 - t0; | ||
|
||
assertThat(timeWaiting, greaterThanOrEqualTo(theTimeout)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters