OSDN Git Service

git-svn-id: http://10.144.169.20/repos/um/branches/l7vsd-3.x-shamshel-refine@7877...
authorkanda <kanda@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Wed, 3 Jun 2009 02:04:35 +0000 (02:04 +0000)
committerkanda <kanda@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Wed, 3 Jun 2009 02:04:35 +0000 (02:04 +0000)
include/atomic_ptr.h
include/lockfree_queue.h

index 51fe35a..b23d44c 100644 (file)
@@ -1,24 +1,24 @@
-#ifndef ATOMIC_PTR_H
-#define ATOMIC_PTR_H
+#ifndef ATOMIC_H
+#define ATOMIC_H
 #include <boost/utility.hpp>
 #include <iostream>
 
 namespace l7vs{
 
-template<class T> class atomic_ptr :boost::noncopyable{
+template<class T> class atomic :boost::noncopyable{
 protected:
-       T _p;
+       T volatile _p;
 public:
        T get(){ return _p; }
-       atomic_ptr& operator=(const T& _q) {
+       atomic& operator=(const T& _q) {
                __sync_lock_test_and_set(&_p,_q);
                return *this;
        }
-       atomic_ptr& operator++(){
+       atomic& operator++(){
                _p++;
                return *this;
        }
-       atomic_ptr& operator--(){
+       atomic& operator--(){
                _p--;
                return *this;
        }
@@ -43,15 +43,16 @@ public:
 };
 
 
+//int
 template <>
-class atomic_ptr<int>{
+class atomic<int>{
 protected:
-       int _p;
+       int volatile _p;
 public:
        int get(){
                return _p;
        }
-       atomic_ptr& operator=(const int _q) {
+       atomic& operator=(const int _q) {
                __sync_lock_test_and_set(&_p,_q);
                return *this;
        }
@@ -84,5 +85,47 @@ public:
 
 };
 
+//unsigned longlong
+template <>
+class atomic<unsigned long long>{
+protected:
+       unsigned long long volatile _p;
+public:
+       unsigned long long get(){
+               return _p;
+       }
+       atomic& operator=(const unsigned long long _q) {
+               __sync_lock_test_and_set(&_p,_q);
+               return *this;
+       }
+       unsigned long long operator++(int){
+               __sync_add_and_fetch(&_p,1);
+               return _p;
+       }
+       unsigned long long operator--(int){
+               __sync_sub_and_fetch(&_p,1);
+               return _p;
+       }
+       bool operator<(const unsigned long long _q) const{
+               return (_p < _q);
+       }
+       bool operator<=(const unsigned long long _q) const{
+               return (_p <= _q);
+       }
+       bool operator>(const unsigned long long _q) const{
+               return (_p > _q);
+       }
+       bool operator>=(const unsigned long long _q) const{
+               return (_p >= _q);
+       }
+       bool operator==(const unsigned long long _q) const{
+               return (_p == _q);
+       }
+       bool operator!=(const unsigned long long _q) const{
+               return (_p != _q);
+       }
+
+};
+
 } //namespace l7vsd
 #endif
index dd23671..d8634e9 100644 (file)
@@ -14,13 +14,13 @@ class lockfree_queue : protected boost::noncopyable {
 protected:
        struct node_type {
                Tvalue*                         value;
-               struct node_type*       next;
+               struct node_type*  volatile     next;
                node_type() : value( NULL ) , next( NULL ){}
        };
        struct mng_queue_type {
-               int                     counter;
-               node_type*              headloc;
-               node_type*              tailloc;
+               int volatile            counter;
+               node_type* volatile     headloc;
+               node_type* volatile     tailloc;
                mng_queue_type() : counter(0), headloc( NULL ), tailloc( NULL ) {}
        };
 public:
@@ -29,35 +29,30 @@ public:
        // constractor
        lockfree_queue(){
                node_type*      new_node = new node_type();
-               new_node->value = new Tvalue();
                mng_queue = new mng_queue_type();
                mng_queue->headloc = new_node;
                mng_queue->tailloc = new_node;
        }
 
-       volatile void push(const Tvalue& _v){
+       void push(const Tvalue* inptr){
                node_type *_new_node,*_tail,*_next;
                _new_node = new node_type();
-               _new_node->value = new Tvalue;
-               *_new_node->value = _v;
+               _new_node->value = const_cast<Tvalue*>( inptr );
 
-               // tracsaction st
-               while(1){
+               // transaction st
+               while(true){
                        _tail = mng_queue->tailloc;
                        _next = _tail->next;
-
-                       if(_tail == mng_queue->tailloc){
-                               if(_next == NULL){
-                                       if(cas(&_tail->next,_next,_new_node)){
-                                               break;
-                                       }
+                       if (_tail == mng_queue->tailloc){
+                               if(!_next){
+                                       if(cas(&_tail->next,_next,_new_node)) break;
                                }
                        }else{
                                cas(&mng_queue->tailloc,_tail,_next);
                        }
 
                }
-               // tracsaction ed
+               // transaction ed
 
                cas(&mng_queue->tailloc,_tail,_new_node);
                add(&mng_queue->counter);
@@ -93,7 +88,6 @@ public:
                }
                //transaction ed
 
-               delete _head_node->value;
                delete _head_node;
                sub(&mng_queue->counter);
                return;
@@ -114,4 +108,3 @@ public:
 } // namespace l7vs
 
 #endif // LOCKFREE_QUEUE_H
-