#pragma once
+#include <atomic>
#include <iostream>
#include <string>
#include <memory>
std::mutex map_mutex; // protects wd_callback_map and wd_remove_map
wd_callback_map_t wd_callback_map;
wd_remove_map_t wd_remove_map;
- bool shutdown{false};
+ std::atomic<bool> shutdown{false};
class AlignedBuf
{
struct pollfd fds[2] = {{wfd, POLLIN}, {efd, POLLIN}};
restart:
- while(! shutdown) {
+ while(! shutdown.load(std::memory_order_acquire)) {
npoll = poll(fds, nfds, -1); /* for up to 10 fds, poll is fast as epoll */
- if (shutdown) {
+ if (shutdown.load(std::memory_order_acquire)) {
return;
}
if (npoll == -1) {
Inotify(Notifiable* n, const std::string& bucket_root)
: Notify(n, bucket_root),
+ wfd(inotify_init1(IN_NONBLOCK)),
+ efd(eventfd(0, EFD_NONBLOCK)),
thrd(&Inotify::ev_loop, this)
{
- wfd = inotify_init1(IN_NONBLOCK);
if (wfd == -1) {
std::cerr << fmt::format("{} inotify_init1 failed with {}", __func__, wfd) << std::endl;
exit(1);
}
- efd = eventfd(0, EFD_NONBLOCK);
+ if (efd == -1) {
+ std::cerr << fmt::format("{} eventfd failed", __func__) << std::endl;
+ exit(1);
+ }
}
void signal_shutdown() {
}
virtual ~Inotify() {
- shutdown = true;
+ shutdown.store(true, std::memory_order_release);
signal_shutdown();
thrd.join();
+ close(wfd);
+ close(efd);
}
};
#endif /* linux */