Dawn Framework 1.0
Universal data acquisition framework for embedded systems
notify_manager.cxx
1// dawn/src/io/notify_manager.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/notify_manager.hxx"
7
8#include <new>
9
10using namespace dawn;
11
12CIONotifierManager::CIONotifierManager()
13{
14}
15
16CIONotifierManager::~CIONotifierManager()
17{
18 // Stop all notifiers
19
20 stop();
21
22 // Delete all notifier instances
23
24 for (auto &e : entries)
25 {
26 switch (e.type)
27 {
29 {
30 delete e.poll;
31 break;
32 }
33
34#ifdef CONFIG_DAWN_IO_NOTIFY_STREAM
36 {
37 delete e.stream;
38 break;
39 }
40#endif
41
42 default:
43 {
44 break;
45 }
46 }
47 }
48
49 entries.clear();
50}
51
52CIONotifier *CIONotifierManager::findPoll(int prio)
53{
54 for (auto &e : entries)
55 {
56 if (e.type == CIOCommon::IO_NOTIFY_POLL && e.prio == prio)
57 {
58 return e.poll;
59 }
60 }
61
62 return nullptr;
63}
64
65int CIONotifierManager::regIO(CIOCommon *io)
66{
67 uint8_t type;
68 int prio;
69
70 if (io == nullptr)
71 {
72 return -EINVAL;
73 }
74
75 // Only register notifiable IOs with valid file descriptors
76
77 if (!io->isNotify() || io->getFd() < 0)
78 {
79 return OK;
80 }
81
82 // Get IO notifier configuration
83
84 type = io->getNotifyType();
85 prio = io->getNotifyPrio();
86
87 switch (type)
88 {
90 {
91 CIONotifier *notifier;
92 SNotifierEntry entry;
93
94 // Find or create notifier for this priority
95
96 notifier = findPoll(prio);
97 if (notifier == nullptr)
98 {
99 notifier = new (std::nothrow) CIONotifier();
100 if (notifier == nullptr)
101 {
102 DAWNERR("Failed to allocate poll notifier\n");
103 return -ENOMEM;
104 }
105
106 notifier->setThreadPriority(prio);
107
108 entry.type = type;
109 entry.prio = prio;
110 entry.poll = notifier;
111 entries.push_back(entry);
112 }
113
114 // Bind notifier to IO
115
116 io->bindNotifier(notifier);
117 break;
118 }
119
120#ifdef CONFIG_DAWN_IO_NOTIFY_STREAM
122 {
123 CStreamNotifier *notifier;
124 SNotifierEntry entry;
125
126 // Always create new stream notifier (one per IO)
127
128 notifier = new (std::nothrow) CStreamNotifier();
129 if (notifier == nullptr)
130 {
131 DAWNERR("Failed to allocate stream notifier\n");
132 return -ENOMEM;
133 }
134
135 notifier->setThreadPriority(prio);
136
137 entry.type = type;
138 entry.prio = prio;
139 entry.stream = notifier;
140 entries.push_back(entry);
141
142 // Bind notifier to IO
143
144 io->bindNotifier(notifier);
145 break;
146 }
147#endif
148
149 default:
150 {
151 DAWNERR("Unsupported notifier type %d\n", type);
152 return -ENOTSUP;
153 }
154 }
155
156 // Register dummy notification to allocate data buffer and poll entry
157
158 io->setNotifier(nullptr, 0, 0);
159
160 return OK;
161}
162
163int CIONotifierManager::start()
164{
165 int ret;
166
167 for (auto &e : entries)
168 {
169 switch (e.type)
170 {
172 {
173 ret = e.poll->start();
174 break;
175 }
176
177#ifdef CONFIG_DAWN_IO_NOTIFY_STREAM
179 {
180 ret = e.stream->start();
181 break;
182 }
183#endif
184
185 default:
186 {
187 ret = -ENOTSUP;
188 break;
189 }
190 }
191
192 if (ret != OK)
193 {
194 DAWNERR("Failed to start notifier (type=%d prio=%d err=%d)\n", e.type, e.prio, ret);
195 return ret;
196 }
197 }
198
199 return OK;
200}
201
202int CIONotifierManager::stop()
203{
204 for (auto &e : entries)
205 {
206 switch (e.type)
207 {
209 {
210 e.poll->stop();
211 break;
212 }
213
214#ifdef CONFIG_DAWN_IO_NOTIFY_STREAM
216 {
217 e.stream->stop();
218 break;
219 }
220#endif
221
222 default:
223 {
224 break;
225 }
226 }
227 }
228
229 return OK;
230}
Base class for all I/O objects.
Definition common.hxx:27
virtual bool isNotify() const =0
Check if IO supports notifications.
virtual int getFd() const
Get file descriptor for notifications.
Definition common.hxx:425
@ IO_NOTIFY_POLL
Poll-based notifier (default)
Definition common.hxx:223
@ IO_NOTIFY_STREAM
Stream notifier (blocking read, one per IO)
Definition common.hxx:224
I/O notification handler with poll-based event delivery.
Definition notifier.hxx:31
int start()
Start object.
Definition object.hxx:116
Stream-based I/O notifier with dedicated thread per I/O.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13