71 #ifndef CGU_ASYNC_QUEUE_H
72 #define CGU_ASYNC_QUEUE_H
83 #ifdef CGU_USE_SCHED_YIELD
99 virtual const char*
what()
const throw() {
return "AsyncQueuePopError: popping from empty AsyncQueue object\n";}
143 template <
class T,
class Container = std::list<T> >
class AsyncQueue {
149 std::queue<T, Container> q;
163 #ifdef CGU_USE_SCHED_YIELD
285 if (
this != &other) {
286 lock2(mutex, other.mutex);
317 lock2(mutex, rhs.mutex);
320 std::queue<T, Container> temp(rhs.q);
430 std::queue<T, Container> q;
445 #ifdef CGU_USE_SCHED_YIELD
529 while (q.empty()) cond.
wait(mutex);
643 if (
this != &other) {
644 lock2(mutex, other.mutex);
649 if (!other.q.empty()) other.cond.
broadcast();
688 lock2(mutex, rhs.mutex);
691 std::queue<T, Container> temp(rhs.q);
744 q((Thread::Mutex::Lock(rhs.mutex), rhs.q)) {}
783 template <
class T,
class Container>
812 template <
class T,
class Container>
818 #if defined(CGU_USE_INHERITABLE_QUEUE) && !defined(DOXYGEN_PARSING)
826 template <
class T,
class Allocator>
827 class AsyncQueue<T, std::list<T, Allocator> > {
829 typedef std::list<T, Allocator> Container;
830 typedef typename Container::value_type
value_type;
831 typedef typename Container::size_type
size_type;
839 class Q:
public std::queue<T, Container> {
841 void splice_end(Container& lst) {
842 this->c.splice(this->c.end(), lst);
845 mutable Thread::Mutex mutex;
848 void lock2(Thread::Mutex& m1, Thread::Mutex& m2) {
856 #ifdef CGU_USE_SCHED_YIELD
868 Thread::Mutex::Lock lock(mutex);
873 Thread::Mutex::Lock lock(mutex);
874 if (q.empty())
throw AsyncQueuePopError();
880 Thread::Mutex::Lock lock(mutex);
881 if (q.empty())
throw AsyncQueuePopError();
886 Thread::Mutex::Lock lock(mutex);
891 Thread::Mutex::Lock lock(mutex);
896 if (
this != &other) {
897 lock2(mutex, other.mutex);
906 lock2(mutex, rhs.mutex);
920 Thread::Mutex::Lock lock(mutex);
932 template <
class T,
class Allocator>
933 class AsyncQueueDispatch<T, std::list<T, Allocator> > {
935 typedef std::list<T, Allocator> Container;
936 typedef typename Container::value_type
value_type;
937 typedef typename Container::size_type
size_type;
945 class Q:
public std::queue<T, Container> {
947 void splice_end(Container& lst) {
948 this->c.splice(this->c.end(), lst);
951 mutable Thread::Mutex mutex;
955 void lock2(Thread::Mutex& m1, Thread::Mutex& m2) {
963 #ifdef CGU_USE_SCHED_YIELD
975 Thread::Mutex::Lock lock(mutex);
981 Thread::Mutex::Lock lock(mutex);
982 if (q.empty())
throw AsyncQueuePopError();
988 Thread::Mutex::Lock lock(mutex);
989 while (q.empty()) cond.wait(mutex);
990 Thread::CancelBlock b;
998 Thread::Mutex::Lock lock(mutex);
1000 if (cond.timed_wait(mutex, ts))
return true;
1002 Thread::CancelBlock b;
1009 Thread::Mutex::Lock lock(mutex);
1010 if (q.empty())
throw AsyncQueuePopError();
1014 bool empty()
const {
1015 Thread::Mutex::Lock lock(mutex);
1020 Thread::Mutex::Lock lock(mutex);
1025 if (
this != &other) {
1026 lock2(mutex, other.mutex);
1030 if (!q.empty()) cond.broadcast();
1031 if (!other.q.empty()) other.cond.broadcast();
1037 lock2(mutex, rhs.mutex);
1042 if (!q.empty()) cond.broadcast();
1050 q((Thread::Mutex::Lock(rhs.mutex), rhs.q)) {}
1053 Thread::Mutex::Lock lock(mutex);
1059 #endif // CGU_USE_INHERITABLE_QUEUE