Dawn Framework 1.0
Universal data acquisition framework for embedded systems
stream_notifier.cxx
1// dawn/src/io/stream_notifier.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/stream_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
15CStreamNotifier::CStreamNotifier()
16 : io(nullptr)
17 , data(nullptr)
18 , batch(1)
19 , threadCtl()
20{
21}
22
23CStreamNotifier::~CStreamNotifier()
24{
25 // Make sure thread is stopped
26
27 stop();
28
29 // Free allocated data
30
31 if (data)
32 {
33 delete data;
34 }
35
36 // Clear registered callbacks
37
38 users.clear();
39}
40
41void CStreamNotifier::thread()
42{
43 SPollLoopCallbacks callbacks;
44 struct pollfd pfd;
45
46 struct
47 {
48 CStreamNotifier *self;
49 } ctx;
50
51 ctx.self = this;
52
53 // Set up single file descriptor for polling
54
55 pfd.fd = io->getFd();
56 pfd.events = POLLIN;
57 pfd.revents = 0;
58
59 // No pre/post-poll hooks needed for single-fd stream
60
61 callbacks.beforePoll = nullptr;
62 callbacks.afterPoll = nullptr;
63
64 callbacks.onPollReady = [](void *priv, struct pollfd *pollfds, nfds_t nfds, int pollRet) -> int
65 {
66 CStreamNotifier *self;
67
68 (void)pollfds;
69 (void)nfds;
70 (void)pollRet;
71
72 if (priv == nullptr)
73 {
74 return -EINVAL;
75 }
76
77 self = static_cast<decltype(ctx) *>(priv)->self;
78 if (self == nullptr)
79 {
80 return -EINVAL;
81 }
82
83 // Read data with configured batch count; skip callbacks when the IO
84 // has no valid data (e.g. a filtered-out record)
85
86 if (self->io->getData(*self->data, self->batch) < 0)
87 {
88 return OK;
89 }
90
91 // Dispatch to all registered callbacks
92
93 for (SIONotifier &user : self->users)
94 {
95 if (user.cb)
96 {
97 user.cb(user.priv, self->data);
98 }
99 }
100
101 return OK;
102 };
103
104 CPollLoopRunner::run(threadCtl, &pfd, 1, DAWN_POLL_TIMEOUT_MS, callbacks, &ctx);
105}
106
107int CStreamNotifier::start()
108{
109 if (io == nullptr)
110 {
111 DAWNERR("No IO bound to stream notifier\n");
112 return -EINVAL;
113 }
114
115 // Start notifier thread
116
117 threadCtl.setThreadFunc([this]() { thread(); });
118 return threadCtl.threadStart();
119}
120
121int CStreamNotifier::stop()
122{
123 // Stop notifier thread
124
125 return threadCtl.threadStop();
126}
127
129{
130 if (n.io == nullptr)
131 {
132 DAWNERR("IO pointer is null\n");
133 return -EINVAL;
134 }
135
136 if (!n.io->isNotify())
137 {
138 DAWNERR("isNotify=false!\n");
139 return -EINVAL;
140 }
141
142 // Stream notifier is 1:1 with IO
143
144 if (io != nullptr && io != n.io)
145 {
146 DAWNERR("Stream notifier already bound to different IO\n");
147 return -EINVAL;
148 }
149
150 // First registration: bind IO and allocate data buffer
151
152 if (io == nullptr)
153 {
154 if (threadCtl.hasThreadObject())
155 {
156 DAWNERR("Cannot bind new IO after thread started\n");
157 return -EPERM;
158 }
159
160 io = n.io;
161 batch = n.io->getNotifyBatch();
162 data = n.io->ddata_alloc(batch);
163 if (data == nullptr)
164 {
165 DAWNERR("Failed to allocate data for stream notifier\n");
166 io = nullptr;
167 return -ENOMEM;
168 }
169 }
170
171 // Append callback
172
173 users.push_back(n);
174
175 return OK;
176}
int getData(IODataCmn &data, size_t len, size_t offset=0)
Get data from I/O (public interface with stats tracking).
Definition common.hxx:382
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
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
int regNotifier(SIONotifier n)
Register I/O notification callback.
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
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