-#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;
}
};
+//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;
}
};
+//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
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:
// 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);
}
//transaction ed
- delete _head_node->value;
delete _head_node;
sub(&mng_queue->counter);
return;
} // namespace l7vs
#endif // LOCKFREE_QUEUE_H
-