├── .gitignore ├── README.md ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── test │ └── java │ │ └── com │ │ └── github │ │ └── benmanes │ │ └── lockfreequeue │ │ ├── CustomBenchmark.java │ │ ├── QueueBenchmark.java │ │ ├── MPSCQueue.java │ │ └── ConcurrentSingleConsumerQueueTest.java └── main │ └── java │ └── com │ └── github │ └── benmanes │ └── lockfreequeue │ └── ConcurrentSingleConsumerQueue.java ├── gradlew.bat └── gradlew /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .settings 3 | .project 4 | .gradle 5 | build 6 | bin 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | LockFreeQueue 2 | ============= 3 | 4 | Multi-producer Single-consumer Queue 5 | 6 | ./gradlew build 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ben-manes/lock-free-queue/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jul 28 19:46:59 PDT 2012 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.0-bin.zip 7 | -------------------------------------------------------------------------------- /src/test/java/com/github/benmanes/lockfreequeue/CustomBenchmark.java: -------------------------------------------------------------------------------- 1 | /* Copyright 2012 Ben Manes. All Rights Reserved. */ 2 | package com.github.benmanes.lockfreequeue; 3 | 4 | import java.util.Arrays; 5 | 6 | /** 7 | * This benchmark evaluates single-threaded performance. 8 | * 9 | * @author ben.manes@gmail.com (Ben Manes) 10 | */ 11 | public class CustomBenchmark { 12 | private static final int ITERATIONS = 100000000; 13 | 14 | private QueueBenchmark benchmark; 15 | 16 | void setUp() { 17 | benchmark = new QueueBenchmark(); 18 | benchmark.setUp(); 19 | } 20 | 21 | void runCSCQ_array() { 22 | final long start = System.nanoTime(); 23 | benchmark.timeConcurrentSingleConsumerQueue_array(ITERATIONS); 24 | final long time = System.nanoTime() - start; 25 | 26 | System.out.printf("%s: %,d ns/op\n", "ConcurrentSingleConsumerQueue_array", time / ITERATIONS); 27 | } 28 | 29 | void runCSCQ_linked() { 30 | final long start = System.nanoTime(); 31 | benchmark.timeConcurrentSingleConsumerQueue_linked(ITERATIONS); 32 | final long time = System.nanoTime() - start; 33 | 34 | System.out.printf("%s: %,d ns/op\n", "ConcurrentSingleConsumerQueue_linked", time / ITERATIONS); 35 | } 36 | 37 | void runCLQ() { 38 | final long start = System.nanoTime(); 39 | benchmark.timeConcurrentLinkedQueue(ITERATIONS); 40 | final long time = System.nanoTime() - start; 41 | 42 | System.out.printf("%s: %,d ns/op\n", "ConcurrentLinkedQueue", time / ITERATIONS); 43 | } 44 | 45 | void runMPSCQ() { 46 | final long start = System.nanoTime(); 47 | benchmark.timeMultiProducerSingleConsumer(ITERATIONS); 48 | final long time = System.nanoTime() - start; 49 | 50 | System.out.printf("%s: %,d ns/op\n", "MPSCQueue", time / ITERATIONS); 51 | } 52 | 53 | public static void main(String[] args) { 54 | boolean setUp = Arrays.asList(args).contains("setUp"); 55 | CustomBenchmark benchmark = new CustomBenchmark(); 56 | benchmark.setUp(); 57 | 58 | for (int i = 0; i < 3; i++) { 59 | System.out.printf("--- %d ---\n", i + 1); 60 | 61 | if (setUp) { 62 | benchmark.setUp(); 63 | } 64 | benchmark.runCSCQ_array(); 65 | 66 | if (setUp) { 67 | benchmark.setUp(); 68 | } 69 | benchmark.runCSCQ_linked(); 70 | 71 | if (setUp) { 72 | benchmark.setUp(); 73 | } 74 | benchmark.runCLQ(); 75 | 76 | if (setUp) { 77 | benchmark.setUp(); 78 | } 79 | benchmark.runMPSCQ(); 80 | } 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/test/java/com/github/benmanes/lockfreequeue/QueueBenchmark.java: -------------------------------------------------------------------------------- 1 | /* Copyright 2012 Ben Manes. All Rights Reserved. */ 2 | package com.github.benmanes.lockfreequeue; 3 | 4 | import java.util.Queue; 5 | import java.util.concurrent.ConcurrentLinkedQueue; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | import com.google.caliper.Runner; 9 | import com.google.caliper.SimpleBenchmark; 10 | 11 | /** 12 | * This benchmark evaluates single-threaded performance. 13 | * 14 | * @author ben.manes@gmail.com (Ben Manes) 15 | */ 16 | public class QueueBenchmark extends SimpleBenchmark { 17 | private ConcurrentSingleConsumerQueue linkedSingleConsumerQueue; 18 | private ConcurrentSingleConsumerQueue arraySingleConsumerQueue; 19 | private Queue concurrentLinkedQueue; 20 | private MPSCQueue mpscQueue; 21 | private AtomicInteger length; 22 | 23 | @Override 24 | protected void setUp() { 25 | arraySingleConsumerQueue = new ConcurrentSingleConsumerQueue(16); 26 | linkedSingleConsumerQueue = new ConcurrentSingleConsumerQueue(1); 27 | concurrentLinkedQueue = new ConcurrentLinkedQueue(); 28 | mpscQueue = new MPSCQueue(); 29 | length = new AtomicInteger(); 30 | 31 | for (int i = 0; i < linkedSingleConsumerQueue.estimatedCapacity(); i++) { 32 | linkedSingleConsumerQueue.add(i); 33 | } 34 | } 35 | 36 | public int timeConcurrentSingleConsumerQueue_array(final int reps) { 37 | int dummy = 0; 38 | while (dummy < reps) { 39 | arraySingleConsumerQueue.offer(dummy); 40 | arraySingleConsumerQueue.poll(); 41 | dummy++; 42 | } 43 | return dummy; 44 | } 45 | 46 | public int timeConcurrentSingleConsumerQueue_linked(final int reps) { 47 | int dummy = 0; 48 | while (dummy < reps) { 49 | linkedSingleConsumerQueue.offer(dummy); 50 | linkedSingleConsumerQueue.poll(); 51 | dummy++; 52 | } 53 | return dummy; 54 | } 55 | 56 | public int timeConcurrentLinkedQueue(final int reps) { 57 | int dummy = 0; 58 | while (dummy < reps) { 59 | concurrentLinkedQueue.offer(dummy); 60 | length.incrementAndGet(); 61 | 62 | concurrentLinkedQueue.poll(); 63 | length.decrementAndGet(); 64 | dummy++; 65 | } 66 | return dummy; 67 | } 68 | 69 | public int timeMultiProducerSingleConsumer(final int reps) { 70 | int dummy = 0; 71 | while (dummy < reps) { 72 | mpscQueue.offer(dummy); 73 | length.incrementAndGet(); 74 | 75 | mpscQueue.poll(); 76 | length.decrementAndGet(); 77 | dummy++; 78 | } 79 | return dummy; 80 | } 81 | 82 | public static void main(String[] args) { 83 | Runner.main(QueueBenchmark.class, args); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/com/github/benmanes/lockfreequeue/MPSCQueue.java: -------------------------------------------------------------------------------- 1 | /* Copyright 2012 Ben Manes. All Rights Reserved. */ 2 | package com.github.benmanes.lockfreequeue; 3 | 4 | import java.util.Collection; 5 | import java.util.Iterator; 6 | import java.util.Queue; 7 | import java.util.concurrent.atomic.AtomicReference; 8 | 9 | /** 10 | * A linked-list based concurrent queue supporting multiple producers and a single consumer. 11 | * 12 | * @author ben.manes@gmail.com (Ben Manes) 13 | */ 14 | public final class MPSCQueue implements Queue { 15 | Node tail = new Node(null); 16 | final AtomicReference> head = new AtomicReference>(tail); 17 | 18 | @Override 19 | public boolean offer(E e) { 20 | Node node = new Node(e); 21 | head.getAndSet(node).lazySet(node); 22 | return true; 23 | } 24 | 25 | @Override 26 | public E poll() { 27 | Node next = tail.get(); 28 | if (next == null) { 29 | return null; 30 | } else { 31 | tail = next; 32 | return next.value; 33 | } 34 | } 35 | 36 | static class Node extends AtomicReference> { 37 | private static final long serialVersionUID = 1L; 38 | 39 | final T value; 40 | 41 | Node(T value) { 42 | this.value = value; 43 | } 44 | } 45 | 46 | @Override 47 | public int size() { 48 | // TODO Auto-generated method stub 49 | return 0; 50 | } 51 | 52 | @Override 53 | public boolean isEmpty() { 54 | // TODO Auto-generated method stub 55 | return false; 56 | } 57 | 58 | @Override 59 | public boolean contains(Object o) { 60 | // TODO Auto-generated method stub 61 | return false; 62 | } 63 | 64 | @Override 65 | public Iterator iterator() { 66 | // TODO Auto-generated method stub 67 | return null; 68 | } 69 | 70 | @Override 71 | public Object[] toArray() { 72 | // TODO Auto-generated method stub 73 | return null; 74 | } 75 | 76 | @Override 77 | public T[] toArray(T[] a) { 78 | // TODO Auto-generated method stub 79 | return null; 80 | } 81 | 82 | @Override 83 | public boolean remove(Object o) { 84 | // TODO Auto-generated method stub 85 | return false; 86 | } 87 | 88 | @Override 89 | public boolean containsAll(Collection c) { 90 | // TODO Auto-generated method stub 91 | return false; 92 | } 93 | 94 | @Override 95 | public boolean addAll(Collection c) { 96 | // TODO Auto-generated method stub 97 | return false; 98 | } 99 | 100 | @Override 101 | public boolean removeAll(Collection c) { 102 | // TODO Auto-generated method stub 103 | return false; 104 | } 105 | 106 | @Override 107 | public boolean retainAll(Collection c) { 108 | // TODO Auto-generated method stub 109 | return false; 110 | } 111 | 112 | @Override 113 | public void clear() { 114 | while (poll() != null); 115 | } 116 | 117 | @Override 118 | public boolean add(E e) { 119 | // TODO Auto-generated method stub 120 | return false; 121 | } 122 | 123 | @Override 124 | public E remove() { 125 | // TODO Auto-generated method stub 126 | return null; 127 | } 128 | 129 | @Override 130 | public E element() { 131 | // TODO Auto-generated method stub 132 | return null; 133 | } 134 | 135 | @Override 136 | public E peek() { 137 | // TODO Auto-generated method stub 138 | return null; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query businessSystem maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /src/test/java/com/github/benmanes/lockfreequeue/ConcurrentSingleConsumerQueueTest.java: -------------------------------------------------------------------------------- 1 | /* Copyright 2012 Ben Manes. All Rights Reserved. */ 2 | package com.github.benmanes.lockfreequeue; 3 | 4 | import java.util.Iterator; 5 | import java.util.Queue; 6 | 7 | import com.google.common.collect.ImmutableList; 8 | import com.google.common.collect.Iterators; 9 | import org.testng.annotations.DataProvider; 10 | import org.testng.annotations.Test; 11 | 12 | import static org.hamcrest.MatcherAssert.assertThat; 13 | import static org.hamcrest.Matchers.empty; 14 | import static org.hamcrest.Matchers.equalTo; 15 | import static org.hamcrest.Matchers.hasItem; 16 | import static org.hamcrest.Matchers.is; 17 | import static org.hamcrest.Matchers.nullValue; 18 | 19 | /** 20 | * A unit-test for {@link java.util.Queue} interface. These tests do not assert 21 | * correct concurrency behavior. 22 | * 23 | * @author ben.manes@gmail.com (Ben Manes) 24 | */ 25 | public final class ConcurrentSingleConsumerQueueTest { 26 | private static final int WARMED_ARRAY_SIZE = 16; 27 | private static final int WARMED_LIST_SIZE = 32; 28 | 29 | @Test(dataProvider = "allQueues") 30 | public void clear(Queue queue) { 31 | queue.clear(); 32 | assertThat(queue, is(empty())); 33 | } 34 | 35 | @Test(dataProvider = "allQueues") 36 | public void estimatedCapacity(ConcurrentSingleConsumerQueue queue) { 37 | assertThat(queue.estimatedCapacity(), is(WARMED_ARRAY_SIZE)); 38 | } 39 | 40 | @Test 41 | public void estimatedCapacity_powerOfTwo() { 42 | ConcurrentSingleConsumerQueue queue = new ConcurrentSingleConsumerQueue(12); 43 | assertThat(queue.estimatedCapacity(), is(16)); 44 | } 45 | 46 | @Test(dataProvider = "emptyQueue") 47 | public void size_whenEmpty(Queue queue) { 48 | assertThat(queue.size(), is(0)); 49 | } 50 | 51 | @Test(dataProvider = "warmedArrayQueue") 52 | public void size_whenArrayPopulated(Queue queue) { 53 | assertThat(queue.size(), is(WARMED_ARRAY_SIZE)); 54 | } 55 | 56 | @Test(dataProvider = "warmedListQueue") 57 | public void size_whenListPopulated(Queue queue) { 58 | assertThat(queue.size(), is(WARMED_LIST_SIZE)); 59 | } 60 | 61 | @Test(dataProvider = "emptyQueue") 62 | public void isEmpty_whenEmpty(Queue queue) { 63 | assertThat(queue.isEmpty(), is(true)); 64 | } 65 | 66 | @Test(dataProvider = "allWarmedQueues") 67 | public void isEmpty_whenPopulated(Queue queue) { 68 | assertThat(queue.isEmpty(), is(false)); 69 | } 70 | 71 | @Test(dataProvider = "emptyQueue") 72 | public void contains_withNull(Queue queue) { 73 | assertThat(queue.contains(null), is(false)); 74 | } 75 | 76 | @Test(dataProvider = "allWarmedQueues") 77 | public void contains_whenFound(Queue queue) { 78 | assertThat(queue.contains(1), is(true)); 79 | } 80 | 81 | @Test(dataProvider = "allWarmedQueues") 82 | public void contains_whenNotFound(Queue queue) { 83 | assertThat(queue.contains(-1), is(false)); 84 | } 85 | 86 | @Test(dataProvider = "emptyQueue", expectedExceptions = NullPointerException.class) 87 | public void offer_withNull(Queue queue) { 88 | queue.offer(null); 89 | } 90 | 91 | @Test(dataProvider = "emptyQueue") 92 | public void offer_intoArray(ConcurrentSingleConsumerQueue queue) { 93 | for (int i = 0; i < queue.estimatedCapacity(); i++) { 94 | queue.offer(i); 95 | assertThat(queue, hasItem(i)); 96 | } 97 | } 98 | 99 | @Test(dataProvider = "emptyQueue") 100 | public void offer_intoLinkedList(ConcurrentSingleConsumerQueue queue) { 101 | for (int i = 0; i < (2 * queue.estimatedCapacity()); i++) { 102 | queue.offer(i); 103 | assertThat(queue, hasItem(i)); 104 | } 105 | } 106 | 107 | @Test(dataProvider = "emptyQueue") 108 | public void peek_whenEmpty(Queue queue) { 109 | assertThat(queue.peek(), is(nullValue())); 110 | } 111 | 112 | @Test(dataProvider = "allWarmedQueues") 113 | public void peek(Queue queue) { 114 | assertThat(queue.peek(), is(1)); 115 | } 116 | 117 | @Test(dataProvider = "emptyQueue") 118 | public void poll_whenEmpty(Queue queue) { 119 | assertThat(queue.poll(), is(nullValue())); 120 | } 121 | 122 | @Test(dataProvider = "allWarmedQueues") 123 | public void poll(Queue queue) { 124 | int originalSize = queue.size(); 125 | for (int i = 1; i <= originalSize; i++) { 126 | assertThat(queue.poll(), is(i)); 127 | assertThat(queue.size(), is(originalSize - i)); 128 | } 129 | assertThat(queue.size(), is(0)); 130 | } 131 | 132 | @Test(dataProvider = "emptyQueue") 133 | public void drainTo_whenEmpty(ConcurrentSingleConsumerQueue queue) { 134 | Integer[] out = new Integer[WARMED_ARRAY_SIZE]; 135 | assertThat(queue.drainTo(out), is(0)); 136 | assertThat(out, is(equalTo(new Integer[WARMED_ARRAY_SIZE]))); 137 | } 138 | 139 | @Test(dataProvider = "allQueues") 140 | public void drainTo_withZeroSizeArray(ConcurrentSingleConsumerQueue queue) { 141 | Integer[] out = new Integer[0]; 142 | assertThat(queue.drainTo(out), is(0)); 143 | assertThat(out, is(equalTo(new Integer[0]))); 144 | } 145 | 146 | /* ---------------- Queue providers -------------- */ 147 | 148 | @DataProvider(name = "allQueues") 149 | public Iterator providesAllQueues() { 150 | return Iterators.concat(providesEmptyQueue(), providesAllWarmedQueues()); 151 | } 152 | 153 | @DataProvider(name = "emptyQueue") 154 | public Iterator providesEmptyQueue() { 155 | return ImmutableList.of(new Object[] { emptyQueue() }).iterator(); 156 | } 157 | 158 | @DataProvider(name = "warmedArrayQueue") 159 | public Iterator providesWarmedArrayQueue() { 160 | return ImmutableList.of( 161 | new Object[] { warmedQueue(WARMED_ARRAY_SIZE) }).iterator(); 162 | } 163 | 164 | @DataProvider(name = "warmedListQueue") 165 | public Iterator providesWarmedListQueue() { 166 | return ImmutableList.of( 167 | new Object[] { warmedQueue(WARMED_LIST_SIZE) }).iterator(); 168 | } 169 | 170 | @DataProvider(name = "allWarmedQueues") 171 | public Iterator providesAllWarmedQueues() { 172 | return Iterators.concat(providesWarmedArrayQueue(), providesWarmedListQueue()); 173 | } 174 | 175 | private Queue emptyQueue() { 176 | return new ConcurrentSingleConsumerQueue(WARMED_ARRAY_SIZE); 177 | } 178 | 179 | private Queue warmedQueue(int count) { 180 | Queue queue = new ConcurrentSingleConsumerQueue(WARMED_ARRAY_SIZE); 181 | warmUp(queue, count); 182 | return queue; 183 | } 184 | 185 | private void warmUp(Queue queue, int count) { 186 | for (int i = 0; i < count; i++) { 187 | queue.add(i + 1); 188 | } 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /src/main/java/com/github/benmanes/lockfreequeue/ConcurrentSingleConsumerQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012 Ben Manes. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.benmanes.lockfreequeue; 17 | 18 | import java.util.AbstractQueue; 19 | import java.util.ConcurrentModificationException; 20 | import java.util.Iterator; 21 | import java.util.NoSuchElementException; 22 | import java.util.Queue; 23 | import java.util.concurrent.atomic.AtomicLong; 24 | import java.util.concurrent.atomic.AtomicReference; 25 | import java.util.concurrent.atomic.AtomicReferenceArray; 26 | 27 | /** 28 | * An unbounded thread-safe {@linkplain Queue queue} based on array and linked nodes. This queue 29 | * differs from {@link java.util.concurrent.ConcurrentLinkedQueue} in that it does not support 30 | * multiple consumer threads. 31 | *

32 | * This queue orders elements FIFO (first-in-first-out). The head of the queue is that 33 | * element that has been on the queue the longest time. The tail of the queue is that 34 | * element that has been on the queue the shortest time. New elements are inserted at the tail of 35 | * the queue, and the queue retrieval operations obtain elements at the head of the queue. 36 | *

37 | * A {@code ConcurrentSingleConsumerQueue} is an appropriate choice when many threads produce into 38 | * a common collection and, at a given instant, only a single thread consumes from the collection. 39 | * This queue does not permit {@code null} elements. 40 | * 41 | * @author ben.manes@gmail.com (Ben Manes) 42 | * @param the type of elements held in this collection 43 | */ 44 | public final class ConcurrentSingleConsumerQueue extends AbstractQueue { 45 | // TODO(bmanes): This implementation is experimental, as it contains fundamental problems with 46 | // maintaining FIFO order. In particular the transition in and out of the link node based queue 47 | // is questionable. 48 | 49 | static int ceilingNextPowerOfTwo(int x) { 50 | // From Hacker's Delight, Chapter 3, Harry S. Warren Jr. 51 | return 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(x - 1)); 52 | } 53 | 54 | final AtomicReferenceArray array; 55 | final AtomicLong head; 56 | final AtomicLong tail; 57 | final int mask; 58 | 59 | Node headNode; 60 | final AtomicReference> tailNode; 61 | 62 | public ConcurrentSingleConsumerQueue(int estimatedCapacity) { 63 | if (estimatedCapacity <= 0) { 64 | throw new IllegalArgumentException(); 65 | } 66 | array = new AtomicReferenceArray(ceilingNextPowerOfTwo(estimatedCapacity)); 67 | mask = array.length() - 1; 68 | head = new AtomicLong(); 69 | tail = new AtomicLong(); 70 | headNode = new Node(null); 71 | tailNode = new AtomicReference>(headNode); 72 | } 73 | 74 | @Override 75 | public boolean isEmpty() { 76 | return head.get() == tail.get(); 77 | } 78 | 79 | @Override 80 | public int size() { 81 | return (int) (tail.get() - head.get()); 82 | } 83 | 84 | public int estimatedCapacity() { 85 | return array.length(); 86 | } 87 | 88 | @Override 89 | public boolean offer(E e) { 90 | if (e == null) { 91 | throw new NullPointerException(); 92 | } 93 | long t = tail.getAndIncrement(); 94 | long h = head.get(); 95 | if ((t - h) < array.length()) { 96 | int index = (int) (t & mask); 97 | array.lazySet(index, e); 98 | } else { 99 | Node node = new Node(e); 100 | tailNode.getAndSet(node).lazySet(node); 101 | } 102 | return true; 103 | } 104 | 105 | @Override 106 | public E peek() { 107 | long h = head.get(); 108 | long t = tail.get(); 109 | if (h == t) { 110 | return null; 111 | } 112 | int index = (int) h & mask; 113 | E e = array.get(index); 114 | return (e == null) ? headNode.get().value : e; 115 | } 116 | 117 | @Override 118 | public E poll() { 119 | long h = head.get(); 120 | long t = tail.get(); 121 | if (h == t) { 122 | return null; 123 | } 124 | int index = (int) h & mask; 125 | E e = array.get(index); 126 | if (e == null) { 127 | Node next = headNode.get(); 128 | headNode = next; 129 | e = next.value; 130 | } else { 131 | array.lazySet(index, null); 132 | } 133 | head.lazySet(h + 1); 134 | return e; 135 | } 136 | 137 | /** 138 | * Removes at most the given number of available elements from this queue and adds them to the 139 | * given array. 140 | * 141 | * @param out the array to transfer elements into 142 | * @return the number of elements transferred 143 | */ 144 | public int drainTo(E[] out) { 145 | for (int i = 0; i < out.length; i++) { 146 | out[i] = poll(); // TODO(bmanes): optimize 147 | if (out[i] == null) { 148 | return i; 149 | } 150 | } 151 | return out.length; 152 | } 153 | 154 | @Override 155 | public Iterator iterator() { 156 | return new Iterator() { 157 | long cursor = head.get(); 158 | Node cursorNode = headNode; 159 | long expectedModCount = cursor; 160 | 161 | @Override 162 | public boolean hasNext() { 163 | return cursor != tail.get(); 164 | } 165 | 166 | @Override 167 | public E next() { 168 | if (!hasNext()) { 169 | throw new NoSuchElementException(); 170 | } else if (head.get() != expectedModCount) { 171 | throw new ConcurrentModificationException(); 172 | } 173 | E e; 174 | if ((cursor - expectedModCount) < array.length()) { 175 | int index = (int) cursor & mask; 176 | e = array.get(index); 177 | } else { 178 | Node next = cursorNode.get(); 179 | cursorNode = next; 180 | e = next.value; 181 | } 182 | cursor++; 183 | return e; 184 | } 185 | 186 | @Override 187 | public void remove() { 188 | throw new UnsupportedOperationException(); 189 | } 190 | }; 191 | } 192 | 193 | static final class Node extends AtomicReference> { 194 | private static final long serialVersionUID = 1L; 195 | 196 | final T value; 197 | 198 | Node(T value) { 199 | this.value = value; 200 | } 201 | } 202 | } 203 | --------------------------------------------------------------------------------