MPSC lock free queue

Source: Internet
Author: User

[C-implemented queue] (http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue)

The following is an MPSC queue implemented by akka.

PS: the criterion for determining the end of a chain head in the Code is that the position of the added element is the end of the chain, which is in conflict with the name in the code.

PPS: single customer does not need to consider the consumer's competitive status at the same time

/*** Copyright (c) 2009-2014 typesafe Inc. */package akka. dispatch; import akka. util. unsafe; import Java. util. concurrent. atomic. atomicreference;/*** lock-free MPSC linked queue implementation based on Dmitriy vyukov's non-intrusive MPSC queue: * http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue */@ suppresswarnings ("serial") Public Abstract class abstractnodequeue <t> extends atomicreference <abstractnodequeue. node <t> {// extends atomicreference for the "head" slot (which is the one that is appended) since unsafe does not expose xchg operation intrinsically // The value of atomicreference corresponds to the tail node of the queue @ suppresswarnings ("UNUSED") Private volatile node <t> _ blank; protected abstractnodequeue () {final node <t> N = ne W node <t> (); _ taildonotcallmedirectly = N; // initialize the root node, value = NULL, chainhead! Set (n); // initialize node at the end of the chain }/**!!! There is a copy of this Code in pollnode ()!!! * // @ Suppresswarnings ("unchecked") protected final node <t> peeknode () {// chain header (;;) {final node <t> tail = (node <t>) unsafe. instance. getobjectvolatile (this, tailoffset); final node <t> next = tail. next (); If (next! = NULL | get () = tail) // next! = NULL indicates that it is not the root node and is in the linked list; get () = tail indicates that there is no node in the entire linked list, and only return next ;}} public final t PEEK () {final node <t> N = peeknode (); Return (n! = NULL )? N. value: NULL;} public final void add (final T value) {final node <t> N = new node <t> (value); getandset (n ). setnext (n); // a classic sentence. Set the value of atomicreference to the latest node, link n to the end of the linked list.} public final void addnode (final node <t> N) {n. setnext (null); getandset (n ). setnext (n);} public final Boolean isempty () {return PEEK () = NULL;} public final int count () {// count is an inaccurate int COUNT = 0; For (node <t> N = peekno De (); n! = NULL; n = n. Next () + + count; return count ;}/**!!! There is a copy of this Code in pollnode ()!!! */Public final t poll () {final node <t> next = peeknode (); If (next = NULL) return NULL; else {final t ret = next. value; next. value = NULL; unsafe. instance. putorderedobject (this, tailoffset, next); // Replace the chain header with return ret; }}@ suppresswarnings ("unchecked") Public final node <t> pollnode () {node <t> tail; node <t> next; For (;) {tail = (node <t>) unsafe. instance. getobjectvolatile (this, tailoffset); n EXT = tail. Next (); If (next! = NULL | get () = tail) break;} If (next = NULL) return NULL; else {tail. value = next. value; next. value = NULL; unsafe. instance. putorderedobject (this, tailoffset, next); Return tail ;}} private final static long tailoffset; static {try {tailoffset = unsafe. instance. objectfieldoffset (abstractnodequeue. class. getdeclaredfield ("_ taildonotcallmedirectly");} catch (throwable t) {Throw new ti Onininitializererror (t) ;}} public static class node <t> {public T value; @ suppresswarnings ("UNUSED") Private volatile node <t> _ nextdonotcallmedirectly; // The next node. Next, directly use unsafe to operate public node () {This (null);} public node (final T value) {This. value = value ;}@ suppresswarnings ("unchecked") Public final node <t> next () {return (node <t>) unsafe. instance. getobjectvolatile (this, nextoffset);} protected final void setnext (final node <t> newnext) {unsafe. instance. putorderedobject (this, nextoffset, newnext);} private final static long nextoffset; static {try {nextoffset = unsafe. instance. objectfieldoffset (node. class. getdeclaredfield ("_ nextdonotcallmedirectly");} catch (throwable t) {Throw new exceptionininitializererror (t );}}}}


MPSC lock free queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.