mwsr_queue.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #ifndef ROSRT_DETAIL_MWSR_QUEUE_H
36 #define ROSRT_DETAIL_MWSR_QUEUE_H
37 
38 #include "lockfree/object_pool.h"
39 #include <ros/atomic.h>
40 
41 namespace rosrt
42 {
43 namespace detail
44 {
45 
46 template<typename T>
47 class MWSRQueue
48 {
49 public:
50  struct Node
51  {
52  T val;
54  };
55 
56  MWSRQueue(uint32_t size)
57  : pool_(size, Node())
58  , head_(0)
59  {
60 
61  }
62 
63  bool push(const T& val)
64  {
65  Node* n = pool_.allocate();
66  if (!n)
67  {
68  return false;
69  }
70 
71  n->val = val;
72  Node* stale_head = head_.load(ros::memory_order_relaxed);
73  do
74  {
75  n->next = stale_head;
76  } while(!head_.compare_exchange_weak(stale_head, n, ros::memory_order_release));
77 
78  return true;
79  }
80 
81  Node* popAll()
82  {
83  Node* last = head_.exchange(0, ros::memory_order_consume);
84  Node* first = 0;
85 
86  // Reverse the list to get it back in push() order
87  while (last)
88  {
89  Node* tmp = last;
90  last = last->next;
91  tmp->next = first;
92  first = tmp;
93  }
94 
95  return first;
96  }
97 
98  void free(Node* node)
99  {
100  pool_.free(node);
101  }
102 
103 private:
105  ros::atomic<Node*> head_;
106 };
107 
108 } // namespace detail
109 } // namespace rosrt
110 
111 #endif // ROSRT_DETAIL_MWSR_QUEUE_H
ros::atomic< Node * > head_
Definition: mwsr_queue.h:105
void free(Node *node)
Definition: mwsr_queue.h:98
MWSRQueue(uint32_t size)
Definition: mwsr_queue.h:56
lockfree::ObjectPool< Node > pool_
Definition: mwsr_queue.h:104
Definition: managers.h:38
bool push(const T &val)
Definition: mwsr_queue.h:63


rosrt
Author(s): Josh Faust
autogenerated on Fri Apr 5 2019 02:16:39