无锁队列-使用hazard指针解决ABA问题

无锁队列-使用hazard指针解决ABA问题

实现一个无锁队列, 原子操作使用了tbb::atomic, ABA问题使用hazard指针解决

 无锁队列实现:

  1. /* 
  2.  * msque.hpp 
  3.  *       Created on: 2012-8-30 
  4.  *           Author: qianqians 
  5.  * msque: 
  6.  */  
  7.   
  8. #ifndef _MSQUE_H  
  9. #define _MSQUE_H  
  10.   
  11. #include <boost/bind.hpp>  
  12. #include <boost/atomic.hpp>  
  13. #include <boost/pool/pool_alloc.hpp>  
  14.   
  15. #include <angelica/container/detail/_hazard_ptr.h>  
  16.   
  17. namespace angelica {  
  18. namespace container{  
  19.   
  20. template <typename T, typename _Allocator = boost::pool_allocator<T> >  
  21. class msque{  
  22. private:  
  23.     struct _list_node{  
  24.         _list_node () {_next = 0;}  
  25.         _list_node (const T & val) : data(val) {_next = 0;}  
  26.         ~_list_node () {}  
  27.   
  28.         T data;  
  29.         boost::atomic<_list_node *> _next;  
  30.     };  
  31.   
  32.     struct _list{  
  33.         boost::atomic<_list_node *> _begin;  
  34.         boost::atomic<_list_node *> _end;  
  35.         boost::atomic_uint32_t _size;  
  36.     };  
  37.   
  38.     typedef angelica::container::detail::_hazard_ptr<_list_node> _hazard_ptr;  
  39.     typedef angelica::container::detail::_hazard_system<_list_node> _hazard_system;  
  40.     typedef typename _Allocator::template rebind<_list_node>::other _node_alloc;  
  41.     typedef typename _Allocator::template rebind<_list>::other _list_alloc;  
  42.   
  43. public:  
  44.     msque(void){  
  45.         __list.store(get_list());  
  46.     }  
  47.   
  48.     ~msque(void){  
  49.         put_list(__list.load());  
  50.     }  
  51.   
  52.     bool empty(){  
  53.         return (__list.load()->_size.load() == 0);  
  54.     }  
  55.   
  56.     std::size_t size(){  
  57.         return __list.load()->_size.load();  
  58.     }  
  59.   
  60.     void clear(){  
  61.         _list * _new_list = get_list();  
  62.         _list * _old_list = __list.exchange(_new_list);  
  63.         put_list(_old_list);  
  64.     }  
  65.   
  66.     void push(const T & data){  
  67.         _list_node * _null = 0;  
  68.   
  69.         _list_node * _node = 0;  
  70.         while(_node == 0){  
  71.             _node = get_node(data);  
  72.         }  
  73.   
  74.         _hazard_ptr * _hp = _hazard_sys.acquire();  
  75.         _hp->_hazard = __list.load()->_end.load();  
  76.         while(1){  
  77.             if(_hp->_hazard != __list.load()->_end.load()){  
  78.                 _hp->_hazard = __list.load()->_end.load();  
  79.                 continue;  
  80.             }  
  81.   
  82.             _list_node * _begin_node = __list.load()->_begin.load();  
  83.             _list_node * next = _hp->_hazard->_next.load();  
  84.   
  85.             if(_hp->_hazard != __list.load()->_end.load()){  
  86.                 _hp->_hazard = __list.load()->_end.load();  
  87.                 continue;  
  88.             }  
  89.   
  90.             if(next != 0){  
  91.                 __list.load()->_end.compare_exchange_weak(_hp->_hazard, next);  
  92.                 _hp->_hazard = __list.load()->_end.load();  
  93.                 continue;  
  94.             }  
  95.   
  96.             if (_hp->_hazard->_next.compare_exchange_weak(_null, _node)){  
  97.                 break;  
  98.             }else{  
  99.                 _null = 0;  
  100.                 _hp->_hazard = __list.load()->_end.load();  
  101.             }  
  102.         }  
  103.         __list.load()->_end.compare_exchange_weak(_hp->_hazard, _node);   
  104.   
  105.         _hazard_sys.release(_hp);  
  106.   
  107.         __list.load()->_size++;  
  108.     }  
  109.   
  110.     bool pop(T & data){  
  111.         bool ret = true;  
  112.   
  113.         _hazard_ptr * _hp_begin = _hazard_sys.acquire();  
  114.         _hazard_ptr * _hp_next = _hazard_sys.acquire();  
  115.         _hp_begin->_hazard = __list.load()->_begin.load();  
  116.         while(1){     
  117.             if(_hp_begin->_hazard != __list.load()->_begin.load()){  
  118.                 _hp_begin->_hazard = __list.load()->_begin.load();  
  119.                 continue;  
  120.             }  
  121.   
  122.             _list_node * end = __list.load()->_end.load();  
  123.             _hp_next->_hazard = _hp_begin->_hazard->_next.load();  
  124.   
  125.             if(_hp_next->_hazard == 0){  
  126.                 ret = false;  
  127.                 goto end;  
  128.             }  
  129.   
  130.             if(_hp_begin->_hazard != __list.load()->_begin.load()){  
  131.                 _hp_begin->_hazard = __list.load()->_begin.load();  
  132.                 continue;  
  133.             }  
  134.   
  135.             if(end == _hp_begin->_hazard){  
  136.                 __list.load()->_end.compare_exchange_weak(end, _hp_next->_hazard);  
  137.                 _hp_begin->_hazard = __list.load()->_begin.load();  
  138.                 continue;  
  139.             }  
  140.   
  141.             if(__list.load()->_begin.compare_exchange_strong(_hp_begin->_hazard, _hp_next->_hazard)){  
  142.                 break;  
  143.             }  
  144.         }  
  145.         data = _hp_next->_hazard->data;  
  146.         _hazard_sys.retire(_hp_begin->_hazard, boost::bind(&angelica::container::msque<T>::put_node, this, _1));  
  147.   
  148.         __list.load()->_size--;  
  149.   
  150.     end:  
  151.         _hazard_sys.release(_hp_begin);  
  152.         _hazard_sys.release(_hp_next);  
  153.   
  154.         return ret;  
  155.     }  
  156.   
  157. private:  
  158.     _list * get_list(){  
  159.         _list * __list = __list_alloc.allocate(1);  
  160.         __list->_size = 0;  
  161.   
  162.         _list_node * _node = __node_alloc.allocate(1);  
  163.         _node->_next.store(0);  
  164.         __list->_begin.store(_node);  
  165.         __list->_end.store(_node);  
  166.   
  167.         return __list;  
  168.     }  
  169.   
  170.     void put_list(_list * _p){  
  171.         _list_node * _node = _p->_begin;  
  172.         do{  
  173.             _list_node * _tmp = _node;  
  174.             _node = _node->_next;  
  175.   
  176.             _hazard_sys.retire(_tmp, boost::bind(&angelica::container::msque<T>::put_node, this, _1));  
  177.         }while(_node != 0);  
  178.         __list_alloc.deallocate(_p, 1);  
  179.     }  
  180.   
  181.     _list_node * get_node(const T & data){  
  182.         _list_node * _node = __node_alloc.allocate(1);  
  183.         _node->data = data;  
  184.         _node->_next = 0;  
  185.   
  186.         return _node;  
  187.     }  
  188.   
  189.     void put_node(_list_node * _p){  
  190.         __node_alloc.deallocate(_p, 1);  
  191.     }  
  192.   
  193. private:  
  194.     boost::atomic<_list *> __list;  
  195.     _list_alloc __list_alloc;  
  196.     _node_alloc __node_alloc;  
  197.     _hazard_system _hazard_sys;  
  198.   
  199.     _Allocator __T_alloc;  
  200.   
  201. };  
  202.   
  203. /* angelica */  
  204. /* container */  
  205. #endif //_MSQUE_H  

  

 hazard指针实现:

  1. /* 
  2.  * _hazard_ptr.hpp 
  3.  *  Created on: 2012-8-26 
  4.  *      Author: qianqians 
  5.  * _hazard_ptr: Used to solve the ABA problem 
  6.  */  
  7.   
  8. #ifndef _HAZARD_PTR_H  
  9. #define _HAZARD_PTR_H  
  10.   
  11. #include <vector>  
  12. #include <boost/thread.hpp>  
  13. #include <boost/function.hpp>  
  14. #include <boost/foreach.hpp>  
  15. #include <boost/atomic.hpp>  
  16. #include <boost/pool/pool_alloc.hpp>  
  17.   
  18. namespace angelica{  
  19. namespace container{  
  20. namespace detail{  
  21.   
  22. // _hazard_ptr  
  23. template <typename X>  
  24. struct _hazard_ptr{  
  25.     X * _hazard; //  
  26.     boost::atomic_int32_t _active; // 0 使用中/1 未使用  
  27. };  
  28.   
  29. // hazard system   
  30. template <typename T>  
  31. class _hazard_system{  
  32. public:  
  33.     _hazard_system(){  
  34.         llen.store(0);  
  35.   
  36.         re_list_set.resize(8);  
  37.         for(int i = 0; i < 8; i++){  
  38.             re_list_set[i] = new recover_list;  
  39.             re_list_set[i]->active.store(1);  
  40.         }  
  41.   
  42.         _head = _get_node();  
  43.     }  
  44.   
  45.     ~_hazard_system(){  
  46.         BOOST_FOREACH(recover_list * _re_list, re_list_set){  
  47.             if(!_re_list->re_vector.empty()){  
  48.                 BOOST_FOREACH(_deallocate_data var, _re_list->re_vector){  
  49.                     var.second(var.first);  
  50.                 }  
  51.                 _re_list->re_vector.clear();  
  52.             }  
  53.         }  
  54.         re_list_set.clear();  
  55.     }  
  56.   
  57. private:  
  58.     // lock-free list(push only) storage _hazard_ptr  
  59.     typedef _hazard_ptr<typename T> _hazard_ptr_;  
  60.     typedef struct _list_node{  
  61.         _hazard_ptr_ _hazard;  
  62.         boost::atomic<_list_node *> next;  
  63.     } _list_head;  
  64.   
  65.     // allocator   
  66.     boost::pool_allocator<_list_node> _alloc_list_node;  
  67.   
  68.     // hazard ptr list  
  69.     boost::atomic<_list_head *> _head;  
  70.     // list lenght  
  71.     boost::atomic_uint32_t llen;  
  72.   
  73.     _list_node * _get_node(){  
  74.         _list_node * _node = _alloc_list_node.allocate(1);  
  75.         _node->_hazard._hazard = 0;  
  76.         _node->_hazard._active = 1;  
  77.         _node->next = 0;  
  78.   
  79.         return _node;  
  80.     }  
  81.   
  82.     void _put_node(_list_node * _node){  
  83.         _alloc_list_node.deallocate(_node, 1);  
  84.     }  
  85.   
  86. public:  
  87.     _hazard_ptr_ * acquire(){  
  88.         // try to reuse a retired hazard ptr  
  89.         for(_list_node * _node = _head; _node; _node = _node->next){  
  90.             if (_node->_hazard._active.exchange(0) == 0)  
  91.                 continue;  
  92.             return &_node->_hazard;  
  93.         }  
  94.   
  95.         // alloc a new node   
  96.         _list_node * _new_node = _get_node();  
  97.         _new_node->_hazard._active.store(0);  
  98.         // increment the list length  
  99.         llen++;  
  100.         // push into list  
  101.         _new_node->next = _head.exchange(_new_node);  
  102.   
  103.         return &(_new_node->_hazard);  
  104.     }  
  105.   
  106.     void release(_hazard_ptr_ * ptr){  
  107.         ptr->_hazard = 0;  
  108.         ptr->_active.store(1);  
  109.     }  
  110.   
  111. private:  
  112.     // deallocate function  
  113.     typedef boost::function<void(typename T * )> fn_dealloc;  
  114.     // deallocate struct data  
  115.     typedef std::pair<typename T *, fn_dealloc> _deallocate_data;  
  116.     // recover list  
  117.     struct recover_list {  
  118.         std::vector<_deallocate_data> re_vector;  
  119.         boost::atomic_int32_t active; // 0 使用中 / 1 未使用  
  120.     };  
  121.   
  122.     // 回收队列集合  
  123.     std::vector<recover_list * > re_list_set;  
  124.   
  125. public:  
  126.     void retire(T * p,  fn_dealloc fn){  
  127.         // get tss rvector  
  128.         recover_list * _rvector_ptr = 0;  
  129.         while(1){  
  130.             for(int i = 0; i < 8; i++) {  
  131.                 if (re_list_set[i]->active.exchange(0) == 0){  
  132.                     continue;  
  133.                 }else{  
  134.                     _rvector_ptr = re_list_set[i];  
  135.                     break;  
  136.                 }  
  137.             }  
  138.   
  139.             if (_rvector_ptr != 0)  
  140.                 break;  
  141.         }  
  142.   
  143.         // push into rvector  
  144.         _rvector_ptr->re_vector.push_back(std::make_pair(p, fn));  
  145.   
  146.         // scan  
  147.         if(_rvector_ptr->re_vector.size() > 32 && _rvector_ptr->re_vector.size() > llen.load()){  
  148.             // scan hazard pointers list collecting all non-null ptrs  
  149.             std::vector<void *> hvector;  
  150.             for(_list_node * _node = _head; _node; _node = _node->next){  
  151.                 void * p = _node->_hazard._hazard;  
  152.                 if (p != 0)  
  153.                     hvector.push_back(p);  
  154.             }  
  155.   
  156.             // sort hazard list  
  157.             std::sort(hvector.begin(), hvector.end(), std::less<void*>());  
  158.   
  159.             // deallocator  
  160.             std::vector<_deallocate_data>::iterator iter = _rvector_ptr->re_vector.begin();  
  161.             while(iter != _rvector_ptr->re_vector.end()){  
  162.                 if(!std::binary_search(hvector.begin(), hvector.end(), iter->first)){  
  163.                     iter->second(iter->first);  
  164.   
  165.                     if(iter->first != _rvector_ptr->re_vector.back().first){  
  166.                         *iter = _rvector_ptr->re_vector.back();  
  167.                         _rvector_ptr->re_vector.pop_back();  
  168.                     }  
  169.                     else{  
  170.                         iter = _rvector_ptr->re_vector.erase(iter);  
  171.                     }  
  172.                 }  
  173.                 else{  
  174.                     ++iter;  
  175.                 }  
  176.             }  
  177.         }  
  178.   
  179.         _rvector_ptr->active.store(1);  
  180.     }  
  181.   
  182. };  
  183.   
  184. /* angelica */  
  185. /* container */  
  186. /* detail */  
  187. #endif // _HAZARD_PTR_H  

改用boost::atomic实现

tbb::atomic:http://threadingbuildingblocks.org/files/documentation/a00118.html 

关于hazard指针:http://blog.csdn.net/pongba/article/details/589864 

另一种lock-free queue:http://www.drdobbs.com/cpp/writing-lock-free-code-a-corrected-queue/210604448?pgno=2 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值