From 05fe3d047f4a969591fe47c55735565186415760 Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Fri, 6 Feb 2015 00:10:33 -0800 Subject: [PATCH] fixed wrong pointer usage --- concurrent-hashmap/hashmap.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/concurrent-hashmap/hashmap.h b/concurrent-hashmap/hashmap.h index 6ea5e76..8e9a06d 100644 --- a/concurrent-hashmap/hashmap.h +++ b/concurrent-hashmap/hashmap.h @@ -110,7 +110,7 @@ class HashMap { static const int SEGMENT_MASK = CONCURRENCY_LEVEL - 1; - Segment segments[CONCURRENCY_LEVEL]; + Segment *segments[CONCURRENCY_LEVEL]; static const int DEFAULT_INITIAL_CAPACITY = 16; @@ -123,6 +123,9 @@ class HashMap { for (int i = 0; i < capacity; i++) { atomic_init(&table[i], NULL); } + for (int i = 0; i < CONCURRENCY_LEVEL; i++) { + segments[i] = new Segment; + } } int hashKey(Key *x) { @@ -162,13 +165,12 @@ class HashMap { } // Recheck under synch if key apparently not there or interference - Segment seg = segments[hash & SEGMENT_MASK]; - seg.lock(); // Critical region begins + Segment *seg = segments[hash & SEGMENT_MASK]; + seg->lock(); // Critical region begins // Not considering resize now, so ignore the reload of table... - atomic *newFirst = &tab[index]; // Synchronized by locking, no need to be load acquire - Entry *newFirstPtr = newFirst->load(relaxed); + Entry *newFirstPtr = first->load(relaxed); if (e != NULL || firstPtr != newFirstPtr) { e = newFirstPtr; while (e != NULL) { @@ -180,7 +182,7 @@ class HashMap { e = e->next.load(relaxed); } } - seg.unlock(); // Critical region ends + seg->unlock(); // Critical region ends return NULL; } @@ -189,10 +191,10 @@ class HashMap { //ASSERT (value != NULL); int hash = hashKey(key); - Segment seg = segments[hash & SEGMENT_MASK]; + Segment *seg = segments[hash & SEGMENT_MASK]; atomic *tab; - seg.lock(); // Critical region begins + seg->lock(); // Critical region begins tab = table; int index = hash & (capacity - 1); @@ -208,7 +210,7 @@ class HashMap { // FIXME: This could be an acquire?? res = e->value.load(acquire); e->value.store(value, seq_cst); - seg.unlock(); // Don't forget to unlock before return + seg->unlock(); // Don't forget to unlock before return return res; } } @@ -216,8 +218,8 @@ class HashMap { // Add to front of list Entry *newEntry = new Entry(hash, key, value, firstPtr); // Publish the newEntry to others - tab[index].store(newEntry, release); - seg.unlock(); // Critical region ends + first->store(newEntry, release); + seg->unlock(); // Critical region ends return NULL; } }; -- 2.34.1