00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #ifndef AMINO_BLOCKING_DEQUE_H
00018 #define AMINO_BLOCKING_DEQUE_H
00019 
00020 #include <stdexcept>
00021 
00022 #include <amino/deque.h>
00023 #include <amino/mutex.h>
00024 #include <amino/lock.h>
00025 #include <amino/condition.h>
00026 
00027 namespace amino {
00028 
00029 template<typename T>
00030 class BlockingDeque: public LockFreeDeque<T> {
00031 private:
00032         mutex f_mutex;
00033         condition_variable f_push_notifier;
00034 public:
00035         virtual void pushRight(T data) {
00036                 unique_lock<mutex> llock(f_mutex);
00037                 LockFreeDeque<T>::pushRight(data);
00038                 f_push_notifier.notify_all();
00039                 llock.unlock();
00040         }
00041 
00042         virtual void pushLeft(T data) {
00043                 unique_lock<mutex> llock(f_mutex);
00044                 LockFreeDeque<T>::pushLeft(data);
00045                 f_push_notifier.notify_all();
00046                 llock.unlock();
00047         }
00048 
00049         virtual bool takeRight(T& ret) {
00050                 while (true) {
00051                         if (this->popRight(ret))
00052                                 return true;
00053                         
00054 
00055                         unique_lock<mutex> llock(f_mutex);
00056                         if (this->empty())
00057                                 f_push_notifier.wait(llock);
00058                         llock.unlock();
00059                 }
00060         }
00061 
00062         virtual bool takeLeft(T& ret) {
00063                 while (true) {
00064                         if (this->popLeft(ret))
00065                                 return true;
00066                         
00067 
00068                         unique_lock<mutex> llock(f_mutex);
00069                         if (this->empty())
00070                                 f_push_notifier.wait(llock);
00071                         llock.unlock();
00072                 }
00073         }
00074 };
00075 }
00076 
00077 #endif