Dawn Framework 1.0
Universal data acquisition framework for embedded systems
notifier.cxx
1// dawn/src/io/notifier.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/notifier.hxx"
7
8#include <new>
9
10#include "dawn/common/poll_loop.hxx"
11#include "dawn/io/common.hxx"
12
13using namespace dawn;
14
15// Update poll array.
16
17void CIONotifier::updatePfds()
18{
19 size_t i = 0;
20
21 pfdsLock.lock();
22
23 for (const SIONotifierPriv &tmp : vnote)
24 {
25 bool found = false;
26 int fd;
27
28 // Check for at least one valid callback
29
30 for (const SIONotifier &n : tmp.user)
31 {
32 if (n.cb)
33 {
34 found = true;
35 break;
36 }
37 }
38
39 fd = tmp.user[0].io->getFd();
40
41 // Add to poll array if callback is registered and FD is valid
42
43 if (found && fd >= 0)
44 {
45 pfds[i].fd = fd;
46 pfds[i].events = POLLIN;
47 pfds[i].revents = 0;
48 }
49 else
50 {
51 if (fd < 0)
52 {
53 DAWNERR("Invalid FD %d from IO 0x%08" PRIx32 ", skipping notifier\n",
54 fd,
55 tmp.user[0].io->getIdV());
56 }
57
58 pfds[i].fd = -1;
59 }
60
61 // Next item
62
63 i++;
64 }
65
66 pfdsLock.unlock();
67}
68
69void CIONotifier::thread()
70{
71 SPollLoopCallbacks callbacks;
72
73 struct
74 {
75 CIONotifier *self;
76 } ctx;
77
78 ctx.self = this;
79
80 /* Loop until stop called */
81
82 callbacks.beforePoll = [](void *priv, struct pollfd *pollfds, nfds_t nfds) -> int
83 {
84 CIONotifier *self;
85
86 if (priv == nullptr)
87 {
88 return -EINVAL;
89 }
90
91 self = static_cast<decltype(ctx) *>(priv)->self;
92 if (self == nullptr)
93 {
94 return -EINVAL;
95 }
96
97 (void)pollfds;
98 (void)nfds;
99
100 // Update poll array if requested
101 if (self->pfdsUpdate.load())
102 {
103 self->updatePfds();
104 self->pfdsUpdate = false;
105 }
106
107 return OK;
108 };
109
110 callbacks.afterPoll = [](void *priv, struct pollfd *pollfds, nfds_t nfds, int ret)
111 {
112 (void)priv;
113 (void)pollfds;
114 (void)nfds;
115 (void)ret;
116 };
117
118 callbacks.onPollReady = [](void *priv, struct pollfd *pollfds, nfds_t nfds, int pollRet) -> int
119 {
120 CIONotifier *self;
121 size_t i;
122 int ret;
123 int j;
124
125 if (priv == nullptr || pollfds == nullptr || nfds == 0 || pollRet <= 0)
126 {
127 return -EINVAL;
128 }
129
130 self = static_cast<decltype(ctx) *>(priv)->self;
131 if (self == nullptr)
132 {
133 return -EINVAL;
134 }
135
136 j = 0;
137
138 // Lock poll data until we handle all requests
139 self->pfdsLock.lock();
140
141 for (i = 0; i < self->pfdsLen; i++)
142 {
143 if (pollfds[i].revents & POLLIN)
144 {
145 // Read data with configured batch count; skip callbacks when
146 // the IO has no valid data (e.g. a filtered-out record)
147 ret = self->vnote[i].user[0].io->getData(*self->vnote[i].data, self->vnote[i].batch);
148
149 if (ret >= 0)
150 {
151 for (SIONotifier &user : self->vnote[i].user)
152 {
153 if (user.cb)
154 {
155 user.cb(user.priv, self->vnote[i].data);
156 }
157 }
158 }
159
160 pollfds[i].revents = 0;
161 j++;
162 }
163
164 // poll() returns the count of fds with events; once we've handled
165 // that many we can stop scanning the rest.
166 if (j >= pollRet)
167 {
168 break;
169 }
170 }
171
172 self->pfdsLock.unlock();
173 return OK;
174 };
175
176 CPollLoopRunner::run(threadCtl, pfds, pfdsLen, DAWN_POLL_TIMEOUT_MS, callbacks, &ctx);
177}
178
179CIONotifier::~CIONotifier()
180{
181 // Make sure thread is stopped
182
183 stop();
184
185 // Clear flag
186
187 pfdsUpdate = false;
188
189 // Free allocated data
190
191 for (const auto &n : vnote)
192 {
193 if (n.data)
194 {
195 delete n.data;
196 }
197 }
198
199 // Clear registered notifications
200
201 vnote.clear();
202
203 // Free poll array
204
205 delete[] pfds;
206}
207
208int CIONotifier::start()
209{
210 // Allocate poll array
211
212 pfdsLen = vnote.size();
213 pfds = new (std::nothrow) pollfd[pfdsLen]();
214 if (pfds == nullptr)
215 {
216 DAWNERR("Failed to allocate poll array\n");
217 return -ENOMEM;
218 }
219
220 // Initialize notifiers
221
222 updatePfds();
223
224 // Start notifier thread
225
226 threadCtl.setThreadFunc([this]() { thread(); });
227 return threadCtl.threadStart();
228}
229
230int CIONotifier::stop()
231{
232 // Stop notifier thread
233
234 return threadCtl.threadStop();
235}
236
238{
239 SIONotifierPriv np;
240 int update = -1;
241 int i = 0;
242
243 if (n.io == nullptr)
244 {
245 DAWNERR("IO pointer is null\n");
246 return -EINVAL;
247 }
248
249 if (!n.io->isNotify())
250 {
251 DAWNERR("isNotify=false for IO 0x%08" PRIx32 "\n", n.io->getIdV());
252 return -EINVAL;
253 }
254
255 // Check if we need update old entry
256
257 for (const auto &v : vnote)
258 {
259 if (n.io == v.user[0].io)
260 {
261 update = i;
262 break;
263 }
264
265 i++;
266 }
267
268 pfdsLock.lock();
269
270 // Update vnote entry or add to vector
271
272 if (update != -1)
273 {
274 vnote[update].user.push_back(n);
275
276 pfdsUpdate = true;
277 }
278 else
279 {
280 // regNotifier() can be called only before start was called
281 // update vnote when thread is running is not supported now
282
283 if (threadCtl.hasThreadObject())
284 {
285 DAWNERR("Cannot register new notifier after thread started\n");
286 pfdsLock.unlock();
287 return -EPERM;
288 }
289
290 np.user.push_back(n);
291
292 // Allocate data handler with configured batch count
293
294 np.batch = n.io->getNotifyBatch();
295 np.data = n.io->ddata_alloc(np.batch);
296 if (np.data == nullptr)
297 {
298 DAWNERR("Failed to allocate data for notifier\n");
299 pfdsLock.unlock();
300 return -ENOMEM;
301 }
302
303 vnote.push_back(np);
304 }
305
306 pfdsLock.unlock();
307
308 return OK;
309}
310
312{
313 if (io == nullptr || data == nullptr)
314 {
315 return -EINVAL;
316 }
317
318 pfdsLock.lock();
319
320 for (auto &entry : vnote)
321 {
322 if (entry.user.empty() || entry.user[0].io != io)
323 {
324 continue;
325 }
326
327 for (SIONotifier &user : entry.user)
328 {
329 if (user.cb)
330 {
331 user.cb(user.priv, data);
332 }
333 }
334
335 pfdsLock.unlock();
336 return OK;
337 }
338
339 pfdsLock.unlock();
340 return -ENOENT;
341}
Base class for all I/O objects.
Definition common.hxx:30
virtual bool isNotify() const =0
Check if IO supports notifications.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
Definition common.cxx:249
int notifyData(CIOCommon *io, io_ddata_t *data)
Emit an immediate notification for already-available data.
Definition notifier.cxx:311
int regNotifier(SIONotifier n)
Register I/O notification callback.
Definition notifier.cxx:237
SObjectId::ObjectId getIdV() const
Get object identifier as raw 32-bit value.
Definition object.cxx:155
static int run(CThreadedObject &threadCtl, struct pollfd *pfds, nfds_t nfds, int timeoutMs, const SPollLoopCallbacks &callbacks, void *priv)
Run poll loop until quit is requested.
Definition poll_loop.hxx:61
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Notifier registration structure.
Definition inotifier.hxx:46
CIOCommon * io
I/O object pointer.
Definition inotifier.hxx:62
int(* cb)(void *priv, io_ddata_t *data)
Notifier callback function type.
Definition inotifier.hxx:70
void * priv
Private context pointer.
Definition inotifier.hxx:53
void(* afterPoll)(void *priv, struct pollfd *pfds, nfds_t nfds, int ret)
Optional hook called after each poll() return.
Definition poll_loop.hxx:39
int(* onPollReady)(void *priv, struct pollfd *pfds, nfds_t nfds, int pollRet)
Optional hook called when poll() reports ready descriptors.
Definition poll_loop.hxx:45
int(* beforePoll)(void *priv, struct pollfd *pfds, nfds_t nfds)
Optional hook called before each poll() call.
Definition poll_loop.hxx:33
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21