Skip to content

Linux + iPhone Continuity / iMessage / SMS

Code review feedback

sfreeman42293c208f0518a
diff --git a/src/core/include/tether/event_loop.hpp b/src/core/include/tether/event_loop.hpp
index 6f8d164..720162c 100644
--- a/src/core/include/tether/event_loop.hpp
+++ b/src/core/include/tether/event_loop.hpp
@@ -42,6 +42,7 @@ namespace tether {
int post_fd_ = -1;
std::atomic<bool> running_;
std::map<int, Callback> callbacks_;
+ std::mutex callbacks_mutex_;
std::mutex post_mutex_;
std::vector<std::function<void()>> posted_;
};
diff --git a/src/core/src/event_loop.cpp b/src/core/src/event_loop.cpp
index 430188f..7498b8c 100644
--- a/src/core/src/event_loop.cpp
+++ b/src/core/src/event_loop.cpp
@@ -69,6 +69,7 @@ namespace tether {
debug::log(ERR, "Failed to add fd to epoll: {}", fd);
return false;
}
+ std::lock_guard<std::mutex> lock(callbacks_mutex_);
callbacks_[fd] = std::move(cb);
return true;
}
@@ -77,6 +78,7 @@ namespace tether {
if (epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) < 0) {
return false;
}
+ std::lock_guard<std::mutex> lock(callbacks_mutex_);
callbacks_.erase(fd);
return true;
}
@@ -97,12 +99,16 @@ namespace tether {
for (int i = 0; i < n; ++i) {
int fd = events[i].data.fd;
- auto it = callbacks_.find(fd);
- if (it != callbacks_.end()) {
- // Copy the callback to ensure it stays alive even if removeFd is called inside it
- auto cb = it->second;
- cb(fd);
+ Callback cb;
+ {
+ std::lock_guard<std::mutex> lock(callbacks_mutex_);
+ auto it = callbacks_.find(fd);
+ if (it == callbacks_.end())
+ continue;
+ cb = it->second;
}
+ // Copy the callback to ensure it stays alive even if removeFd is called inside it.
+ cb(fd);
}
}
}