{"action":"created","comment":{"url":"https://api.github.com/repos/hazelcast/hazelcast/pulls/comments/35857643","id":35857643,"diff_hunk":"@@ -0,0 +1,284 @@\n+/*\n+ * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package com.hazelcast.util;\n+\n+import java.util.AbstractQueue;\n+import java.util.Collection;\n+import java.util.Iterator;\n+import java.util.concurrent.BlockingQueue;\n+import java.util.concurrent.TimeUnit;\n+import java.util.concurrent.atomic.AtomicReference;\n+\n+import static java.util.concurrent.locks.LockSupport.park;\n+import static java.util.concurrent.locks.LockSupport.unpark;\n+\n+/**\n+ * Single consumer, multi producer variable length queue implementation.\n+ * <p/>\n+ * The fast queue is a blocking implementation.\n+ *\n+ * @param <E>\n+ */\n+public final class MPSCQueue<E> extends AbstractQueue<E> implements BlockingQueue<E> {\n+    private static final Node BLOCKED = new Node();\n+    private static final int INITIAL_ARRAY_SIZE = 512;\n+\n+    private final Thread owningThread;\n+    private final AtomicReference<Node> head = new AtomicReference<Node>();\n+    private final boolean spin;\n+    private Object[] array;\n+    private int index = -1;\n+\n+    public MPSCQueue(Thread owningThread) {\n+        this(owningThread, false);\n+    }\n+\n+    public MPSCQueue(Thread owningThread, boolean spin) {\n+        if (owningThread == null) {\n+            throw new IllegalArgumentException(\"owningThread can't be null\");\n+        }\n+        this.owningThread = owningThread;\n+        this.array = new Object[INITIAL_ARRAY_SIZE];\n+        this.spin = spin;\n+    }\n+\n+    @Override\n+    public void clear() {\n+        head.set(null);\n+    }\n+\n+    @Override\n+    public boolean offer(E value) {\n+        if (value == null) {\n+            throw new IllegalArgumentException(\"value can't be null\");\n+        }\n+\n+        AtomicReference<Node> head = this.head;\n+        Node newHead = new Node();\n+        newHead.value = value;\n+\n+        for (; ; ) {\n+            Node oldHead = head.get();\n+            if (oldHead == null || oldHead == BLOCKED) {\n+                newHead.next = null;\n+                newHead.size = 1;\n+            } else {\n+                newHead.next = oldHead;\n+                newHead.size = oldHead.size + 1;\n+            }\n+\n+            if (!head.compareAndSet(oldHead, newHead)) {\n+                continue;\n+            }\n+\n+            if (oldHead == BLOCKED) {\n+                unpark(owningThread);\n+            }\n+\n+            return true;\n+        }\n+    }\n+\n+    @Override\n+    public E take() throws InterruptedException {\n+        if (spin) {\n+            for (; ; ) {\n+                E item = poll();\n+                if (item != null) {\n+                    return item;\n+                }\n+            }\n+        }\n+\n+        E item = next();\n+        if (item != null) {\n+            return item;\n+        }\n+\n+        takeAll();\n+        assert index == 0;\n+        assert array[index] != null;\n+\n+        return next();\n+    }\n+\n+    @Override\n+    public E poll() {\n+        E item = next();\n+\n+        if (item != null) {\n+            return item;\n+        }\n+\n+        if (!pollAll()) {\n+            return null;\n+        }\n+\n+        return next();\n+    }\n+\n+\n+    private E next() {\n+        if (index == -1) {\n+            return null;\n+        }\n+\n+        if (index == array.length) {\n+            index = -1;\n+            return null;\n+        }\n+\n+        E item = (E) array[index];\n+        if (item == null) {\n+            index = -1;\n+            return null;\n+        }\n+        array[index] = null;\n+        index++;\n+        return item;\n+    }\n+\n+    public void takeAll() throws InterruptedException {\n+        AtomicReference<Node> head = this.head;\n+        for (; ; ) {\n+            Node currentHead = head.get();\n+\n+            if (currentHead == null) {\n+\n+                // there is nothing to be take, so lets block.\n+                if (!head.compareAndSet(null, BLOCKED)) {\n+                    continue;\n+                }\n+\n+                park();\n+            } else if (currentHead == BLOCKED) {\n+                park();\n+            } else {\n+                if (!head.compareAndSet(currentHead, null)) {\n+                    continue;\n+                }\n+\n+                initArray(currentHead);\n+                break;\n+            }\n+        }\n+\n+        if (owningThread.isInterrupted()) {\n+            head.compareAndSet(BLOCKED, null);","path":"hazelcast/src/main/java/com/hazelcast/util/MPSCQueue.java","position":181,"original_position":181,"commit_id":"b2c2d36df146df1430239f17f1dc4caaab51c65b","original_commit_id":"b2c2d36df146df1430239f17f1dc4caaab51c65b","user":{"login":"jerrinot","id":158619,"avatar_url":"https://avatars.githubusercontent.com/u/158619?v=3","gravatar_id":"","url":"https://api.github.com/users/jerrinot","html_url":"https://github.com/jerrinot","followers_url":"https://api.github.com/users/jerrinot/followers","following_url":"https://api.github.com/users/jerrinot/following{/other_user}","gists_url":"https://api.github.com/users/jerrinot/gists{/gist_id}","starred_url":"https://api.github.com/users/jerrinot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerrinot/subscriptions","organizations_url":"https://api.github.com/users/jerrinot/orgs","repos_url":"https://api.github.com/users/jerrinot/repos","events_url":"https://api.github.com/users/jerrinot/events{/privacy}","received_events_url":"https://api.github.com/users/jerrinot/received_events","type":"User","site_admin":false},"body":"how this can ever succeed? if the head is `BLOCKED` then you will never exit the main loop, won't you?","created_at":"2015-07-30T10:28:04Z","updated_at":"2015-07-30T10:28:04Z","html_url":"https://github.com/hazelcast/hazelcast/pull/5820#discussion_r35857643","pull_request_url":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820","_links":{"self":{"href":"https://api.github.com/repos/hazelcast/hazelcast/pulls/comments/35857643"},"html":{"href":"https://github.com/hazelcast/hazelcast/pull/5820#discussion_r35857643"},"pull_request":{"href":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820"}}},"pull_request":{"url":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820","id":41206787,"html_url":"https://github.com/hazelcast/hazelcast/pull/5820","diff_url":"https://github.com/hazelcast/hazelcast/pull/5820.diff","patch_url":"https://github.com/hazelcast/hazelcast/pull/5820.patch","issue_url":"https://api.github.com/repos/hazelcast/hazelcast/issues/5820","number":5820,"state":"open","locked":false,"title":"AsyncResponseHandler FastQueue + spinning (experimental setting)","user":{"login":"pveentjer","id":105243,"avatar_url":"https://avatars.githubusercontent.com/u/105243?v=3","gravatar_id":"","url":"https://api.github.com/users/pveentjer","html_url":"https://github.com/pveentjer","followers_url":"https://api.github.com/users/pveentjer/followers","following_url":"https://api.github.com/users/pveentjer/following{/other_user}","gists_url":"https://api.github.com/users/pveentjer/gists{/gist_id}","starred_url":"https://api.github.com/users/pveentjer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pveentjer/subscriptions","organizations_url":"https://api.github.com/users/pveentjer/orgs","repos_url":"https://api.github.com/users/pveentjer/repos","events_url":"https://api.github.com/users/pveentjer/events{/privacy}","received_events_url":"https://api.github.com/users/pveentjer/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-07-30T07:54:01Z","updated_at":"2015-07-30T10:28:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"13d7602954d8a9961d495e0e20b519c1175ea86e","assignee":null,"milestone":{"url":"https://api.github.com/repos/hazelcast/hazelcast/milestones/61","html_url":"https://github.com/hazelcast/hazelcast/milestones/3.6","labels_url":"https://api.github.com/repos/hazelcast/hazelcast/milestones/61/labels","id":982584,"number":61,"title":"3.6","description":"","creator":{"login":"enesakar","id":234086,"avatar_url":"https://avatars.githubusercontent.com/u/234086?v=3","gravatar_id":"","url":"https://api.github.com/users/enesakar","html_url":"https://github.com/enesakar","followers_url":"https://api.github.com/users/enesakar/followers","following_url":"https://api.github.com/users/enesakar/following{/other_user}","gists_url":"https://api.github.com/users/enesakar/gists{/gist_id}","starred_url":"https://api.github.com/users/enesakar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/enesakar/subscriptions","organizations_url":"https://api.github.com/users/enesakar/orgs","repos_url":"https://api.github.com/users/enesakar/repos","events_url":"https://api.github.com/users/enesakar/events{/privacy}","received_events_url":"https://api.github.com/users/enesakar/received_events","type":"User","site_admin":false},"open_issues":134,"closed_issues":153,"state":"open","created_at":"2015-02-17T09:27:09Z","updated_at":"2015-07-30T07:54:26Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820/commits","review_comments_url":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820/comments","review_comment_url":"https://api.github.com/repos/hazelcast/hazelcast/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hazelcast/hazelcast/issues/5820/comments","statuses_url":"https://api.github.com/repos/hazelcast/hazelcast/statuses/b2c2d36df146df1430239f17f1dc4caaab51c65b","head":{"label":"pveentjer:performance/3.x/fastqueue-part2","ref":"performance/3.x/fastqueue-part2","sha":"b2c2d36df146df1430239f17f1dc4caaab51c65b","user":{"login":"pveentjer","id":105243,"avatar_url":"https://avatars.githubusercontent.com/u/105243?v=3","gravatar_id":"","url":"https://api.github.com/users/pveentjer","html_url":"https://github.com/pveentjer","followers_url":"https://api.github.com/users/pveentjer/followers","following_url":"https://api.github.com/users/pveentjer/following{/other_user}","gists_url":"https://api.github.com/users/pveentjer/gists{/gist_id}","starred_url":"https://api.github.com/users/pveentjer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pveentjer/subscriptions","organizations_url":"https://api.github.com/users/pveentjer/orgs","repos_url":"https://api.github.com/users/pveentjer/repos","events_url":"https://api.github.com/users/pveentjer/events{/privacy}","received_events_url":"https://api.github.com/users/pveentjer/received_events","type":"User","site_admin":false},"repo":{"id":10990111,"name":"hazelcast","full_name":"pveentjer/hazelcast","owner":{"login":"pveentjer","id":105243,"avatar_url":"https://avatars.githubusercontent.com/u/105243?v=3","gravatar_id":"","url":"https://api.github.com/users/pveentjer","html_url":"https://github.com/pveentjer","followers_url":"https://api.github.com/users/pveentjer/followers","following_url":"https://api.github.com/users/pveentjer/following{/other_user}","gists_url":"https://api.github.com/users/pveentjer/gists{/gist_id}","starred_url":"https://api.github.com/users/pveentjer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pveentjer/subscriptions","organizations_url":"https://api.github.com/users/pveentjer/orgs","repos_url":"https://api.github.com/users/pveentjer/repos","events_url":"https://api.github.com/users/pveentjer/events{/privacy}","received_events_url":"https://api.github.com/users/pveentjer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/pveentjer/hazelcast","description":"Open Source In-Memory Data Grid","fork":true,"url":"https://api.github.com/repos/pveentjer/hazelcast","forks_url":"https://api.github.com/repos/pveentjer/hazelcast/forks","keys_url":"https://api.github.com/repos/pveentjer/hazelcast/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pveentjer/hazelcast/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pveentjer/hazelcast/teams","hooks_url":"https://api.github.com/repos/pveentjer/hazelcast/hooks","issue_events_url":"https://api.github.com/repos/pveentjer/hazelcast/issues/events{/number}","events_url":"https://api.github.com/repos/pveentjer/hazelcast/events","assignees_url":"https://api.github.com/repos/pveentjer/hazelcast/assignees{/user}","branches_url":"https://api.github.com/repos/pveentjer/hazelcast/branches{/branch}","tags_url":"https://api.github.com/repos/pveentjer/hazelcast/tags","blobs_url":"https://api.github.com/repos/pveentjer/hazelcast/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pveentjer/hazelcast/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pveentjer/hazelcast/git/refs{/sha}","trees_url":"https://api.github.com/repos/pveentjer/hazelcast/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pveentjer/hazelcast/statuses/{sha}","languages_url":"https://api.github.com/repos/pveentjer/hazelcast/languages","stargazers_url":"https://api.github.com/repos/pveentjer/hazelcast/stargazers","contributors_url":"https://api.github.com/repos/pveentjer/hazelcast/contributors","subscribers_url":"https://api.github.com/repos/pveentjer/hazelcast/subscribers","subscription_url":"https://api.github.com/repos/pveentjer/hazelcast/subscription","commits_url":"https://api.github.com/repos/pveentjer/hazelcast/commits{/sha}","git_commits_url":"https://api.github.com/repos/pveentjer/hazelcast/git/commits{/sha}","comments_url":"https://api.github.com/repos/pveentjer/hazelcast/comments{/number}","issue_comment_url":"https://api.github.com/repos/pveentjer/hazelcast/issues/comments{/number}","contents_url":"https://api.github.com/repos/pveentjer/hazelcast/contents/{+path}","compare_url":"https://api.github.com/repos/pveentjer/hazelcast/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pveentjer/hazelcast/merges","archive_url":"https://api.github.com/repos/pveentjer/hazelcast/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pveentjer/hazelcast/downloads","issues_url":"https://api.github.com/repos/pveentjer/hazelcast/issues{/number}","pulls_url":"https://api.github.com/repos/pveentjer/hazelcast/pulls{/number}","milestones_url":"https://api.github.com/repos/pveentjer/hazelcast/milestones{/number}","notifications_url":"https://api.github.com/repos/pveentjer/hazelcast/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pveentjer/hazelcast/labels{/name}","releases_url":"https://api.github.com/repos/pveentjer/hazelcast/releases{/id}","created_at":"2013-06-27T07:15:08Z","updated_at":"2015-04-22T04:00:47Z","pushed_at":"2015-07-30T10:04:59Z","git_url":"git://github.com/pveentjer/hazelcast.git","ssh_url":"git@github.com:pveentjer/hazelcast.git","clone_url":"https://github.com/pveentjer/hazelcast.git","svn_url":"https://github.com/pveentjer/hazelcast","homepage":"www.hazelcast.com","size":77440,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"hazelcast:master","ref":"master","sha":"ccc4cf5c5afe3eadfdb39332f605f2157820ee89","user":{"login":"hazelcast","id":1453152,"avatar_url":"https://avatars.githubusercontent.com/u/1453152?v=3","gravatar_id":"","url":"https://api.github.com/users/hazelcast","html_url":"https://github.com/hazelcast","followers_url":"https://api.github.com/users/hazelcast/followers","following_url":"https://api.github.com/users/hazelcast/following{/other_user}","gists_url":"https://api.github.com/users/hazelcast/gists{/gist_id}","starred_url":"https://api.github.com/users/hazelcast/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hazelcast/subscriptions","organizations_url":"https://api.github.com/users/hazelcast/orgs","repos_url":"https://api.github.com/users/hazelcast/repos","events_url":"https://api.github.com/users/hazelcast/events{/privacy}","received_events_url":"https://api.github.com/users/hazelcast/received_events","type":"Organization","site_admin":false},"repo":{"id":3786237,"name":"hazelcast","full_name":"hazelcast/hazelcast","owner":{"login":"hazelcast","id":1453152,"avatar_url":"https://avatars.githubusercontent.com/u/1453152?v=3","gravatar_id":"","url":"https://api.github.com/users/hazelcast","html_url":"https://github.com/hazelcast","followers_url":"https://api.github.com/users/hazelcast/followers","following_url":"https://api.github.com/users/hazelcast/following{/other_user}","gists_url":"https://api.github.com/users/hazelcast/gists{/gist_id}","starred_url":"https://api.github.com/users/hazelcast/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hazelcast/subscriptions","organizations_url":"https://api.github.com/users/hazelcast/orgs","repos_url":"https://api.github.com/users/hazelcast/repos","events_url":"https://api.github.com/users/hazelcast/events{/privacy}","received_events_url":"https://api.github.com/users/hazelcast/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/hazelcast/hazelcast","description":"Open Source In-Memory Data Grid","fork":false,"url":"https://api.github.com/repos/hazelcast/hazelcast","forks_url":"https://api.github.com/repos/hazelcast/hazelcast/forks","keys_url":"https://api.github.com/repos/hazelcast/hazelcast/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hazelcast/hazelcast/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hazelcast/hazelcast/teams","hooks_url":"https://api.github.com/repos/hazelcast/hazelcast/hooks","issue_events_url":"https://api.github.com/repos/hazelcast/hazelcast/issues/events{/number}","events_url":"https://api.github.com/repos/hazelcast/hazelcast/events","assignees_url":"https://api.github.com/repos/hazelcast/hazelcast/assignees{/user}","branches_url":"https://api.github.com/repos/hazelcast/hazelcast/branches{/branch}","tags_url":"https://api.github.com/repos/hazelcast/hazelcast/tags","blobs_url":"https://api.github.com/repos/hazelcast/hazelcast/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hazelcast/hazelcast/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hazelcast/hazelcast/git/refs{/sha}","trees_url":"https://api.github.com/repos/hazelcast/hazelcast/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hazelcast/hazelcast/statuses/{sha}","languages_url":"https://api.github.com/repos/hazelcast/hazelcast/languages","stargazers_url":"https://api.github.com/repos/hazelcast/hazelcast/stargazers","contributors_url":"https://api.github.com/repos/hazelcast/hazelcast/contributors","subscribers_url":"https://api.github.com/repos/hazelcast/hazelcast/subscribers","subscription_url":"https://api.github.com/repos/hazelcast/hazelcast/subscription","commits_url":"https://api.github.com/repos/hazelcast/hazelcast/commits{/sha}","git_commits_url":"https://api.github.com/repos/hazelcast/hazelcast/git/commits{/sha}","comments_url":"https://api.github.com/repos/hazelcast/hazelcast/comments{/number}","issue_comment_url":"https://api.github.com/repos/hazelcast/hazelcast/issues/comments{/number}","contents_url":"https://api.github.com/repos/hazelcast/hazelcast/contents/{+path}","compare_url":"https://api.github.com/repos/hazelcast/hazelcast/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hazelcast/hazelcast/merges","archive_url":"https://api.github.com/repos/hazelcast/hazelcast/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hazelcast/hazelcast/downloads","issues_url":"https://api.github.com/repos/hazelcast/hazelcast/issues{/number}","pulls_url":"https://api.github.com/repos/hazelcast/hazelcast/pulls{/number}","milestones_url":"https://api.github.com/repos/hazelcast/hazelcast/milestones{/number}","notifications_url":"https://api.github.com/repos/hazelcast/hazelcast/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hazelcast/hazelcast/labels{/name}","releases_url":"https://api.github.com/repos/hazelcast/hazelcast/releases{/id}","created_at":"2012-03-21T12:25:18Z","updated_at":"2015-07-30T02:13:28Z","pushed_at":"2015-07-30T10:05:00Z","git_url":"git://github.com/hazelcast/hazelcast.git","ssh_url":"git@github.com:hazelcast/hazelcast.git","clone_url":"https://github.com/hazelcast/hazelcast.git","svn_url":"https://github.com/hazelcast/hazelcast","homepage":"www.hazelcast.com","size":144931,"stargazers_count":1198,"watchers_count":1198,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":573,"mirror_url":null,"open_issues_count":477,"forks":573,"open_issues":477,"watchers":1198,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820"},"html":{"href":"https://github.com/hazelcast/hazelcast/pull/5820"},"issue":{"href":"https://api.github.com/repos/hazelcast/hazelcast/issues/5820"},"comments":{"href":"https://api.github.com/repos/hazelcast/hazelcast/issues/5820/comments"},"review_comments":{"href":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820/comments"},"review_comment":{"href":"https://api.github.com/repos/hazelcast/hazelcast/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hazelcast/hazelcast/pulls/5820/commits"},"statuses":{"href":"https://api.github.com/repos/hazelcast/hazelcast/statuses/b2c2d36df146df1430239f17f1dc4caaab51c65b"}}}}