libzypp: zypp-core/ng/thread/asyncqueue.h Source File

libzypp 17.38.11

asyncqueue.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9#ifndef ZYPP_NG_THREAD_ASYNCQUEUE_H_INCLUDED
10#define ZYPP_NG_THREAD_ASYNCQUEUE_H_INCLUDED
11
12#include <zypp-core/ng/base/AbstractEventSource>
13
14#include <queue>
15#include <set>
16#include <mutex>
17#include <memory>
18#include <optional>
19#include <condition_variable>
20
21namespace zyppng {
22
23 class AsyncQueueWatch;
24
25 class AsyncQueueBase {
26 public:
27 virtual ~AsyncQueueBase();
28
29 void addWatch ( AsyncQueueWatch &watch );
30 void removeWatch ( AsyncQueueWatch &watch );
31 void notifyWatches ( );
32
33 private:
34 std::set<AsyncQueueWatch *> _watches;
35 std::recursive_mutex _watchLock;
36 };
37
42 template< class Message >
43 class AsyncQueue : public AsyncQueueBase {
44
45 public:
46
47 using Ptr = std::shared_ptr<AsyncQueue>;
48
49 static Ptr create () {
50 return Ptr( new AsyncQueue() );
51 }
52
53 AsyncQueue(const AsyncQueue&) = delete;
54 AsyncQueue& operator=(const AsyncQueue&) = delete;
55
63 template< typename T = Message >
64 void pushUnlocked ( T &&value ) {
65 _messages.push( std::forward<T>(value) );
66 }
67
72 template< typename T = Message >
73 void push ( T &&value ) {
74 {
75 std::lock_guard lk( _mut );
76 pushUnlocked( std::forward<T>(value) );
77 }
78 notify();
79 }
80
84 Message pop () {
85 std::unique_lock<std::mutex> lk( _mut );
86 _cv.wait( lk, [this](){ return _messages.size() > 0; } );
87 Message msg = std::move( _messages.front() );
88 _messages.pop();
89 return msg;
90 }
91
96 std::optional<Message> tryPop () {
97 std::lock_guard lk( _mut );
98 return tryPopUnlocked();
99 }
100
105 std::optional<Message> tryPopUnlocked () {
106 if ( _messages.size() ) {
107 Message msg = std::move( _messages.front() );
108 _messages.pop();
109 return msg;
110 }
111 return {};
112 }
113
118 void lock () {
119 _mut.lock();
120 }
121
126 void unlock () {
127 _mut.unlock();
128 }
129
134 void notify () {
135 _cv.notify_all();
136 notifyWatches();
137 }
138
139 private:
140 AsyncQueue() = default;
141 std::queue<Message> _messages;
142 std::mutex _mut;
143 std::condition_variable _cv;
144 };
145
146 class AsyncQueueWatchPrivate;
147 class AsyncQueueWatch : public AbstractEventSource
148 {
149 ZYPP_DECLARE_PRIVATE(AsyncQueueWatch)
150 public:
151
152 static std::shared_ptr<AsyncQueueWatch> create ( std::shared_ptr<AsyncQueueBase> queue );
153 ~AsyncQueueWatch() override;
154
155 void postNotifyEvent ();
156
157 SignalProxy<void()> sigMessageAvailable();
158
159 // AbstractEventSource interface
160 void onFdReady(int fd, int events) override;
161 void onSignal(int signal) override;
162
163 protected:
164 AsyncQueueWatch( std::shared_ptr<AsyncQueueBase> &&queue );
165 AsyncQueueWatch( AsyncQueueWatchPrivate &dd );
166 };
167
168}
169
170
171#endif
zyppng::AbstractEventSource::AbstractEventSource
AbstractEventSource()
Definition abstracteventsource.cc:20
zyppng::AsyncQueueBase
Definition asyncqueue.h:25
zyppng::AsyncQueueBase::addWatch
void addWatch(AsyncQueueWatch &watch)
Definition asyncqueue.cc:12
zyppng::AsyncQueueBase::~AsyncQueueBase
virtual ~AsyncQueueBase()
Definition asyncqueue.cc:9
zyppng::AsyncQueueBase::removeWatch
void removeWatch(AsyncQueueWatch &watch)
Definition asyncqueue.cc:18
zyppng::AsyncQueueBase::_watches
std::set< AsyncQueueWatch * > _watches
Definition asyncqueue.h:34
zyppng::AsyncQueueBase::notifyWatches
void notifyWatches()
Definition asyncqueue.cc:24
zyppng::AsyncQueueBase::_watchLock
std::recursive_mutex _watchLock
Definition asyncqueue.h:35
zyppng::AsyncQueueWatchPrivate
Definition asyncqueue_p.h:13
zyppng::AsyncQueueWatch
Definition asyncqueue.h:148
zyppng::AsyncQueueWatch::postNotifyEvent
void postNotifyEvent()
Definition asyncqueue.cc:78
zyppng::AsyncQueueWatch::~AsyncQueueWatch
~AsyncQueueWatch() override
Definition asyncqueue.cc:70
zyppng::AsyncQueueWatch::onSignal
void onSignal(int signal) override
Definition asyncqueue.cc:105
zyppng::AsyncQueueWatch::AsyncQueueWatch
AsyncQueueWatch(std::shared_ptr< AsyncQueueBase > &&queue)
zyppng::AsyncQueueWatch::onFdReady
void onFdReady(int fd, int events) override
Definition asyncqueue.cc:95
zyppng::AsyncQueueWatch::sigMessageAvailable
SignalProxy< void()> sigMessageAvailable()
Definition asyncqueue.cc:90
zyppng::AsyncQueueWatch::create
static std::shared_ptr< AsyncQueueWatch > create(std::shared_ptr< AsyncQueueBase > queue)
Definition asyncqueue.cc:61
zyppng::AsyncQueue::notify
void notify()
Definition asyncqueue.h:134
zyppng::AsyncQueue::_cv
std::condition_variable _cv
Definition asyncqueue.h:143
zyppng::AsyncQueue::tryPopUnlocked
std::optional< Message > tryPopUnlocked()
Definition asyncqueue.h:105
zyppng::AsyncQueue::AsyncQueue
AsyncQueue(const AsyncQueue &)=delete
zyppng::AsyncQueue::tryPop
std::optional< Message > tryPop()
Definition asyncqueue.h:96
zyppng::AsyncQueue::unlock
void unlock()
Definition asyncqueue.h:126
zyppng::AsyncQueue::AsyncQueue
AsyncQueue()=default
zyppng::AsyncQueue::_messages
std::queue< Message > _messages
Definition asyncqueue.h:141
zyppng::AsyncQueue::pushUnlocked
void pushUnlocked(T &&value)
Definition asyncqueue.h:64
zyppng::AsyncQueue::create
static Ptr create()
Definition asyncqueue.h:49
zyppng::AsyncQueue::_mut
std::mutex _mut
Definition asyncqueue.h:142
zyppng::AsyncQueue::Ptr
std::shared_ptr< AsyncQueue > Ptr
Definition asyncqueue.h:47
zyppng::AsyncQueue::lock
void lock()
Definition asyncqueue.h:118
zyppng::AsyncQueue::operator=
AsyncQueue & operator=(const AsyncQueue &)=delete
zyppng::AsyncQueue::push
void push(T &&value)
Definition asyncqueue.h:73
zyppng::AsyncQueue::pop
Message pop()
Definition asyncqueue.h:84
zyppng::SignalProxy
Definition signals.h:325
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::UserRequestType::Message
@ Message
Definition userrequest.h:35
ZYPP_DECLARE_PRIVATE
#define ZYPP_DECLARE_PRIVATE(Class)
Definition zyppglobal.h:80