00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef AMINO_ORDERDED_LIST_H
00017 #define AMINO_ORDERDED_LIST_H
00018 #include "smr.h"
00019 #include "list.h"
00020
00021 namespace amino {
00022
00047 template<typename KeyType> class OrderedList {
00048 public:
00049 typedef __list_iterator<KeyType> iterator;
00050 typedef __list_const_iterator<KeyType> const_iterator;
00051
00052 private:
00053 template<typename T> friend class Set;
00057 atomic<NodeType<KeyType>*> head;
00061 NodeType<KeyType>* dummy;
00062
00063 SMR<NodeType<KeyType> , NHPOINTER>* mm;
00064
00065
00066 OrderedList(const OrderedList& s) {
00067 }
00068 OrderedList& operator=(const OrderedList& st) {
00069 }
00070
00071 public:
00075 OrderedList() {
00076 mm = getSMR<NodeType<KeyType> , NHPOINTER> ();
00077 dummy = new NodeType<KeyType> ();
00078 head.store(dummy->next.load(memory_order_relaxed), memory_order_relaxed);
00079 }
00080
00081
00082
00083
00084 ~OrderedList() {
00085 NodeType<KeyType>* tmp = head.load();
00086 while (tmp != NULL) {
00087 NodeType<KeyType>*tmp_keep = tmp;
00088 tmp = tmp_keep->next.load(memory_order_relaxed);
00089 delete tmp_keep;
00090 }
00091
00092 delete dummy;
00093 }
00094
00104 bool front(KeyType& ret) {
00105 NodeType<KeyType>* node = head.load();
00106 if (node != NULL) {
00107 ret = node->data;
00108 return true;
00109 } else
00110 return false;
00111 }
00112
00121 bool push_front(const KeyType& e) {
00122 return add(e, &head);
00123 }
00124
00136 bool insert(int index, const KeyType& e) {
00137 return add(e, &head);
00138 }
00139
00146 bool empty() {
00147 return head.load() == NULL;
00148 }
00149
00150 iterator begin() {
00151 return head.load(memory_order_relaxed);
00152 }
00153
00154 iterator end() {
00155 return NULL;
00156 }
00165 bool remove(const KeyType& e) {
00166 return remove(e, &head);
00167 }
00168
00179 bool remove(const KeyType& e, atomic<NodeType<KeyType>*>* start) {
00180 FindStateHolder<KeyType> holder = FindStateHolder<KeyType> ();
00181 bool result;
00182
00183 while (true) {
00184 if (!find(e, start, holder)) {
00185 result = false;
00186 break;
00187 }
00194 if (!POINTER(holder.cur)->next.compare_swap(holder.next, MARK(
00195 holder.next))) {
00196 continue;
00197 }
00205 if (holder.prev->compare_swap(holder.cur, POINTER(holder.next))) {
00206 mm->delNode(POINTER(holder.cur));
00207 } else {
00208 find(e, start, holder);
00209 }
00210 result = true;
00211 break;
00212 }
00213 retireAll();
00214 return result;
00215 }
00216
00225 bool search(const KeyType& e) {
00226 FindStateHolder<KeyType> holder = FindStateHolder<KeyType> ();
00227 bool result = find(e, &head, holder);
00228 retireAll();
00229 return result;
00230 }
00231
00242 bool search(const KeyType& e, atomic<NodeType<KeyType>*>* start) {
00243 FindStateHolder<KeyType> holder = FindStateHolder<KeyType> ();
00244 bool result = find(e, start, holder);
00245 retireAll();
00246 return result;
00247 }
00248
00249 private:
00254 void retireAll() {
00255 mm->retire(0);
00256 mm->retire(1);
00257 mm->retire(2);
00258 }
00259
00271 bool add(const KeyType& e, atomic<NodeType<KeyType>*>* start) {
00272 NodeType<KeyType>* node = new NodeType<KeyType> ();
00273 node->data = e;
00274 FindStateHolder<KeyType> holder = FindStateHolder<KeyType> ();
00275 bool result;
00276
00277 while (true) {
00278 if (find(e, start, holder)) {
00279 delete node;
00280 result = false;
00281 break;
00282 }
00283 node->next.store(POINTER(holder.cur), memory_order_relaxed);
00284
00291 if (holder.prev->compare_swap(holder.cur, POINTER(node))) {
00292 result = true;
00293 break;
00294 }
00295 }
00296
00297 retireAll();
00298 return result;
00299 }
00300
00311 NodeType<KeyType>* add_returnAddress(const KeyType& e, atomic<NodeType<
00312 KeyType>*>* start) {
00313 NodeType<KeyType>* node = new NodeType<KeyType> ();
00314 node->data = e;
00315 FindStateHolder<KeyType> holder = FindStateHolder<KeyType> ();
00316 NodeType<KeyType>* result;
00317
00318 while (true) {
00319 if (find(e, start, holder)) {
00320 delete node;
00321 result = holder.cur;
00322 break;
00323 }
00324 node->next.store(POINTER(holder.cur), memory_order_relaxed);
00325
00326
00333 if (holder.prev->compare_swap(holder.cur, POINTER(node))) {
00334 result = node;
00335 assert(result != NULL);
00336 break;
00337 }
00338 }
00339
00340 retireAll();
00341 return result;
00342 }
00343
00356 bool find(const KeyType& key, atomic<NodeType<KeyType>*>* start,
00357 FindStateHolder<KeyType>& holder) {
00358 try_again:
00359 NodeType<KeyType>* next = NULL;
00360 atomic<NodeType<KeyType>*>* prev = start;
00361
00362 NodeType<KeyType>* cur = POINTER(prev->load(memory_order_relaxed));
00363
00364 mm->employ(1, cur);
00365
00366 if (prev->load(memory_order_relaxed) != cur)
00367 goto try_again;
00368
00369 while (true) {
00370 if (NULL == cur) {
00371
00372 holder.isFound = false;
00373 holder.prev = prev;
00374 holder.cur = cur;
00375 holder.next = next;
00376 return false;
00377 }
00378
00379 NodeType<KeyType>* markedNext = cur->next.load(memory_order_relaxed);
00380 bool cmark = MARKED(markedNext);
00381 next = POINTER(markedNext);
00382
00383 mm->employ(0, next);
00384
00385 if (cur->next.load(memory_order_relaxed) != markedNext)
00386 goto try_again;
00387
00388 KeyType cKey = cur->data;
00389
00390 if (prev->load(memory_order_relaxed) != cur)
00391 goto try_again;
00392
00393 if (!cmark) {
00394 if (cKey >= key) {
00395
00396 holder.isFound = (cKey == key);
00397 holder.prev = prev;
00398 holder.cur = cur;
00399 holder.next = next;
00400 return holder.isFound;
00401 }
00402 prev = &(cur->next);
00403 mm->employ(2, cur);
00404 } else {
00405 if (prev->compare_swap(cur, next)) {
00406 mm->delNode(cur);
00407 } else {
00408 goto try_again;
00409 }
00410 }
00411
00412 cur = next;
00413 mm->employ(1, next);
00414 }
00415 }
00416
00417 };
00418
00419 }
00420
00421 #endif