10 #define MODEL_ASSERT assert
12 #include <model-assert.h>
17 #define relaxed memory_order_relaxed
18 #define release memory_order_release
19 #define acquire memory_order_acquire
20 #define acq_rel memory_order_acq_rel
21 #define seq_cst memory_order_seq_cst
26 For the sake of simplicity, we do not use template but some toy structs to
27 represent the Key and Value.
30 // Probably represent the coordinate (x, y, z)
36 return x + 31 * y + 31 * 31 * z;
39 bool equals(Key *other) {
42 return x == other->x && y == other->y && z == other->z;
45 Key(int x, int y, int z) :
55 // Probably represent the speed vector (vX, vY, vZ)
60 Value(int vX, int vY, int vZ) :
76 Entry(int h, Key *k, Value *v, Entry *n) {
79 this->value.store(v, relaxed);
80 this->next.store(n, relaxed);
104 atomic<Entry*> *table;
109 static const int CONCURRENCY_LEVEL = 16;
111 static const int SEGMENT_MASK = CONCURRENCY_LEVEL - 1;
113 Segment segments[CONCURRENCY_LEVEL];
115 static const int DEFAULT_INITIAL_CAPACITY = 16;
117 // Not gonna consider resize now...
121 this->capacity = DEFAULT_INITIAL_CAPACITY;
122 this->table = new atomic<Entry*>[capacity];
123 for (int i = 0; i < capacity; i++) {
124 atomic_init(&table[i], NULL);
128 int hashKey(Key *x) {
129 int h = x->hashCode();
130 // Use logical right shift
131 unsigned int tmp = (unsigned int) h;
132 return ((h << 7) - h + (tmp >> 9) + (tmp >> 17));
135 bool eq(Key *x, Key *y) {
136 return x == y || x->equals(y);
139 Value* get(Key *key) {
140 int hash = hashKey(key); // throws null pointer exception if key null
142 // Try first without locking...
143 atomic<Entry*> *tab = table;
144 int index = hash & (capacity - 1);
145 atomic<Entry*> *first = &tab[index];
149 // Should be a load acquire
150 Entry *firstPtr = first->load(acquire);
153 if (e->hash == hash && eq(key, e->key)) {
154 res = e->value.load(seq_cst);
159 // Loading the next entry
160 e = e->next.load(acquire);
164 // Recheck under synch if key apparently not there or interference
165 Segment seg = segments[hash & SEGMENT_MASK];
166 seg.lock(); // Critical region begins
167 // Not considering resize now, so ignore the reload of table...
168 atomic<Entry*> *newFirst = &tab[index];
170 // Synchronized by locking, no need to be load acquire
171 Entry *newFirstPtr = newFirst->load(relaxed);
172 if (e != NULL || firstPtr != newFirstPtr) {
175 if (e->hash == hash && eq(key, e->key)) {
176 res = e->value.load(seq_cst);
179 // Synchronized by locking
180 e = e->next.load(relaxed);
183 seg.unlock(); // Critical region ends
187 Value* put(Key *key, Value *value) {
188 // Don't allow NULL value
189 //ASSERT (value != NULL);
191 int hash = hashKey(key);
192 Segment seg = segments[hash & SEGMENT_MASK];
195 seg.lock(); // Critical region begins
197 int index = hash & (capacity - 1);
199 atomic<Entry*> *first = &tab[index];
203 // The written of the entry is synchronized by locking
204 Entry *firstPtr = first->load(relaxed);
207 if (e->hash == hash && eq(key, e->key)) {
208 // FIXME: This could be an acquire??
209 res = e->value.load(acquire);
210 e->value.store(value, seq_cst);
211 seg.unlock(); // Don't forget to unlock before return
216 // Add to front of list
217 Entry *newEntry = new Entry(hash, key, value, firstPtr);
218 // Publish the newEntry to others
219 tab[index].store(newEntry, release);
220 seg.unlock(); // Critical region ends