chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4029004081
##########
test/bthread_fd_unittest.cpp:
##########
@@ -411,39 +411,62 @@ TEST(FDTest, add_existing_fd) {
#endif
}
+struct EpollWaitArg {
+ int epfd;
+ butil::atomic<bool> done{false};
+ int result = 0;
+ int error = 0;
+};
+
void* epoll_waiter(void* arg) {
+ EpollWaitArg* a = static_cast<EpollWaitArg*>(arg);
#if defined(OS_LINUX)
epoll_event e;
- if (1 == epoll_wait((int)(intptr_t)arg, &e, 1, -1)) {
- std::cout << e.events << std::endl;
- }
+ a->result = epoll_wait(a->epfd, &e, 1, 10000);
#elif defined(OS_MACOSX)
struct kevent e;
- if (1 == kevent((int)(intptr_t)arg, nullptr, 0, &e, 1, nullptr)) {
- std::cout << e.flags << std::endl;
- }
+ timespec timeout = {10, 0};
+ a->result = kevent(a->epfd, nullptr, 0, &e, 1, &timeout);
#endif
- std::cout << pthread_self() << " quits" << std::endl;
+ a->error = errno;
+ a->done.store(true, butil::memory_order_release);
return nullptr;
}
TEST(FDTest, interrupt_pthread) {
#if defined(OS_LINUX)
- const int epfd = epoll_create(1024);
+ butil::fd_guard epfd(epoll_create(1024));
#elif defined(OS_MACOSX)
- const int epfd = kqueue();
+ butil::fd_guard epfd(kqueue());
#endif
- pthread_t th, th2;
- ASSERT_EQ(0, pthread_create(&th, nullptr, epoll_waiter,
(void*)(intptr_t)epfd));
- ASSERT_EQ(0, pthread_create(&th2, nullptr, epoll_waiter,
(void*)(intptr_t)epfd));
- bthread_usleep(100000L);
- std::cout << "wake up " << th << std::endl;
- bthread::interrupt_pthread(th);
- bthread_usleep(100000L);
- std::cout << "wake up " << th2 << std::endl;
- bthread::interrupt_pthread(th2);
- pthread_join(th, nullptr);
- pthread_join(th2, nullptr);
+ ASSERT_GE(epfd, 0);
+ EpollWaitArg args[2];
+ pthread_t threads[2];
+ size_t started = 0;
+ for (; started < ARRAY_SIZE(threads); ++started) {
+ args[started].epfd = epfd;
+ int rc = pthread_create(&threads[started], nullptr,
+ epoll_waiter, &args[started]);
+ EXPECT_EQ(0, rc);
+ if (rc != 0) {
+ break;
+ }
+ }
+ int64_t deadline = butil::cpuwide_time_us() + 15000000L;
+ for (size_t i = 0; i < started; ++i) {
+ // Signals are not persistent. Retry until the syscall observes one;
+ // keep the pthread joinable until all signalling is finished.
+ while (!args[i].done.load(butil::memory_order_acquire) &&
+ butil::cpuwide_time_us() < deadline) {
+ EXPECT_EQ(0, bthread::interrupt_pthread(threads[i]));
+ bthread_usleep(1000);
Review Comment:
Fixed in a62c47d217b023f12b75bf2f9319c16da98f7d11.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]