tool: Use a different timerfd per each dp 58/258558/2
authorSeonah Moon <seonah1.moon@samsung.com>
Thu, 20 May 2021 07:03:53 +0000 (16:03 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Thu, 20 May 2021 07:14:49 +0000 (16:14 +0900)
Change-Id: Ic0d922609c98bd430f557b525198b609f0b0ccfa

tool/tool_run.cpp

index ab7632b..83c25a5 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <limits.h>
+#include <map>
 
 #define RESET_COLOR "\e[m"
 #define MAKE_RED "\e[31m"
@@ -23,7 +24,6 @@
 extern int interrupt_flag;
 
 static int epollfd = 0;
-static int timerfd = 0;
 static FILE *log_file = NULL;
 
 static struct {
@@ -60,8 +60,9 @@ static struct {
        .is_sec = false,
 };
 
+static std::map<vine_dp_h, int> timerfds;
 static int send_message(vine_dp_h dp);
-static void _stop_message_timer();
+static void _stop_message_timer(vine_dp_h dp);
 
 static void __print_received_data(unsigned char *buf, size_t len)
 {
@@ -86,7 +87,7 @@ static void __received_cb(vine_dp_h dp, size_t received_len, void *user_data)
 static void __terminated_cb(vine_dp_h dp, void *user_data)
 {
        printf("peer is terminated.\n");
-       _stop_message_timer();
+       _stop_message_timer(dp);
 }
 
 static void __opened_cb(vine_dp_h dp, vine_error_e result, void *user_data)
@@ -221,7 +222,7 @@ static void _start_message_timer(vine_dp_h dp, int sec)
        if (sec <= 0)
                return;
 
-       timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
+       int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
        if (timerfd == -1)
                return;
 
@@ -235,26 +236,44 @@ static void _start_message_timer(vine_dp_h dp, int sec)
                return;
        }
 
+       timerfds.insert(std::make_pair(dp, timerfd));
        struct epoll_event ev;
        ev.events = EPOLLIN;
        ev.data.ptr = (void *)dp;
        epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &ev);
 }
 
-static void _stop_message_timer()
+static void _stop_message_timer(vine_dp_h dp)
 {
-       if (timerfd <= 0)
+       auto it = timerfds.find(dp);
+       if (it == timerfds.end())
                return;
 
+       int timerfd = it->second;
+       if (timerfd <= 0) {
+               timerfds.erase(it);
+               return;
+       }
+
        epoll_ctl(epollfd, EPOLL_CTL_DEL, timerfd, NULL);
        close(timerfd);
-       timerfd = 0;
+       timerfds.erase(it);
+}
+
+static void _stop_message_timer_all()
+{
+       for (auto &it : timerfds) {
+               if (it.second <= 0)
+                       continue;
+               close(it.second);
+       }
+       timerfds.clear();
 }
 
 static void _message_timer_handler(void *user_data)
 {
-       vine_dp_h dp = (vine_dp_h)user_data;
-       _stop_message_timer();
+       vine_dp_h dp = (vine_dp_h) user_data;
+       _stop_message_timer(dp);
        send_message(dp);
 }
 
@@ -307,7 +326,6 @@ static int _send_message(vine_dp_h dp)
 
        int ret = vine_dp_send(dp, buf, len);
        printf("%s to send a message.\n", ret == VINE_ERROR_NONE ? "Succeeded" : "Failed");
-
        _start_message_timer(dp, vine_configs.interval);
        return ret;
 }
@@ -340,8 +358,7 @@ static void deinit()
 {
        tool_config_deinit();
 
-       if (timerfd)
-               _stop_message_timer();
+       _stop_message_timer_all();
 
        if (log_file) {
                fclose(log_file);