71 #ifndef CGU_ASYNC_QUEUE_H 72 #define CGU_ASYNC_QUEUE_H 87 #ifdef CGU_USE_SCHED_YIELD 103 virtual const char*
what()
const throw() {
return "AsyncQueuePopError: popping from empty AsyncQueue object\n";}
147 template <
class T,
class Container = std::list<T> >
class AsyncQueue {
155 std::queue<T, Container> q;
169 #ifdef CGU_USE_SCHED_YIELD 219 q.push(std::move(obj));
245 template<
class... Args>
248 q.emplace(std::forward<Args>(args)...);
317 obj = std::move(q.front());
442 if (
this != &other) {
443 lock2(mutex, other.mutex);
474 lock2(mutex, rhs.mutex);
477 std::queue<T, Container> temp{rhs.q};
508 q = std::move(rhs.q);
650 std::queue<T, Container> q;
665 #ifdef CGU_USE_SCHED_YIELD 716 q.push(std::move(obj));
743 template<
class... Args>
746 q.emplace(std::forward<Args>(args)...);
816 obj = std::move(q.front());
903 while (q.empty()) cond.
wait(mutex);
948 while (q.empty()) cond.
wait(mutex);
950 obj = std::move(q.front());
1107 obj = std::move(q.front());
1247 if (
this != &other) {
1248 lock2(mutex, other.mutex);
1253 if (!other.q.empty()) other.cond.
broadcast();
1292 lock2(mutex, rhs.mutex);
1295 std::queue<T, Container> temp{rhs.q};
1334 q = std::move(rhs.q);
1431 q((Thread::Mutex::Lock(rhs.mutex), rhs.q)) {}
1470 template <
class T,
class Container>
1499 template <
class T,
class Container>
1505 #if defined(CGU_USE_INHERITABLE_QUEUE) && !defined(DOXYGEN_PARSING) 1514 template <
class T,
class Allocator>
1515 class AsyncQueue<T,
std::list<T, Allocator> > {
1517 typedef std::list<T, Allocator> Container;
1518 typedef typename Container::value_type
value_type;
1519 typedef typename Container::size_type
size_type;
1527 class Q:
public std::queue<T, Container> {
1529 void splice_end(Container&& lst) {
1530 this->c.splice(this->c.end(), std::move(lst));
1532 void unsplice_beginning(Container& lst) {
1533 lst.splice(lst.begin(), this->c, this->c.begin());
1536 mutable Thread::Mutex mutex;
1539 void lock2(Thread::Mutex& m1, Thread::Mutex& m2) {
1542 if (!m2.trylock()) {
1547 #ifdef CGU_USE_SCHED_YIELD 1557 Container temp{obj};
1558 Thread::Mutex::Lock lock{mutex};
1560 q.splice_end(std::move(temp));
1570 temp.push_back(std::move(obj));
1571 Thread::Mutex::Lock lock{mutex};
1573 q.splice_end(std::move(temp));
1576 template<
class... Args>
1577 void emplace(Args&&... args) {
1579 temp.emplace_back(std::forward<Args>(args)...);
1580 Thread::Mutex::Lock lock{mutex};
1582 q.splice_end(std::move(temp));
1586 Thread::Mutex::Lock lock{mutex};
1587 if (q.empty())
throw AsyncQueuePopError();
1593 Thread::Mutex::Lock lock{mutex};
1594 if (q.empty())
throw AsyncQueuePopError();
1595 obj = std::move(q.front());
1605 Thread::Mutex::Lock lock{mutex};
1606 if (q.empty())
throw AsyncQueuePopError();
1608 q.unsplice_beginning(temp);
1610 obj = std::move(temp.front());
1614 Thread::Mutex::Lock lock{mutex};
1615 if (q.empty())
throw AsyncQueuePopError();
1619 bool empty()
const {
1620 Thread::Mutex::Lock lock{mutex};
1625 Thread::Mutex::Lock lock{mutex};
1630 if (
this != &other) {
1631 lock2(mutex, other.mutex);
1640 lock2(mutex, rhs.mutex);
1650 Thread::Mutex::Lock lock{mutex};
1651 q = std::move(rhs.q);
1662 Thread::Mutex::Lock lock{mutex};
1675 template <
class T,
class Allocator>
1676 class AsyncQueueDispatch<T,
std::list<T, Allocator> > {
1678 typedef std::list<T, Allocator> Container;
1679 typedef typename Container::value_type
value_type;
1680 typedef typename Container::size_type
size_type;
1688 class Q:
public std::queue<T, Container> {
1690 void splice_end(Container&& lst) {
1691 this->c.splice(this->c.end(), std::move(lst));
1693 void unsplice_beginning(Container& lst) {
1694 lst.splice(lst.begin(), this->c, this->c.begin());
1697 mutable Thread::Mutex mutex;
1701 void lock2(Thread::Mutex& m1, Thread::Mutex& m2) {
1704 if (!m2.trylock()) {
1709 #ifdef CGU_USE_SCHED_YIELD 1719 Container temp{obj};
1720 Thread::Mutex::Lock lock{mutex};
1722 q.splice_end(std::move(temp));
1733 temp.push_back(std::move(obj));
1734 Thread::Mutex::Lock lock{mutex};
1736 q.splice_end(std::move(temp));
1740 template<
class... Args>
1741 void emplace(Args&&... args) {
1743 temp.emplace_back(std::forward<Args>(args)...);
1744 Thread::Mutex::Lock lock{mutex};
1746 q.splice_end(std::move(temp));
1751 Thread::Mutex::Lock lock{mutex};
1752 if (q.empty())
throw AsyncQueuePopError();
1758 Thread::Mutex::Lock lock{mutex};
1759 if (q.empty())
throw AsyncQueuePopError();
1760 obj = std::move(q.front());
1770 Thread::Mutex::Lock lock{mutex};
1771 if (q.empty())
throw AsyncQueuePopError();
1773 q.unsplice_beginning(temp);
1775 obj = std::move(temp.front());
1779 Thread::Mutex::Lock lock{mutex};
1780 while (q.empty()) cond.wait(mutex);
1781 Thread::CancelBlock b;
1787 Thread::Mutex::Lock lock{mutex};
1788 while (q.empty()) cond.wait(mutex);
1789 Thread::CancelBlock b;
1790 obj = std::move(q.front());
1797 bool cancelstate_restored =
false;
1799 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
1804 pthread_setcancelstate(old_state, &ignore);
1805 cancelstate_restored =
true;
1806 Thread::Mutex::TrackLock lock{mutex};
1807 while (q.empty()) cond.wait(mutex);
1808 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &ignore);
1809 cancelstate_restored =
false;
1811 q.unsplice_beginning(temp);
1813 obj = std::move(temp.front());
1814 pthread_setcancelstate(old_state, &ignore);
1824 if (!cancelstate_restored) {
1825 pthread_setcancelstate(old_state, &ignore);
1834 Thread::Mutex::Lock lock{mutex};
1836 if (cond.timed_wait(mutex, ts))
return true;
1838 Thread::CancelBlock b;
1847 Thread::Mutex::Lock lock{mutex};
1849 if (cond.timed_wait(mutex, ts))
return true;
1851 Thread::CancelBlock b;
1852 obj = std::move(q.front());
1862 bool cancelstate_restored =
false;
1864 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
1869 pthread_setcancelstate(old_state, &ignore);
1870 cancelstate_restored =
true;
1871 Thread::Mutex::TrackLock lock{mutex};
1873 if (cond.timed_wait(mutex, ts))
return true;
1875 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &ignore);
1876 cancelstate_restored =
false;
1878 q.unsplice_beginning(temp);
1880 obj = std::move(temp.front());
1881 pthread_setcancelstate(old_state, &ignore);
1892 if (!cancelstate_restored) {
1893 pthread_setcancelstate(old_state, &ignore);
1900 Thread::Mutex::Lock lock{mutex};
1901 if (q.empty())
throw AsyncQueuePopError();
1905 bool empty()
const {
1906 Thread::Mutex::Lock lock{mutex};
1911 Thread::Mutex::Lock lock{mutex};
1916 if (
this != &other) {
1917 lock2(mutex, other.mutex);
1921 if (!q.empty()) cond.broadcast();
1922 if (!other.q.empty()) other.cond.broadcast();
1928 lock2(mutex, rhs.mutex);
1933 if (!q.empty()) cond.broadcast();
1939 Thread::Mutex::Lock lock{mutex};
1940 q = std::move(rhs.q);
1941 if (!q.empty()) cond.broadcast();
1950 q((Thread::Mutex::Lock(rhs.mutex), rhs.q)) {}
1953 Thread::Mutex::Lock lock{mutex};
1959 #endif // CGU_USE_INHERITABLE_QUEUE void emplace(Args &&... args)
Definition: async_queue.h:744
int broadcast()
Definition: mutex.h:483
void push(value_type &&obj)
Definition: async_queue.h:217
void pop(value_type &obj)
Definition: async_queue.h:771
void pop()
Definition: async_queue.h:391
virtual const char * what() const
Definition: async_queue.h:103
void swap(Cgu::AsyncQueue< T, Container > &q1, Cgu::AsyncQueue< T, Container > &q2)
Definition: async_queue.h:1471
~AsyncQueueDispatch()
Definition: async_queue.h:1440
Container::size_type size_type
Definition: async_queue.h:150
int signal()
Definition: mutex.h:472
void pop_dispatch(value_type &obj)
Definition: async_queue.h:901
void move_pop(value_type &obj)
Definition: async_queue.h:314
A wrapper class for pthread condition variables.
Definition: mutex.h:449
AsyncQueue(const AsyncQueue &rhs)
Definition: async_queue.h:584
AsyncQueueDispatch()=default
An exception thrown if calling pop() on a AsyncQueue or AsyncQueueDispatch object fails because the q...
Definition: async_queue.h:102
void move_pop_basic(value_type &obj)
Definition: async_queue.h:874
A thread-safe asynchronous queue.
Definition: async_queue.h:147
A thread-safe asynchronous queue with a blocking pop() method.
Definition: async_queue.h:642
void pop()
Definition: async_queue.h:1191
static void get_abs_time(timespec &ts, unsigned int millisec)
void push(value_type &&obj)
Definition: async_queue.h:714
A class enabling the cancellation state of a thread to be controlled.
Definition: thread.h:686
bool move_pop_timed_dispatch(value_type &obj, unsigned int millisec)
Definition: async_queue.h:1099
A scoped locking class for exception safe Mutex locking.
Definition: mutex.h:207
bool move_pop_timed_dispatch_basic(value_type &obj, unsigned int millisec)
Definition: async_queue.h:1175
void move_pop_dispatch_basic(value_type &obj)
Definition: async_queue.h:1012
void move_pop_basic(value_type &obj)
Definition: async_queue.h:375
AsyncQueueDispatch & operator=(AsyncQueueDispatch &&rhs)
Definition: async_queue.h:1332
~AsyncQueue()
Definition: async_queue.h:591
AsyncQueueDispatch(AsyncQueueDispatch &&rhs)
Definition: async_queue.h:1400
bool empty() const
Definition: async_queue.h:407
void push(const value_type &obj)
Definition: async_queue.h:194
int timed_wait(Mutex &mutex, const timespec &abs_time)
Definition: mutex.h:558
AsyncQueue(AsyncQueue &&rhs)
Definition: async_queue.h:559
A wrapper class for pthread mutexes.
Definition: mutex.h:117
void move_pop_dispatch(value_type &obj)
Definition: async_queue.h:946
int unlock()
Definition: mutex.h:170
void pop(value_type &obj)
Definition: async_queue.h:272
AsyncQueue & operator=(AsyncQueue &&rhs)
Definition: async_queue.h:506
void emplace(Args &&... args)
Definition: async_queue.h:246
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
Definition: application.h:44
void push(const value_type &obj)
Definition: async_queue.h:690
Container::value_type value_type
Definition: async_queue.h:149
bool pop_timed_dispatch(value_type &obj, unsigned int millisec)
Definition: async_queue.h:1044
AsyncQueueDispatch(const AsyncQueueDispatch &rhs)
Definition: async_queue.h:1430
int trylock()
Definition: mutex.h:157
void move_pop(value_type &obj)
Definition: async_queue.h:813
size_type size() const
Definition: async_queue.h:1224
Container::value_type value_type
Definition: async_queue.h:644
AsyncQueueDispatch & operator=(const AsyncQueueDispatch &rhs)
Definition: async_queue.h:1290
void swap(AsyncQueue &other)
Definition: async_queue.h:441
int lock()
Definition: mutex.h:147
bool empty() const
Definition: async_queue.h:1207
Container container_type
Definition: async_queue.h:646
size_type size() const
Definition: async_queue.h:424
AsyncQueue & operator=(const AsyncQueue &rhs)
Definition: async_queue.h:472
Container container_type
Definition: async_queue.h:151
void swap(AsyncQueueDispatch &other)
Definition: async_queue.h:1246
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Container::size_type size_type
Definition: async_queue.h:645
int wait(Mutex &mutex)
Definition: mutex.h:508