[
  {
    "path": ".gitmodules",
    "content": "[submodule \"Ext/libnstd\"]\n\tpath = Ext/libnstd\n\turl = https://github.com/craflin/libnstd.git\n[submodule \"Ext/mare\"]\n\tpath = Ext/mare\n\turl = https://github.com/craflin/mare.git\n"
  },
  {
    "path": "LICENSE",
    "content": "\n                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright [yyyy] [name of copyright owner]\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"
  },
  {
    "path": "LockFreeLifoQueue.h",
    "content": "\n#pragma once\n\n#include <nstd/Atomic.h>\n#include <nstd/Memory.h>\n\ntemplate <typename T> class LockFreeLifoQueue\n{\npublic:\n  explicit LockFreeLifoQueue(usize capacity)\n    : _capacity(capacity)\n  {\n    _indexMask = capacity;\n    for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _indexMask |= _indexMask >> i;\n    _abaOffset = _indexMask + 1;\n\n    _queue = (Node*)Memory::alloc(sizeof(Node) * (capacity + 1));\n    for(usize i = 1; i < capacity;)\n    {\n      Node& node = _queue[i];\n      node.abaNextFree = ++i;\n    }\n    _queue[capacity].abaNextFree = 0;\n\n    _abaFree = 1;\n    _abaPushed = 0;\n  }\n\n  ~LockFreeLifoQueue()\n  {\n    for(usize abaPushed = _abaPushed;;)\n    {\n      usize nodeIndex = abaPushed & _indexMask;\n      if(!nodeIndex)\n        break;\n      Node& node = _queue[nodeIndex];\n      abaPushed = node.abaNextPushed;\n      (&node.data)->~T();\n    }\n\n    Memory::free(_queue);\n  }\n  \n  usize capacity() const {return _capacity;}\n\n  usize size() const {return 0;}\n\n  bool push(const T& data)\n  {\n    Node* node;\n    usize abaFree;\n    for(;;)\n    {\n      abaFree = _abaFree;\n      usize nodeIndex = abaFree & _indexMask;\n      if(!nodeIndex)\n        return false;\n      node = &_queue[nodeIndex];\n      if(Atomic::compareAndSwap(_abaFree, abaFree, node->abaNextFree + _abaOffset) == abaFree)\n        break;\n    }\n\n    new (&node->data)T(data);\n    for(;;)\n    {\n      usize abaPushed = _abaPushed;\n      node->abaNextPushed = abaPushed;\n      if(Atomic::compareAndSwap(_abaPushed, abaPushed, abaFree) == abaPushed)\n        return true;\n    }\n  }\n\n  bool pop(T& result)\n  {\n    Node* node;\n    usize abaPushed;\n    for(;;)\n    {\n      abaPushed = _abaPushed;\n      usize nodeIndex = abaPushed & _indexMask;\n      if(!nodeIndex)\n        return false;\n      node = &_queue[nodeIndex];\n      if(Atomic::compareAndSwap(_abaPushed, abaPushed, node->abaNextPushed + _abaOffset) == abaPushed)\n        break;\n    }\n\n    result = node->data;\n    (&node->data)->~T();\n    abaPushed += _abaOffset;\n    for(;;)\n    {\n      usize abaFree = _abaFree;\n      node->abaNextFree = abaFree;\n      if(Atomic::compareAndSwap(_abaFree, abaFree, abaPushed) == abaFree)\n        return true;\n    }\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n    volatile usize abaNextFree;\n    volatile usize abaNextPushed;\n  };\n\nprivate:\n  usize _indexMask;\n  Node* _queue;\n  usize _abaOffset;\n  usize _capacity;\n  char cacheLinePad1[64];\n  volatile usize _abaFree;\n  char cacheLinePad2[64];\n  volatile usize _abaPushed;\n  char cacheLinePad3[64];\n};\n"
  },
  {
    "path": "LockFreeQueue.h",
    "content": "\n#pragma once\n\n#include <nstd/Atomic.h>\n#include <nstd/Memory.h>\n\ntemplate <typename T> class LockFreeQueue\n{\npublic:\n  explicit LockFreeQueue(usize capacity)\n  {\n    _capacityMask = capacity - 1;\n    for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _capacityMask |= _capacityMask >> i;\n    _capacity = _capacityMask + 1;\n\n    _queue = (Node*)Memory::alloc(sizeof(Node) * _capacity);\n    for(usize i = 0; i < _capacity; ++i)\n    {\n      _queue[i].tail = i;\n      _queue[i].head = -1;\n    }\n\n    _tail = 0;\n    _head = 0;\n  }\n\n  ~LockFreeQueue()\n  {\n    for(usize i = _head; i != _tail; ++i)\n      (&_queue[i & _capacityMask].data)->~T();\n\n    Memory::free(_queue);\n  }\n  \n  usize capacity() const {return _capacity;}\n  \n  usize size() const\n  {\n    usize head = Atomic::load(_head);\n    return _tail - head;\n  }\n  \n  bool push(const T& data)\n  {\n    Node* node;\n    usize next, tail = _tail;\n    for(;; tail = next)\n    {\n      node = &_queue[tail & _capacityMask];\n      if(Atomic::load(node->tail) != tail)\n        return false;\n      if((next = Atomic::compareAndSwap(_tail, tail, tail + 1)) == tail)\n        break;\n    }\n    new (&node->data)T(data);\n    Atomic::store(node->head, tail);\n    return true;\n  }\n\n  bool pop(T& result)\n  {\n    Node* node;\n    usize next, head = _head;\n    for(;; head = next)\n    {\n      node = &_queue[head & _capacityMask];\n      if(Atomic::load(node->head) != head)\n        return false;\n      if((next = Atomic::compareAndSwap(_head, head, head + 1)) == head)\n        break;\n    }\n    result = node->data;\n    (&node->data)->~T();\n    Atomic::store(node->tail, head + _capacity);\n    return true;\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n    usize tail;\n    usize head;\n  };\n\nprivate:\n  usize _capacityMask;\n  Node* _queue;\n  usize _capacity;\n  char cacheLinePad1[64];\n  usize _tail;\n  char cacheLinePad2[64];\n  usize _head;\n  char cacheLinePad3[64];\n};\n"
  },
  {
    "path": "LockFreeQueueCpp11.h",
    "content": "\n#pragma once\n\n#include <atomic>\n#include <cstddef>\n\ntemplate <typename T> class LockFreeQueueCpp11\n{\npublic:\n  explicit LockFreeQueueCpp11(size_t capacity)\n  {\n    _capacityMask = capacity - 1;\n    for(size_t i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _capacityMask |= _capacityMask >> i;\n    _capacity = _capacityMask + 1;\n\n    _queue = (Node*)new char[sizeof(Node) * _capacity];\n    for(size_t i = 0; i < _capacity; ++i)\n    {\n      _queue[i].tail.store(i, std::memory_order_relaxed);\n      _queue[i].head.store(-1, std::memory_order_relaxed);\n    }\n\n    _tail.store(0, std::memory_order_relaxed);\n    _head.store(0, std::memory_order_relaxed);\n  }\n\n  ~LockFreeQueueCpp11()\n  {\n    for(size_t i = _head; i != _tail; ++i)\n      (&_queue[i & _capacityMask].data)->~T();\n\n    delete [] (char*)_queue;\n  }\n  \n  size_t capacity() const {return _capacity;}\n  \n  size_t size() const\n  {\n    size_t head = _head.load(std::memory_order_acquire);\n    return _tail.load(std::memory_order_relaxed) - head;\n  }\n  \n  bool push(const T& data)\n  {\n    Node* node;\n    size_t tail = _tail.load(std::memory_order_relaxed);\n    for(;;)\n    {\n      node = &_queue[tail & _capacityMask];\n      if(node->tail.load(std::memory_order_relaxed) != tail)\n        return false;\n      if((_tail.compare_exchange_weak(tail, tail + 1, std::memory_order_relaxed)))\n        break;\n    }\n    new (&node->data)T(data);\n    node->head.store(tail, std::memory_order_release);\n    return true;\n  }\n\n  bool pop(T& result)\n  {\n    Node* node;\n    size_t head = _head.load(std::memory_order_relaxed);\n    for(;;)\n    {\n      node = &_queue[head & _capacityMask];\n      if(node->head.load(std::memory_order_relaxed) != head)\n        return false;\n      if(_head.compare_exchange_weak(head, head + 1, std::memory_order_relaxed))\n        break;\n    }\n    result = node->data;\n    (&node->data)->~T();\n    node->tail.store(head + _capacity, std::memory_order_release);\n    return true;\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n    std::atomic<size_t> tail;\n    std::atomic<size_t> head;\n  };\n\nprivate:\n  size_t _capacityMask;\n  Node* _queue;\n  size_t _capacity;\n  char cacheLinePad1[64];\n  std::atomic<size_t> _tail;\n  char cacheLinePad2[64];\n  std::atomic<size_t> _head;\n  char cacheLinePad3[64];\n};\n"
  },
  {
    "path": "LockFreeQueueSlow1.h",
    "content": "\n#pragma once\n\n#include <nstd/Atomic.h>\n#include <nstd/Memory.h>\n\ntemplate <typename T> class LockFreeQueueSlow1\n{\npublic:\n  explicit LockFreeQueueSlow1(usize capacity)\n  {\n    _capacityMask = capacity - 1;\n    for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _capacityMask |= _capacityMask >> i;\n    _capacity = _capacityMask + 1;\n\n    _queue = (Node*)Memory::alloc(sizeof(Node) * _capacity);\n    for(Node* node = _queue, * end = _queue + _capacity; node < end; ++node)\n      node->state = Node::free;\n\n    _freeNodes = _capacity;\n    _occupiedNodes = 0;\n    _writeIndex = -1;\n    _safeWriteIndex = -1;\n    _readIndex = -1;\n    _safeReadIndex = -1;\n  }\n\n  ~LockFreeQueueSlow1()\n  {\n    for(Node* node = _queue, * end = _queue + _capacity; node < end; ++node)\n      switch(node->state)\n      {\n      case Node::set:\n      case Node::occupied:\n        (&node->data)->~T();\n        break;\n      default:\n        break;\n      }\n    Memory::free(_queue);\n  }\n  \n  usize capacity() const {return _capacity;}\n  \n  usize size() const {return _capacity - _freeNodes;}\n  \n  bool push(const T& data)\n  {\n  begin:\n    usize freeNodes = _freeNodes;\n    if(freeNodes == 0)\n      return false; // queue is full\n    if(Atomic::compareAndSwap(_freeNodes, freeNodes, freeNodes - 1) != freeNodes)\n      goto begin;\n    usize writeIndex = Atomic::increment(_writeIndex);\n    Node* node = &_queue[writeIndex & _capacityMask];\n    ASSERT(node->state == Node::free);\n    new (&node->data)T(data);\n    Atomic::store(node->state, Node::set);\n  commit:\n    usize safeWriteIndex = _safeWriteIndex;\n    usize nextSafeWriteIndex = safeWriteIndex + 1;\n  commitNext:\n    node = &_queue[nextSafeWriteIndex & _capacityMask];\n    if(node->state == Node::set && Atomic::compareAndSwap(node->state, Node::set, Node::occupied) == Node::set)\n    {\n      if(Atomic::compareAndSwap(_safeWriteIndex, safeWriteIndex, nextSafeWriteIndex) == safeWriteIndex)\n      {\n        Atomic::increment(_occupiedNodes);\n        safeWriteIndex = nextSafeWriteIndex;\n        ++nextSafeWriteIndex;\n        goto commitNext;\n      }\n      else\n        node->state = Node::set;\n      goto commit;\n    }\n    return true;\n  }\n  \n  bool pop(T& result)\n  {\n  begin:\n    usize occupiedNodes = _occupiedNodes;\n    if(occupiedNodes == 0)\n      return false; // queue is empty\n    if(Atomic::compareAndSwap(_occupiedNodes, occupiedNodes, occupiedNodes - 1) != occupiedNodes)\n      goto begin;\n    usize readIndex = Atomic::increment(_readIndex);\n    Node* node = &_queue[readIndex & _capacityMask];\n    ASSERT(node->state == Node::occupied);\n    result = node->data;\n    (&node->data)->~T();\n    Atomic::store(node->state, Node::unset);\n  release:\n    usize safeReadIndex = _safeReadIndex;\n    usize nextSafeReadIndex = safeReadIndex + 1;\n  releaseNext:\n    node = &_queue[nextSafeReadIndex & _capacityMask];\n    if(node->state == Node::unset && Atomic::compareAndSwap(node->state, Node::unset, Node::free) == Node::unset)\n    {\n      if(Atomic::compareAndSwap(_safeReadIndex, safeReadIndex, nextSafeReadIndex) == safeReadIndex)\n      {\n        Atomic::increment(_freeNodes);\n        safeReadIndex = nextSafeReadIndex;\n        ++nextSafeReadIndex;\n        goto releaseNext;\n      }\n      else\n        node->state = Node::unset;\n      goto release;\n    }\n    return true;\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n    enum State\n    {\n      free,\n      set,\n      occupied,\n      unset,\n    };\n    volatile int state;\n  };\n\nprivate:\n  usize _capacityMask;\n  Node* _queue;\n  usize _capacity;\n  volatile usize _freeNodes;\n  volatile usize _occupiedNodes;\n  volatile usize _writeIndex;\n  volatile usize _safeWriteIndex;\n  volatile usize _readIndex;\n  volatile usize _safeReadIndex;\n};\n"
  },
  {
    "path": "LockFreeQueueSlow2.h",
    "content": "\n#pragma once\n\n#include <nstd/Atomic.h>\n#include <nstd/Memory.h>\n\ntemplate <typename T> class LockFreeQueueSlow2\n{\npublic:\n  explicit LockFreeQueueSlow2(usize capacity)\n  {\n    _capacityMask = capacity - 1;\n    for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _capacityMask |= _capacityMask >> i;\n    _capacity = _capacityMask + 1;\n\n    _queue = (Node*)Memory::alloc(sizeof(Node) * _capacity);\n    for(usize i = 0; i < _capacity; ++i)\n    {\n      _queue[i].tail = i;\n      _queue[i].head = i - 1;\n    }\n\n    _tail = 0;\n    _head = 0;\n  }\n\n  ~LockFreeQueueSlow2()\n  {\n    for(usize i = _head; i != _tail; ++i)\n      (&_queue[i & _capacityMask].data)->~T();\n\n    Memory::free(_queue);\n  }\n  \n  usize capacity() const {return _capacity;}\n  \n  usize size() const\n  {\n    usize head = Atomic::load(_head);\n    return _tail - head;\n  }\n  \n  bool push(const T& data)\n  {\n  begin:\n    usize tail = _tail;\n    Node* node = &_queue[tail & _capacityMask];\n    usize newTail = tail + 1;\n    usize nodeTail = node->tail;\n    if(nodeTail == tail)\n    {\n      nodeTail = Atomic::compareAndSwap(node->tail, tail, newTail);\n      if(nodeTail == tail)\n      {\n        Atomic::compareAndSwap(_tail, tail, newTail);\n        new (&node->data)T(data);\n        Atomic::store(node->head, tail);\n        return true;\n      }\n    }\n    if(nodeTail == newTail)\n    {\n      Atomic::compareAndSwap(_tail, tail, newTail);\n      goto begin;\n    }\n    else\n      return false;\n  }\n\n  bool pop(T& result)\n  {\n  begin:\n    usize head = _head;\n    Node* node = &_queue[head & _capacityMask];\n    usize newHead = head + 1;\n    usize nodeHead = node->head;\n    if(nodeHead == head)\n    {\n      nodeHead = Atomic::compareAndSwap(node->head, head, newHead);\n      if(nodeHead == head)\n      {\n        Atomic::compareAndSwap(_head, head, newHead);\n        result = node->data;\n        (&node->data)->~T();\n        Atomic::store(node->tail, head + _capacity);\n        return true;\n      }\n    }\n    if(nodeHead == newHead)\n    {\n      Atomic::compareAndSwap(_head, head, newHead);\n      goto begin;\n    }\n    else\n      return false;\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n    volatile usize head;\n    volatile usize tail;\n  };\n\nprivate:\n  usize _capacityMask;\n  Node* _queue;\n  usize _capacity;\n  char cacheLinePad1[64];\n  volatile usize _head;\n  char cacheLinePad2[64];\n  volatile usize _tail;\n  char cacheLinePad3[64];\n};\n"
  },
  {
    "path": "LockFreeQueueSlow3.h",
    "content": "\n#pragma once\n\n#include <nstd/Atomic.h>\n#include <nstd/Memory.h>\n\ntemplate <typename T> class LockFreeQueueSlow3\n{\npublic:\n  explicit LockFreeQueueSlow3(usize capacity)\n  {\n    _capacityMask = capacity - 1;\n    for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _capacityMask |= _capacityMask >> i;\n    _capacity = _capacityMask + 1;\n\n    _queue = (Node*)Memory::alloc(sizeof(Node) * _capacity);\n    for(usize i = 0; i < _capacity; ++i)\n      _queue[i].state = 0;\n\n    _tail = 0;\n    _head = 0;\n  }\n\n  ~LockFreeQueueSlow3()\n  {\n    for(usize i = _head; i != _tail; ++i)\n      (&_queue[i & _capacityMask].data)->~T();\n\n    Memory::free(_queue);\n  }\n  \n  usize capacity() const {return _capacity;}\n  \n  usize size() const\n  {\n    usize head = Atomic::load(_head);\n    return _tail - head;\n  }\n  \n  bool push(const T& data)\n  {\n    for(;;)\n    {\n      usize tail = _tail;\n      Node* node = &_queue[tail & _capacityMask];\n      switch(Atomic::compareAndSwap(node->state, 0, 2))\n      {\n      case 2:\n        continue;\n      case 0:\n        break;\n      default:\n        return false;\n      }\n      if(Atomic::compareAndSwap(_tail, tail, tail + 1) == tail)\n      {\n        new (&node->data)T(data);\n        Atomic::store(node->state, 1);\n        return true;\n      }\n      else\n        node->state = 0;\n    }\n  }\n\n  bool pop(T& result)\n  {\n    for(;;)\n    {\n      usize head = _head;\n      Node* node = &_queue[head & _capacityMask];\n      switch(Atomic::compareAndSwap(node->state, 1, 3))\n      {\n      case 3:\n        continue;\n      case 1:\n        break;\n      default:\n        return false;\n      }\n      if(Atomic::compareAndSwap(_head, head, head + 1) == head)\n      {\n        result = node->data;\n        (&node->data)->~T();\n        Atomic::store(node->state, 0);\n        return true;\n      }\n      else\n        node->state = 1;\n    }\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n    volatile int state;\n  };\n\nprivate:\n  usize _capacityMask;\n  Node* _queue;\n  usize _capacity;\n  char cacheLinePad1[64];\n  volatile usize _tail;\n  char cacheLinePad2[64];\n  volatile usize _head;\n  char cacheLinePad3[64];\n};\n"
  },
  {
    "path": "Marefile",
    "content": "\n\nif(tool == \"vcxproj\") {\n  platforms = { \"Win32\", \"x64\" }\n}\n\nbuildDir = \"Build/$(configuration)/.$(target)\"\n\ntargets = {\n\n  LockFreeQueue = cppApplication + {\n    dependencies = { \"libnstd\" }\n    includePaths = { \"Ext/libnstd/include\" }\n    libPaths = { \"Build/$(configuration)/.libnstd\" }\n    libs = { \"nstd\" }\n    root = \"Src\"\n    files = {\n      \"*.cpp\" = cppSource\n      \"*.h\"\n    }\n    if tool == \"vcxproj\" {\n      linkFlags += { \"/SUBSYSTEM:CONSOLE\" }\n    }\n    if platform == \"Linux\" {\n      libs += { \"pthread\", \"rt\" }\n      cppFlags += { \"-std=c++11\" }\n    }\n    defines -= \"NDEBUG\"\n  }\n  \n  include \"Ext/libnstd/libnstd.mare\"\n  libnstd += {\n    folder = \"Ext\"\n  }\n}\n"
  },
  {
    "path": "MutexLockQueue.h",
    "content": "\n#pragma once\n\n#include <nstd/Mutex.h>\n#include <nstd/Memory.h>\n\ntemplate <typename T> class MutexLockQueue\n{\npublic:\n  explicit MutexLockQueue(usize capacity)\n  {\n    _capacityMask = capacity - 1;\n    for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _capacityMask |= _capacityMask >> i;\n    _capacity = _capacityMask + 1;\n\n    _queue = (Node*)Memory::alloc(sizeof(Node) * _capacity);\n\n    _head = 0;\n    _tail = 0;\n  }\n\n  ~MutexLockQueue()\n  {\n    for(usize i = _head; i != _tail; ++i)\n      (&_queue[i & _capacityMask].data)->~T();\n    Memory::free(_queue);\n  }\n  \n  usize capacity() const {return _capacity;}\n  \n  usize size() const\n  {\n    usize result;\n    _mutex.lock();\n    result = _tail - _head;\n    _mutex.unlock();\n    return result;\n  }\n  \n  bool push(const T& data)\n  {\n    _mutex.lock();\n    if(_tail - _head == _capacity)\n    {\n      _mutex.unlock();\n      return false; // queue is full\n    }\n    Node& node = _queue[(_tail++) & _capacityMask];\n    new (&node.data)T(data);\n    _mutex.unlock();\n    return true;\n  }\n  \n  bool pop(T& result)\n  {\n    _mutex.lock();\n    if(_head == _tail)\n    {\n      _mutex.unlock();\n      return false; // queue is empty\n    }\n    Node& node = _queue[(_head++) & _capacityMask];\n    result = node.data;\n    (&node.data)->~T();\n    _mutex.unlock();\n    return true;\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n  };\n\nprivate:\n  usize _capacityMask;\n  Node* _queue;\n  usize _capacity;\n  usize _head;\n  usize _tail;\n  mutable Mutex _mutex;\n};\n"
  },
  {
    "path": "README.md",
    "content": "LockFreeQueue\n=============\n\nHere, I am testing some multi-producer multi-consumer bounded ring buffer FIFO queue implementations for fun.\n\n* [LockFreeQueueCpp11.h](LockFreeQueueCpp11.h) - The fastest lock free queue I have managed to implement.\n* [LockFreeQueue.h](LockFreeQueue.h) - The fastest lock free queue I have managed to implement without c++11. It is equally fast as LockFreeQueueCpp11.h.\n* [mpmc_bounded_queue.h](mpmc_bounded_queue.h) - Bounded MPMC queue by [Dmitry Vyukov, 2011]\n* [LockFreeQueueSlow1.h](LockFreeQueueSlow1.h) - My first attempt at implementing a lock free queue. It is working correctly, but it is a lot slower than LockFreeQueue.h.\n* [LockFreeQueueSlow2.h](LockFreeQueueSlow2.h) - A lock free queue based on [John D. Valois, 1994]. The queue uses Valois' algorithm adapted to a ring buffer structure with some modifications to tackle the ABA-Problem.\n* [LockFreeQueueSlow3.h](LockFreeQueueSlow3.h) - Another lock free queue almost as fast as LockFreeQueue.h.\n* [MutexLockQueue.h](MutexLockQueue.h) - A naive queue implementation that uses a conventional mutex lock (CriticalSecion / pthread-Mutex).\n* [SpinLockQueue.h](SpinLockQueue.h) - A naive queue implementation that uses an atomic TestAndSet-lock.\n\nAnd for the fun of it, here is a multi-producer multi-consumer LIFO queue:\n\n* [LockFreeLifoQueue.h](LockFreeLifoQueue.h) - A lock free multi-producer multi-consumer bounded LIFO queue.\n\n#### References\n\n[John D. Valois, 1994] - Implementing Lock-Free Queues<br/>\n[Dmitry Vyukov, 2011] - [Bounded MPMC queue](http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue)"
  },
  {
    "path": "SpinLockQueue.h",
    "content": "\n#pragma once\n\n#include <nstd/Atomic.h>\n#include <nstd/Memory.h>\n\ntemplate <typename T> class SpinLockQueue\n{\npublic:\n  explicit SpinLockQueue(usize capacity)\n  {\n    _capacityMask = capacity - 1;\n    for(usize i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      _capacityMask |= _capacityMask >> i;\n    _capacity = _capacityMask + 1;\n\n    _queue = (Node*)Memory::alloc(sizeof(Node) * _capacity);\n\n    _lock = 0;\n    _head = 0;\n    _tail = 0;\n  }\n\n  ~SpinLockQueue()\n  {\n    for(usize i = _head; i != _tail; ++i)\n      (&_queue[i & _capacityMask].data)->~T();\n    Memory::free(_queue);\n  }\n  \n  usize capacity() const {return _capacity;}\n  \n  usize size() const\n  {\n    usize result;\n    while(Atomic::testAndSet(_lock) != 0);\n    result = _tail - _head;\n    Atomic::store(_lock, 0);\n    return result;\n  }\n  \n  bool push(const T& data)\n  {\n    while(Atomic::testAndSet(_lock) != 0);\n    if(_tail - _head == _capacity)\n    {\n      _lock = 0;\n      return false; // queue is full\n    }\n    Node& node = _queue[(_tail++) & _capacityMask];\n    new (&node.data)T(data);\n    Atomic::store(_lock, 0);\n    return true;\n  }\n  \n  bool pop(T& result)\n  {\n    while(Atomic::testAndSet(_lock) != 0);\n    if(_head == _tail)\n    {\n      _lock = 0;\n      return false; // queue is empty\n    }\n    Node& node = _queue[(_head++) & _capacityMask];\n    result = node.data;\n    (&node.data)->~T();\n    Atomic::store(_lock, 0);\n    return true;\n  }\n\nprivate:\n  struct Node\n  {\n    T data;\n  };\n\nprivate:\n  usize _capacityMask;\n  Node* _queue;\n  usize _capacity;\n  usize _head;\n  usize _tail;\n  mutable volatile int32 _lock;\n};\n"
  },
  {
    "path": "Test.cpp",
    "content": "\n#include <nstd/Console.h>\n#include <nstd/Debug.h>\n#include <nstd/Thread.h>\n#include <nstd/List.h>\n#include <nstd/Time.h>\n\n#include \"LockFreeQueueCpp11.h\"\n#include \"LockFreeQueue.h\"\n#include \"LockFreeQueueSlow1.h\"\n#include \"LockFreeQueueSlow2.h\"\n#include \"LockFreeQueueSlow3.h\"\n#include \"MutexLockQueue.h\"\n#include \"SpinLockQueue.h\"\n#include \"mpmc_bounded_queue.h\"\n#include \"LockFreeLifoQueue.h\"\n\nstatic const int testItems = 250000 * 64 / 3 * 10;\nstatic const int testThreadConsumerThreads = 8;\nstatic const int testThreadProducerThreads = 8;\nstatic const int testItemsPerConsumerThread = testItems / testThreadConsumerThreads;\nstatic const int testItemsPerProducerThread = testItems / testThreadProducerThreads;\n\ntemplate<typename T> class IQueue\n{\npublic:\n  virtual usize size() const = 0;\n  virtual usize capacity() const = 0;\n  virtual bool push(const T& data) = 0;\n  virtual bool pop(T& result) = 0;\n};\n\ntemplate<typename T, class Q> class TestQueue : public IQueue<T>\n{\npublic:\n  TestQueue(usize size) : queue(size) {}\n  usize size() const {return queue.size();}\n  usize capacity() const {return queue.capacity();}\n  bool push(const T& data) {return queue.push(data);}\n  bool pop(T& result) {return queue.pop(result);}\nprivate:\n  Q queue;\n};\n\nvolatile usize producerSum;\nvolatile usize consumerSum;\nvolatile int64 maxPushDuration;\nvolatile int64 maxPopDuration;\n\nuint producerThread(void* param)\n{\n  IQueue<int>* queue = (IQueue<int>*)param;\n  for(int i = 0; i < testItemsPerProducerThread; ++i)\n  {\n    for(;;)\n    {\n      int64 startTime = Time::microTicks();\n      while(!queue->push(i))\n      {\n        Thread::yield();\n        startTime = Time::microTicks();\n      }\n      {\n        int64 duration = Time::microTicks() - startTime;\n        for(;;)\n        {\n          int64 lmaxPushDuration = maxPushDuration;\n          if(duration <= lmaxPushDuration || Atomic::compareAndSwap(maxPushDuration, lmaxPushDuration, duration) == lmaxPushDuration)\n            break;\n        }\n        break;\n      }\n    }\n    Atomic::fetchAndAdd(producerSum, i);\n  }\n  return 0;\n}\n\nuint consumerThread(void* param)\n{\n  IQueue<int>* queue = (IQueue<int>*)param;\n  int val;\n  for(int i = 0; i < testItemsPerConsumerThread; ++i)\n  {\n    for(;;)\n    {\n      int64 startTime = Time::microTicks();\n      while(!queue->pop(val))\n      {\n        Thread::yield();\n        startTime = Time::microTicks();\n      }\n      {\n        int64 duration = Time::microTicks() - startTime;\n        for(;;)\n        {\n          int64 lmaxPopDuration = maxPopDuration;\n          if(duration <= lmaxPopDuration || Atomic::compareAndSwap(maxPopDuration, lmaxPopDuration, duration) == lmaxPopDuration)\n            break;\n        }\n        break;\n      }\n    }\n    Atomic::fetchAndAdd(consumerSum, val);\n  }\n  return 0;\n}\n\ntemplate<class Q> void testQueue(const String& name, bool fifo = false)\n{\n  Console::printf(_T(\"Testing %s... \\n\"), (const tchar*)name);\n\n  volatile int32 int32_ = 0;\n  volatile uint32 uint32_ = 0;\n  ASSERT(Atomic::compareAndSwap(int32_, 0, 1) == 0);\n  ASSERT(int32_ == 1);\n  ASSERT(Atomic::compareAndSwap(uint32_, 0, 1) == 0);\n  ASSERT(uint32_ == 1);\n\n  volatile int64 int64_ = 0;\n  volatile uint64 uint64_ = 0;\n  ASSERT(Atomic::compareAndSwap(int64_, 0, 1) == 0);\n  ASSERT(int64_ == 1);\n  ASSERT(Atomic::compareAndSwap(uint64_, 0, 1) == 0);\n  ASSERT(uint64_ == 1);\n\n  {\n    TestQueue<int, Q> queue(10000);\n    int result;\n    ASSERT(queue.capacity() >= 10000);\n    ASSERT(!queue.pop(result));\n    ASSERT(queue.push(42));\n    ASSERT(queue.pop(result));\n    ASSERT(result == 42);\n    ASSERT(!queue.pop(result));\n  }\n\n  {\n    TestQueue<int, Q> queue(2);\n    int result;\n    ASSERT(queue.capacity() >= 2);\n    ASSERT(!queue.pop(result));\n    ASSERT(queue.push(42));\n    ASSERT(queue.push(43));\n    ASSERT(queue.pop(result));\n    ASSERT(result == (fifo ? 43 : 42));\n    ASSERT(queue.pop(result));\n    ASSERT(result == (fifo ? 42 : 43));\n    ASSERT(!queue.pop(result));\n    ASSERT(queue.push(44));\n    ASSERT(queue.push(45));\n    ASSERT(queue.pop(result));\n    ASSERT(result == (fifo ? 45 : 44));\n    ASSERT(queue.push(47));\n  }\n\n  producerSum = 0;\n  consumerSum = 0;\n  maxPushDuration = 0;\n  maxPopDuration = 0;\n\n  int64 microStartTime = Time::microTicks();\n  {\n    TestQueue<int, Q> queue(100);\n    List<Thread*> threads;\n    for(int i = 0; i < testThreadProducerThreads; ++i)\n    {\n      Thread* thread = new Thread;\n      thread->start(producerThread, &queue);\n      threads.append(thread);\n    }\n    for(int i = 0; i < testThreadConsumerThreads; ++i)\n    {\n      Thread* thread = new Thread;\n      thread->start(consumerThread, &queue);\n      threads.append(thread);\n    }\n    for(List<Thread*>::Iterator i = threads.begin(), end = threads.end(); i != end; ++i)\n    {\n      Thread* thread = *i;\n      thread->join();\n      delete thread;\n    }\n    ASSERT(queue.size() == 0);\n    ASSERT(producerSum == consumerSum);\n  }\n  int64 microDuration = Time::microTicks() - microStartTime;\n  Console::printf(_T(\"%lld ms, maxPush: %lld microseconds, maxPop: %lld microseconds\\n\"), microDuration / 1000, maxPushDuration, maxPopDuration);\n}\n\nint main(int argc, char* argv[])\n{\n  for(int i = 0; i < 3; ++i)\n  {\n    Console::printf(_T(\"--- Run %d ---\\n\"), i);\n    testQueue<LockFreeQueueCpp11<int> >(\"LockFreeQueueCpp11\");\n    testQueue<mpmc_bounded_queue<int> >(\"mpmc_bounded_queue\");\n    testQueue<LockFreeQueue<int> >(\"LockFreeQueue\");\n    testQueue<LockFreeQueueSlow1<int> >(\"LockFreeQueueSlow1\");\n    testQueue<LockFreeQueueSlow2<int> >(\"LockFreeQueueSlow2\");\n    testQueue<LockFreeQueueSlow3<int> >(\"LockFreeQueueSlow3\");\n    testQueue<MutexLockQueue<int> >(\"MutexLockQueue\");\n    testQueue<SpinLockQueue<int> >(\"SpinLockQueue\");\n    testQueue<LockFreeLifoQueue<int> >(\"LockFreeLifoQueue\", true);\n  }\n\n  return 0;\n}\n"
  },
  {
    "path": "generate",
    "content": "#!/bin/sh\n\n[ ! -f \"Build/Debug/.mare/mare\" ] && Ext/mare/compile --buildDir=Build/Debug/.mare --outputDir=Build/Debug/.mare --sourceDir=Ext/mare/src\nBuild/Debug/.mare/mare $@\n\n"
  },
  {
    "path": "generate.bat",
    "content": "@echo off\n\nif not exist Build\\Debug\\.mare\\mare.exe call Ext\\mare\\compile.bat --buildDir=Build/Debug/.mare --outputDir=Build/Debug/.mare --sourceDir=Ext/mare/src\nif not \"%1\"==\"\" (Build\\Debug\\.mare\\mare.exe %*) else Build\\Debug\\.mare\\mare.exe --vcxproj=2013\n"
  },
  {
    "path": "mpmc_bounded_queue.h",
    "content": "/*  Multi-producer/multi-consumer bounded queue.\n *  Copyright (c) 2010-2011, Dmitry Vyukov. All rights reserved.\n *  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n *     1. Redistributions of source code must retain the above copyright notice, this list of\n *        conditions and the following disclaimer.\n *     2. Redistributions in binary form must reproduce the above copyright notice, this list\n *        of conditions and the following disclaimer in the documentation and/or other materials\n *        provided with the distribution.\n *  THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\n *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL\n *  DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n *  The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted\n *  as representing official policies, either expressed or implied, of Dmitry Vyukov.\n */ \n\n#include <atomic>\n#include <cassert>\n\ntemplate<typename T>\nclass mpmc_bounded_queue\n{\nprivate:\n  static inline size_t nextPowerOfTwo(size_t buffer_size)\n  {\n    size_t result = buffer_size - 1;\n    for(size_t i = 1; i <= sizeof(void*) * 4; i <<= 1)\n      result |= result >> i;\n    return result + 1;\n  }\npublic:\n  mpmc_bounded_queue(size_t buffer_size)\n    : buffer_(new cell_t [nextPowerOfTwo(buffer_size)])\n    , buffer_mask_(nextPowerOfTwo(buffer_size) - 1)\n  {\n    buffer_size = buffer_mask_ + 1;\n    assert((buffer_size >= 2) &&\n      ((buffer_size & (buffer_size - 1)) == 0));\n    for (size_t i = 0; i != buffer_size; i += 1)\n      buffer_[i].sequence_.store(i, std::memory_order_relaxed);\n    enqueue_pos_.store(0, std::memory_order_relaxed);\n    dequeue_pos_.store(0, std::memory_order_relaxed);\n  }\n\n  ~mpmc_bounded_queue()\n  {\n    delete [] buffer_;\n  }\n\n  size_t size() const\n  {\n    size_t head = dequeue_pos_.load(std::memory_order_acquire);;\n    return enqueue_pos_.load(std::memory_order_relaxed) - head;\n  }\n\n  size_t capacity() const\n  {\n    return buffer_mask_ + 1;\n  }\n\n  bool push(T const& data)\n  {\n    cell_t* cell;\n    size_t pos = enqueue_pos_.load(std::memory_order_relaxed);\n    for (;;)\n    {\n      cell = &buffer_[pos & buffer_mask_];\n      size_t seq = \n        cell->sequence_.load(std::memory_order_acquire);\n      intptr_t dif = (intptr_t)seq - (intptr_t)pos;\n      if (dif == 0)\n      {\n        if (enqueue_pos_.compare_exchange_weak\n            (pos, pos + 1, std::memory_order_relaxed))\n          break;\n      }\n      else if (dif < 0)\n        return false;\n      else\n        pos = enqueue_pos_.load(std::memory_order_relaxed);\n    }\n    cell->data_ = data;\n    cell->sequence_.store(pos + 1, std::memory_order_release);\n    return true;\n  }\n\n  bool pop(T& data)\n  {\n    cell_t* cell;\n    size_t pos = dequeue_pos_.load(std::memory_order_relaxed);\n    for (;;)\n    {\n      cell = &buffer_[pos & buffer_mask_];\n      size_t seq = \n        cell->sequence_.load(std::memory_order_acquire);\n      intptr_t dif = (intptr_t)seq - (intptr_t)(pos + 1);\n      if (dif == 0)\n      {\n        if (dequeue_pos_.compare_exchange_weak\n            (pos, pos + 1, std::memory_order_relaxed))\n          break;\n      }\n      else if (dif < 0)\n        return false;\n      else\n        pos = dequeue_pos_.load(std::memory_order_relaxed);\n    }\n    data = cell->data_;\n    cell->sequence_.store\n      (pos + buffer_mask_ + 1, std::memory_order_release);\n    return true;\n  }\n\nprivate:\n  struct cell_t\n  {\n    std::atomic<size_t>   sequence_;\n    T                     data_;\n  };\n  static size_t const     cacheline_size = 64;\n  typedef char            cacheline_pad_t [cacheline_size];\n  cacheline_pad_t         pad0_;\n  cell_t* const           buffer_;\n  size_t const            buffer_mask_;\n  cacheline_pad_t         pad1_;\n  std::atomic<size_t>     enqueue_pos_;\n  cacheline_pad_t         pad2_;\n  std::atomic<size_t>     dequeue_pos_;\n  cacheline_pad_t         pad3_;\n  mpmc_bounded_queue(mpmc_bounded_queue const&);\n  void operator = (mpmc_bounded_queue const&);\n};\n"
  }
]