From 2fcb8ce4599ab94178aae1ac9f9ff800fd25cd0a Mon Sep 17 00:00:00 2001 From: Alberto Compagno Date: Wed, 23 Oct 2019 15:41:59 +0200 Subject: [HICN-354] Fixed bug on raaqm when reassemblying packets Moved rescheduleOnIOService in the header file to allow its usage together with inheritance Change-Id: I15e4b92535e1478d0dd09828d2d13e2b77e000b3 Signed-off-by: Alberto Compagno --- .../hicn/transport/interfaces/socket_producer.h | 41 +++++++++++++++++----- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'libtransport/src/hicn/transport/interfaces/socket_producer.h') diff --git a/libtransport/src/hicn/transport/interfaces/socket_producer.h b/libtransport/src/hicn/transport/interfaces/socket_producer.h index 5c617d761..709a2582b 100644 --- a/libtransport/src/hicn/transport/interfaces/socket_producer.h +++ b/libtransport/src/hicn/transport/interfaces/socket_producer.h @@ -160,15 +160,6 @@ class ProducerSocket : public Socket, virtual int getSocketOption(int socket_option_key, std::string &socket_option_value); - template - int rescheduleOnIOService(int socket_option_key, arg2 socket_option_value, - Lambda lambda_func); - - template - int rescheduleOnIOServiceWithReference(int socket_option_key, - arg2 &socket_option_value, - Lambda lambda_func); - protected: // Threads std::thread listening_thread_; @@ -215,6 +206,38 @@ class ProducerSocket : public Socket, ProducerContentCallback on_content_produced_; + // If the thread calling lambda_func is not the same of io_service, this + // function reschedule the function on it + template + int rescheduleOnIOService(int socket_option_key, arg2 socket_option_value, + Lambda lambda_func) { + // To enforce type check + std::function func = lambda_func; + int result = SOCKET_OPTION_SET; + if (listening_thread_.joinable() && + std::this_thread::get_id() != listening_thread_.get_id()) { + std::mutex mtx; + /* Condition variable for the wait */ + std::condition_variable cv; + bool done = false; + io_service_.dispatch([&socket_option_key, &socket_option_value, + &mtx, &cv, &result, &done, &func]() { + std::unique_lock lck(mtx); + done = true; + result = func(socket_option_key, socket_option_value); + cv.notify_all(); + }); + std::unique_lock lck(mtx); + if (!done) { + cv.wait(lck); + } + } else { + result = func(socket_option_key, socket_option_value); + } + + return result; + } + private: void listen(); -- cgit 1.2.3-korg