6#include "dawn/io/notifier.hxx"
10#include "dawn/common/poll_loop.hxx"
11#include "dawn/io/common.hxx"
17void CIONotifier::updatePfds()
23 for (
const SIONotifierPriv &tmp : vnote)
39 fd = tmp.user[0].io->getFd();
46 pfds[i].events = POLLIN;
53 DAWNERR(
"Invalid FD %d from IO 0x%08" PRIx32
", skipping notifier\n",
55 tmp.user[0].io->getIdV());
69void CIONotifier::thread()
71 SPollLoopCallbacks callbacks;
82 callbacks.
beforePoll = [](
void *priv,
struct pollfd *pollfds, nfds_t nfds) ->
int
91 self =
static_cast<decltype(ctx) *
>(priv)->self;
101 if (self->pfdsUpdate.load())
104 self->pfdsUpdate = false;
110 callbacks.
afterPoll = [](
void *priv,
struct pollfd *pollfds, nfds_t nfds,
int ret)
118 callbacks.
onPollReady = [](
void *priv,
struct pollfd *pollfds, nfds_t nfds,
int pollRet) ->
int
125 if (priv ==
nullptr || pollfds ==
nullptr || nfds == 0 || pollRet <= 0)
130 self =
static_cast<decltype(ctx) *
>(priv)->self;
139 self->pfdsLock.lock();
141 for (i = 0; i < self->pfdsLen; i++)
143 if (pollfds[i].revents & POLLIN)
147 ret = self->vnote[i].user[0].io->getData(*self->vnote[i].data, self->vnote[i].batch);
155 user.cb(user.priv, self->vnote[i].data);
160 pollfds[i].revents = 0;
172 self->pfdsLock.unlock();
179CIONotifier::~CIONotifier()
191 for (
const auto &n : vnote)
208int CIONotifier::start()
212 pfdsLen = vnote.size();
213 pfds =
new (std::nothrow) pollfd[pfdsLen]();
216 DAWNERR(
"Failed to allocate poll array\n");
226 threadCtl.setThreadFunc([
this]() { thread(); });
227 return threadCtl.threadStart();
230int CIONotifier::stop()
234 return threadCtl.threadStop();
245 DAWNERR(
"IO pointer is null\n");
251 DAWNERR(
"isNotify=false for IO 0x%08" PRIx32
"\n", n.
io->
getIdV());
257 for (
const auto &v : vnote)
259 if (n.
io == v.user[0].io)
274 vnote[update].user.push_back(n);
283 if (threadCtl.hasThreadObject())
285 DAWNERR(
"Cannot register new notifier after thread started\n");
290 np.user.push_back(n);
294 np.batch = n.
io->getNotifyBatch();
296 if (np.data ==
nullptr)
298 DAWNERR(
"Failed to allocate data for notifier\n");
313 if (io ==
nullptr || data ==
nullptr)
320 for (
auto &entry : vnote)
322 if (entry.user.empty() || entry.user[0].io != io)
Base class for all I/O objects.
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.
int notifyData(CIOCommon *io, io_ddata_t *data)
Emit an immediate notification for already-available data.
int regNotifier(SIONotifier n)
Register I/O notification callback.
SObjectId::ObjectId getIdV() const
Get object identifier as raw 32-bit value.
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.
Out-of-tree user-extension hooks for Dawn.
Notifier registration structure.
CIOCommon * io
I/O object pointer.
int(* cb)(void *priv, io_ddata_t *data)
Notifier callback function type.
void * priv
Private context pointer.
void(* afterPoll)(void *priv, struct pollfd *pfds, nfds_t nfds, int ret)
Optional hook called after each poll() return.
int(* onPollReady)(void *priv, struct pollfd *pfds, nfds_t nfds, int pollRet)
Optional hook called when poll() reports ready descriptors.
int(* beforePoll)(void *priv, struct pollfd *pfds, nfds_t nfds)
Optional hook called before each poll() call.
Heap-allocated dynamic I/O data buffer.