Dawn Framework 1.0
Universal data acquisition framework for embedded systems
poll_loop.hxx
1// dawn/include/dawn/common/poll_loop.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <poll.h>
9
10#include "dawn/common/thread.hxx"
11
12namespace dawn
13{
21static constexpr int DAWN_POLL_TIMEOUT_MS = 1000;
22
28{
33 int (*beforePoll)(void *priv, struct pollfd *pfds, nfds_t nfds);
34
39 void (*afterPoll)(void *priv, struct pollfd *pfds, nfds_t nfds, int ret);
40
45 int (*onPollReady)(void *priv, struct pollfd *pfds, nfds_t nfds, int pollRet);
46};
47
53{
54public:
61 static int run(CThreadedObject &threadCtl,
62 struct pollfd *pfds,
63 nfds_t nfds,
64 int timeoutMs,
65 const SPollLoopCallbacks &callbacks,
66 void *priv)
67 {
68 int ret;
69
70 do
71 {
72 if (callbacks.beforePoll)
73 {
74 ret = callbacks.beforePoll(priv, pfds, nfds);
75 if (ret < 0)
76 {
77 continue;
78 }
79 }
80
81 ret = poll(pfds, nfds, timeoutMs);
82
83 if (callbacks.afterPoll)
84 {
85 callbacks.afterPoll(priv, pfds, nfds, ret);
86 }
87
88 if (ret > 0 && callbacks.onPollReady)
89 {
90 callbacks.onPollReady(priv, pfds, nfds, ret);
91 }
92 }
93 while (!threadCtl.shouldQuit());
94
95 threadCtl.markThreadFinished();
96 return 0;
97 }
98};
99
100} // Namespace dawn
Shared runner for poll-based worker threads.
Definition poll_loop.hxx:53
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
Portable thread owner abstraction for Dawn components.
Definition thread.hxx:32
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Callback set for poll-based worker loops.
Definition poll_loop.hxx:28
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