OSDN Git Service

lockfree_hashmap.h lockfree_queue.hの修正
authorkanda <kanda@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Thu, 18 Jun 2009 04:28:53 +0000 (04:28 +0000)
committerkanda <kanda@1ed66053-1c2d-0410-8867-f7571e6e31d3>
Thu, 18 Jun 2009 04:28:53 +0000 (04:28 +0000)
likely/unlikelyの追加

git-svn-id: http://10.144.169.20/repos/um/branches/l7vsd-3.x-shamshel-refine@7940 1ed66053-1c2d-0410-8867-f7571e6e31d3

include/lockfree_hashmap.h
include/lockfree_queue.h

index 7089d62..81ddd22 100644 (file)
@@ -32,7 +32,7 @@ public:
        lockfree_hashmap( size_t num = 65535, Hash inhasher = boost::hash< Tkey* >() ) : 
                element_num( num ) {
                hashmap = new container[num];
-               hasher = inhasher;
+               hasher = inhasher;      
                __sync_lock_test_and_set( &all_ctr, 0 );
        }
 
@@ -48,7 +48,7 @@ public:
                do{
                        if( likely( !find_bucket( hashvalue, key, pre_key, true ) ) ) return;
                }
-               while( !__sync_bool_compare_and_swap( &hashmap[hashvalue].key, pre_key, key ) );
+               while( unlikely( !__sync_bool_compare_and_swap( &hashmap[hashvalue].key, pre_key, key ) ) );
                hashmap[hashvalue].value        = const_cast< Tvalue* >( value );
                __sync_add_and_fetch( &all_ctr, 1 );
        }
@@ -70,15 +70,15 @@ public:
                do{
                        if( unlikely( !find_bucket( hashvalue, key, pre_key, false ) ) ) return;
                }
-               while( !__sync_bool_compare_and_swap( &hashmap[hashvalue].key, pre_key, NULL ) );
+               while( unlikely( !__sync_bool_compare_and_swap( &hashmap[hashvalue].key, pre_key, NULL ) ) );
                hashmap[hashvalue].value        = NULL;
                __sync_sub_and_fetch( &all_ctr, 1 );
        }
 
        //poper
        void    pop( Tkey*& key, Tvalue*& value ){
-               for( size_t i = 0 ; i < element_num; ++i ){
-                       if( hashmap[i].key ){
+               for( size_t i = 0 ; likely( i < element_num ) ; ++i ){
+                       if( unlikely( hashmap[i].key ) ){
                                key     = hashmap[i].key;
                                value   = hashmap[i].value;
                                hashmap[i].key          = NULL;
@@ -104,8 +104,8 @@ public:
 
        // functor
        void    do_all( boost::function< void( Tvalue* ) >      func ){
-               for( size_t     i = 0; i < element_num; ++i ){
-                       if( hashmap[i].key != NULL ){
+               for( size_t     i = 0; likely( i < element_num ) ; ++i ){
+                       if( unlikely( hashmap[i].key ) ){
                                func( hashmap[i].value );
                        }
                }
@@ -115,19 +115,19 @@ private:
        //bucket finder
        bool    find_bucket( size_t& hash_value, const Tkey*& key, Tkey*& pre_key, bool insert ){
                for(;;){
-                       if( hashmap[hash_value].key == NULL ){
+                       if( unlikely (hashmap[hash_value].key == NULL ) ){
                                if( insert ) return true;
                                if( unlikely( hashmap[hash_value].rehash ) )
                                        hash_value = re_hashvalue( hash_value );
                                else
                                        return( false );
                        }
-                       else if( hashmap[hash_value].key == key ) {
+                       else if( likely( hashmap[hash_value].key == key ) ){
                                pre_key = hashmap[hash_value].key;
                                return true;
                        }
                        else{
-                               if( insert ){
+                               if( unlikely( insert ) ){
                                        hashmap[hash_value].rehash = true;
                                        hash_value = re_hashvalue( hash_value );
                                }
@@ -138,9 +138,11 @@ private:
                        }
                }
        }
+       //get array number by hasher
        size_t  get_hashvalue(const Tkey* key ){
                return hasher( const_cast<Tkey*>(key) ) % element_num;
        }
+       //reget array number case of collision(method chain)
        size_t  re_hashvalue( size_t& key ){
                return ((key+1) % element_num);
        }
index 96b8fc9..a326682 100644 (file)
@@ -32,6 +32,7 @@ public:
                delete [] node;
        }
 
+       //pusher
        bool push(const Tvalue* value){
                size_t tail,nexttail;
                //transaction st
@@ -39,8 +40,9 @@ public:
                        start:
                        tail = tailloc;
                        nexttail = get_num_next(tail);
-                       if ( unlikely( node[tail].value ) ) {
+                       if ( unlikely( node[tail].value ) ){
                                //return false;
+                               //do spin case of full queue
                                goto start;
                        }
                        if ( likely( __sync_bool_compare_and_swap(&tailloc,tail,nexttail) ) ) break;
@@ -51,6 +53,7 @@ public:
                return true;
        }
 
+       //poper
        Tvalue* pop() {
                size_t head,nexthead;
                Tvalue* rtnvalue;
@@ -58,13 +61,13 @@ public:
                while(true){
                        head = headloc;
                        nexthead = get_num_next(head);
-                       if( unlikely( !(node[head].value) ) ) {
-                               if( unlikely( headloc==tailloc ) ) {
+                       if( unlikely( !(node[head].value) ) ){
+                               if( unlikely( headloc==tailloc ) ){
                                        //false
                                        return NULL;
                                }
                        }else{
-                               if( likely( __sync_bool_compare_and_swap(&headloc,head,nexthead) ) ) {
+                               if( likely( __sync_bool_compare_and_swap(&headloc,head,nexthead) ) ){
                                        rtnvalue = node[head].value;
                                        break;
                                }
@@ -76,15 +79,14 @@ public:
                return rtnvalue;
        }
 
-       bool empty() const{
-               if( counter ) return false;
-               return true;
-       }
-       size_t size() const{
-               return counter;
-       }
+       //size
+       size_t size() const{ return counter; }
+
+       //empty
+       bool empty() const{ return !counter; }
 
 private:
+       //get next value head and tail cyclic over elemental number
        size_t get_num_next( const size_t num ){
                if( unlikely( num >= element_num ) ){
                        return 0;