00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef ACEDIA_DETAILS_H_
00031 #define ACEDIA_DETAILS_H_
00032
00033 #include "acedia_config.hpp"
00034
00035 #include "unit.hpp"
00036 #include "acedia_atomic.hpp"
00037 #include "acedia_preprocessor.hpp"
00038
00039 #include <boost/cstdint.hpp>
00040 #include <boost/thread/mutex.hpp>
00041 #include <boost/thread/condition_variable.hpp>
00042
00043 namespace acedia
00044 {
00045
00046
00047 class AnyArray;
00048
00049 namespace details
00050 {
00051
00052
00053 class ContinuationInvoker;
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 class AnyArrayProcessor
00066 {
00067
00068 public:
00069
00070 AnyArrayProcessor();
00071
00072 virtual ~AnyArrayProcessor();
00073
00074
00075
00076 virtual bool operator()(const AnyArray &arr) = 0;
00077
00078
00079
00080
00081 virtual ContinuationInvoker *matchingInvoker(const AnyArray &arr) = 0;
00082
00083 void passThrough(AnyArrayProcessor *nextProcessor);
00084
00085 void setNext(AnyArrayProcessor *nextProcessor);
00086
00087 inline bool hasNext() { return NULL != next; }
00088
00089 protected:
00090
00091 AnyArrayProcessor *next;
00092
00093 };
00094
00095 namespace container
00096 {
00097 template<typename T>
00098 T *inverseList(T *h)
00099 {
00100 if (!h) return NULL;
00101
00102 T *n = h->next;
00103 h->next = NULL;
00104 T *inversedHead = h;
00105
00106 while (NULL != (h = n))
00107 {
00108 n = h->next;
00109 h->next = inversedHead;
00110 inversedHead = h;
00111 }
00112 return inversedHead;
00113 }
00114
00115 namespace intrusive
00116 {
00117 template<typename T>
00118 class Stack
00119 {
00120 volatile T *head;
00121
00122 public:
00123
00124 inline Stack() throw() : head(NULL) { }
00125
00126 void push(T *what)
00127 {
00128 for (;;)
00129 {
00130 T *h = const_cast<T*>(head);
00131 what->next = h;
00132 if (atomic::compareAndSwap(&head, h, what)) return;
00133 }
00134 }
00135
00136 T *pop()
00137 {
00138 for (;;)
00139 {
00140 T *h = const_cast<T*>(head);
00141 if (h)
00142 {
00143 if (atomic::compareAndSwap(&head, h, h->next))
00144 {
00145 return h;
00146 }
00147
00148 }
00149 else return NULL;
00150 }
00151 }
00152
00153 };
00154
00155 template<typename T>
00156 class SingleReaderListBase
00157 {
00158
00159 protected:
00160
00161
00162 volatile T *head;
00163
00164
00165 T *inversedHead;
00166
00167
00168 T *takeFirstFromInversedHead()
00169 {
00170 if (inversedHead)
00171 {
00172 T *result = inversedHead;
00173 inversedHead = inversedHead->next;
00174 result->next = NULL;
00175 return result;
00176 }
00177 return NULL;
00178 }
00179
00180
00181 T *takeHead()
00182 {
00183 for (;;)
00184 {
00185 T *h = const_cast<T*>(head);
00186 if (h)
00187 {
00188 if (atomic::compareAndSwap(&head, h,
00189 reinterpret_cast<T*>(NULL)))
00190 {
00191
00192
00193 return h;
00194 }
00195
00196 }
00197 else return NULL;
00198 }
00199 }
00200
00201 inline SingleReaderListBase() throw() :
00202 head(NULL), inversedHead(NULL) { }
00203
00204
00205
00206
00207 void append(T *what)
00208 {
00209 T *h;
00210 do
00211 {
00212 h = const_cast<T*>(head);
00213 what->next = h;
00214 }
00215 while (!atomic::compareAndSwap(&head, h, what));
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 T *tryTakeFirst()
00229 {
00230 if (inversedHead) return takeFirstFromInversedHead();
00231 else
00232 {
00233 inversedHead = inverseList(takeHead());
00234 return takeFirstFromInversedHead();
00235 }
00236 }
00237
00238 public:
00239
00240 inline bool isEmpty()
00241 {
00242 return NULL == head && NULL == inversedHead;
00243 }
00244
00245 inline bool hasNext() { return !isEmpty(); }
00246
00247 void clear()
00248 {
00249 T *t;
00250 while (NULL != (t = tryTakeFirst()))
00251 {
00252 delete t;
00253 }
00254 }
00255
00256 };
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 template<typename T>
00280 class SingleReaderQueue : public SingleReaderListBase<T>
00281 {
00282
00283 public:
00284
00285 inline SingleReaderQueue() : SingleReaderListBase<T>() { }
00286
00287
00288
00289
00290 inline void enqueue(T *what) { append(what); }
00291
00292
00293
00294
00295
00296 void enqueueList(T *lHead, T *lTail)
00297 {
00298 T *h;
00299 do
00300 {
00301 h = const_cast<T*>(this->head);
00302 lTail->next = h;
00303 }
00304 while (!atomic::compareAndSwap(&(this->head), h,
00305 lHead));
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 inline T *tryDequeue() { return this->tryTakeFirst(); }
00319
00320 };
00321
00322 template<typename T>
00323 class SingleReaderList : public SingleReaderListBase<T>
00324 {
00325
00326 template<class C>
00327 friend class SingleReaderListIterator;
00328
00329 public:
00330
00331 T *front()
00332 {
00333 if (!(this->inversedHead))
00334 {
00335 this->inversedHead = inverseList(this->takeHead());
00336 }
00337 return this->inversedHead;
00338 }
00339
00340 inline SingleReaderList() : SingleReaderListBase<T>() { }
00341
00342 inline T *tryTakeFirst()
00343 {
00344 return SingleReaderListBase<T>::tryTakeFirst();
00345 }
00346
00347 inline void append(T *what)
00348 {
00349 SingleReaderListBase<T>::append(what);
00350 }
00351
00352 };
00353
00354 template<class T>
00355 class SingleReaderListIterator
00356 {
00357
00358 public:
00359
00360 typedef SingleReaderList<T> List;
00361
00362 private:
00363
00364 List &list;
00365 T *item;
00366 T *prevItem;
00367 bool initialized;
00368
00369 public:
00370
00371 inline SingleReaderListIterator(List &parent) throw() :
00372 list(parent), item(NULL), prevItem(NULL),
00373 initialized(false) { }
00374
00375 inline T *next()
00376 {
00377 if (!initialized)
00378 {
00379 item = list.front();
00380 initialized = true;
00381 }
00382 else
00383 {
00384 if (item)
00385 {
00386 prevItem = item;
00387 if (!item->next)
00388 {
00389 item->next = inverseList(list.takeHead());
00390 }
00391 item = item->next;
00392 }
00393 }
00394 return item;
00395 }
00396
00397
00398
00399 T *erase()
00400 {
00401 if (!item) return NULL;
00402 T *i = item;
00403 if (!prevItem)
00404 {
00405
00406 list.inversedHead = item->next;
00407 }
00408 else
00409 {
00410 prevItem->next = item->next;
00411 item = prevItem->next;
00412 }
00413 return i;
00414 }
00415
00416 inline T &operator*() { return *item; }
00417
00418 inline bool hasNext()
00419 {
00420 if (initialized)
00421 {
00422 if (!item) return false;
00423 if (!item->next)
00424 {
00425 item->next = inverseList(list.takeHead());
00426 return NULL != item->next;
00427 }
00428 return true;
00429 } else {
00430 if (!list.inversedHead)
00431 {
00432 list.inversedHead =
00433 inverseList(list.takeHead());
00434 }
00435 return NULL != list.inversedHead;
00436 }
00437 }
00438
00439 inline bool isValid() { return NULL != item; }
00440
00441 };
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452 template<typename T>
00453 class Queue
00454 {
00455
00456 SingleReaderQueue<T> impl;
00457 boost::condition_variable cv;
00458 boost::mutex mm;
00459
00460 public:
00461
00462
00463 void enqueue(T *what)
00464 {
00465 impl.enqueue(what);
00466 ACEDIA_MEMORY_BARRIER();
00467 cv.notify_all();
00468 }
00469
00470
00471 T *dequeue()
00472 {
00473 boost::unique_lock<boost::mutex> lock(mm);
00474
00475
00476 T *t = impl.tryDequeue();
00477 while (!t)
00478 {
00479 cv.wait(lock);
00480 t = impl.tryDequeue();
00481 }
00482 return t;
00483 }
00484
00485
00486 T *tryDequeue()
00487 {
00488 boost::unique_lock<boost::mutex> lock(mm);
00489 return impl.tryDequeue();
00490 }
00491
00492
00493 T *tryDequeue(boost::int64_t msTimeout)
00494 {
00495 boost::system_time timeout = boost::get_system_time();
00496 timeout += boost::posix_time::milliseconds(msTimeout);
00497 boost::unique_lock<boost::mutex> lock(mm);
00498 T *t = impl.tryDequeue();
00499 while (!t)
00500 {
00501 if (!cv.timed_wait(lock, timeout)) return NULL;
00502 t = impl.tryDequeue();
00503 }
00504 return t;
00505 }
00506
00507 };
00508
00509 }
00510
00511
00512
00513
00514 template<typename T>
00515 struct Node
00516 {
00517 T value;
00518 Node *next;
00519 inline Node(const T &t) : value(t), next(NULL) { }
00520 private:
00521
00522 Node(const Node &other);
00523 Node &operator=(const Node &other);
00524 };
00525
00526 template<typename T>
00527 class SingleReaderQueue
00528 {
00529
00530 private:
00531
00532 intrusive::SingleReaderQueue< Node<T> > impl;
00533
00534 public:
00535
00536 inline void enqueue(const T &what)
00537 {
00538 impl.enqueue(new Node<T>(what));
00539 }
00540
00541 inline void enqueueList(Node<T> *lHead, Node<T> *lTail)
00542 {
00543 impl.enqueueList(lHead, lTail);
00544 }
00545
00546 bool tryDequeue(T &storage)
00547 {
00548 Node<T> *n = impl.tryDequeue();
00549 if (n)
00550 {
00551 storage = n->value;
00552 delete n;
00553 return true;
00554 }
00555 return false;
00556 }
00557
00558 inline Node<T> *tryDequeueNode() { return impl.tryDequeue(); }
00559
00560 inline bool isEmpty() { return impl.isEmpty(); }
00561
00562 inline bool hasNext() { return !isEmpty(); }
00563
00564 };
00565
00566 template<typename T>
00567 class Stack
00568 {
00569
00570 intrusive::Stack< Node<T> > impl;
00571
00572 public:
00573
00574 void push(const T &what)
00575 {
00576 impl.push(new Node<T>(what));
00577 }
00578
00579 bool pop(T &storage)
00580 {
00581 Node<T> *n = impl.pop();
00582 if (n)
00583 {
00584 storage = n->value;
00585 delete n;
00586 return true;
00587 }
00588 return false;
00589 }
00590
00591 };
00592
00593 template<typename T>
00594 class SingleReaderList
00595 {
00596
00597 template<typename C>
00598 friend class SingleReaderListIterator;
00599
00600 public:
00601
00602 typedef Node<T> ListNode;
00603
00604 private:
00605
00606 intrusive::SingleReaderList<ListNode> impl;
00607
00608 public:
00609
00610 inline void append(const T &what)
00611 {
00612 impl.append(new ListNode(what));
00613 }
00614
00615 bool tryTakeFirst(T &storage)
00616 {
00617 ListNode *n = impl.tryTakeFirst();
00618 if (n)
00619 {
00620 storage = n->value;
00621 delete n;
00622 return true;
00623 }
00624 return false;
00625 }
00626
00627 inline bool isEmpty() { return impl.isEmpty(); }
00628
00629 inline bool hasNext() { return impl.hasNext(); }
00630
00631 inline void clear() { impl.clear(); }
00632
00633 };
00634
00635 template<typename T>
00636 class SingleReaderListIterator
00637 {
00638
00639 public:
00640
00641 typedef Node<T> ListNode;
00642 typedef SingleReaderList<T> List;
00643
00644 private:
00645
00646 intrusive::SingleReaderListIterator<ListNode> i;
00647
00648 public:
00649
00650 inline SingleReaderListIterator(List &parent) :
00651 i(parent.impl) { }
00652
00653 inline SingleReaderListIterator(List &parent, ListNode *n) :
00654 i(parent.impl, n) { }
00655
00656 inline void operator++() { ++i; }
00657
00658 inline T &operator*() { return (*i).value; }
00659
00660 inline bool isValid() { return i.isValid(); }
00661
00662 inline T &next() { return (i.next())->value; }
00663
00664 inline bool hasNext() { return i.hasNext(); }
00665
00666 inline void erase()
00667 {
00668 ListNode *n = i.erase();
00669 if (n) delete n;
00670 }
00671
00672 };
00673
00674
00675
00676
00677 template<typename T>
00678 class Queue
00679 {
00680
00681 intrusive::Queue< Node<T> > impl;
00682
00683 bool setStorageIfN(T &storage, Node<T> *n)
00684 {
00685 if (n)
00686 {
00687 storage = n->value;
00688 delete n;
00689 return true;
00690 }
00691 return false;
00692 }
00693
00694 public:
00695
00696
00697 inline void enqueue(const T &what)
00698 {
00699 impl.enqueue(new Node<T>(what));
00700 }
00701
00702
00703 inline void dequeue(T &storage)
00704 {
00705 (void) setStorageIfN(storage, impl.dequeue());
00706 }
00707
00708
00709 inline bool tryDequeue(T &storage)
00710 {
00711 return setStorageIfN(storage, impl.tryDequeue());
00712 }
00713
00714
00715 inline bool tryDequeue(T &storage, boost::int64_t msTimeout)
00716 {
00717 return setStorageIfN(storage, impl.tryDequeue(msTimeout));
00718 }
00719
00720 };
00721
00722 }
00723
00724 }
00725
00726 }
00727
00728 #endif