11#include < arpa/inet.h>
22#include < netinet/in.h>
3+ #include < poll.h>
34#include < sys/socket.h>
45
56#include " capiocl.hpp"
@@ -74,19 +75,37 @@ static int incoming_socket_multicast(const std::string &address_ip, const int po
7475 return _socket;
7576}
7677
77- [[noreturn]] void
78- capiocl::monitor::MulticastMonitor::commit_listener (std::vector<std::string> &committed_files,
79- std::mutex &lock, const std::string &ip_addr,
80- const int ip_port) {
78+ void capiocl::monitor::MulticastMonitor::commit_listener (std::vector<std::string> &committed_files,
79+ std::mutex &lock,
80+ const std::string &ip_addr,
81+ const int ip_port, const bool *terminate) {
82+ pthread_setcancelstate (PTHREAD_CANCEL_ASYNCHRONOUS, nullptr );
8183 sockaddr_in addr_in = {};
8284 socklen_t addr_len = {};
8385 const auto socket = incoming_socket_multicast (ip_addr, ip_port, addr_in, addr_len);
8486 const auto addr = reinterpret_cast <sockaddr *>(&addr_in);
8587 char incoming_message[MESSAGE_SIZE] = {0 };
8688
89+ // Polling for non blocking
90+ pollfd pfd = {};
91+ pfd.fd = socket;
92+ pfd.events = POLLIN | POLLPRI;
93+
8794 do {
8895 bzero (incoming_message, sizeof (incoming_message));
8996
97+ // TODO: migrate to epoll for linux and kqueue on MacOS
98+ if (poll (&pfd, 1 , MULTICAST_THREAD_POLL_INTERVAL) == 0 ) {
99+ // No data from incoming socket. Continue, awaking thread ensuring pthread_cancel points
100+ // can be reached
101+ if (*terminate) {
102+ close (socket);
103+ return ;
104+ }
105+
106+ continue ;
107+ }
108+
90109 // LCOV_EXCL_START
91110 if (recvfrom (socket, incoming_message, MESSAGE_SIZE, 0 , addr, &addr_len) < 0 ) {
92111 continue ;
@@ -113,9 +132,11 @@ capiocl::monitor::MulticastMonitor::commit_listener(std::vector<std::string> &co
113132 } while (true );
114133}
115134
116- [[noreturn]] void capiocl::monitor::MulticastMonitor::home_node_listener (
135+ void capiocl::monitor::MulticastMonitor::home_node_listener (
117136 std::unordered_map<std::string, std::string> &home_nodes, std::mutex &lock,
118- const std::string &ip_addr, int ip_port) {
137+ const std::string &ip_addr, int ip_port, const bool *terminate) {
138+ pthread_setcancelstate (PTHREAD_CANCEL_ASYNCHRONOUS, nullptr );
139+
119140 char this_hostname[HOST_NAME_MAX] = {};
120141 gethostname (this_hostname, HOST_NAME_MAX);
121142
@@ -129,6 +150,23 @@ capiocl::monitor::MulticastMonitor::commit_listener(std::vector<std::string> &co
129150 do {
130151 bzero (incoming_message, sizeof (incoming_message));
131152
153+ // Polling for non blocking
154+ pollfd pfd = {};
155+ pfd.fd = socket;
156+ pfd.events = POLLIN | POLLPRI;
157+
158+ // TODO: migrate to epoll for linux and kqueue on MacOS
159+ if (poll (&pfd, 1 , MULTICAST_THREAD_POLL_INTERVAL) == 0 ) {
160+ // No data from incoming socket. Continue, awaking thread ensuring pthread_cancel points
161+ // can be reached
162+ if (*terminate) {
163+ close (socket);
164+ return ;
165+ }
166+
167+ continue ;
168+ }
169+
132170 // LCOV_EXCL_START
133171 if (recvfrom (socket, incoming_message, MESSAGE_SIZE, 0 , addr, &addr_len) < 0 ) {
134172 continue ;
@@ -193,19 +231,23 @@ capiocl::monitor::MulticastMonitor::MulticastMonitor(
193231
194232 commit_thread =
195233 std::thread (&commit_listener, std::ref (_committed_files), std::ref (committed_lock),
196- MULTICAST_COMMIT_ADDR, MULTICAST_COMMIT_PORT);
234+ MULTICAST_COMMIT_ADDR, MULTICAST_COMMIT_PORT, & this -> terminate );
197235
198236 home_node_thread =
199237 std::thread (&home_node_listener, std::ref (_home_nodes), std::ref (home_node_lock),
200- MULTICAST_HOME_NODE_ADDR, MULTICAST_HOME_NODE_PORT);
238+ MULTICAST_HOME_NODE_ADDR, MULTICAST_HOME_NODE_PORT, & this -> terminate );
201239
202240 gethostname (_hostname, HOST_NAME_MAX);
203241}
204242
205243capiocl::monitor::MulticastMonitor::~MulticastMonitor () {
244+
245+ terminate = true ;
246+
206247 pthread_cancel (commit_thread.native_handle ());
207- pthread_cancel (home_node_thread.native_handle ());
208248 commit_thread.join ();
249+
250+ pthread_cancel (home_node_thread.native_handle ());
209251 home_node_thread.join ();
210252}
211253
0 commit comments