mail[Wesnoth-commits] r44814 - in /trunk/src/ana: api/ana.hpp src/asio_client.cpp src/asio_client.hpp src/asio_server.cpp src/asio_server.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by billybiset on July 28, 2010 - 21:14:
Author: billynux
Date: Wed Jul 28 21:14:42 2010
New Revision: 44814

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44814&view=rev
Log:
Fix a memory leak in ANA while trying to run the io_service in many different 
threads and updated the relevant documentation in the code.

Modified:
    trunk/src/ana/api/ana.hpp
    trunk/src/ana/src/asio_client.cpp
    trunk/src/ana/src/asio_client.hpp
    trunk/src/ana/src/asio_server.cpp
    trunk/src/ana/src/asio_server.hpp

Modified: trunk/src/ana/api/ana.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/api/ana.hpp?rev=44814&r1=44813&r2=44814&view=diff
==============================================================================
--- trunk/src/ana/api/ana.hpp (original)
+++ trunk/src/ana/api/ana.hpp Wed Jul 28 21:14:42 2010
@@ -456,6 +456,18 @@
 
         /**
          * Start the server on a given port.
+         *
+         * Each time you call this method, a new thread will be started on 
the io_service object
+         * from asio. This means that it is possible to have multiple 
threads running the service,
+         * thus more threads will be able to run the handlers you implement.
+         *
+         * The drawback is, however, that if you run the service on multiple 
threads, then you must
+         * be aware that the execution of your handlers may occur 
concurrently and thus you have to
+         * prevent all of the troubles arising from this concurrency.
+         *
+         * Note that if you just call this method once, then you ensure 
mutual exclusion between
+         * your handlers, just make sure you don't block waiting for a call 
to a handler from one
+         * of your handlers, otherwise you'll always get a deadlock.
          *
          * @param port : The port to be used for the server incoming 
connections.
          *               The port shouldn't be currently occupied.
@@ -652,7 +664,21 @@
                                            std::string user_name = "",
                                            std::string password = "") = 0;
 
-        /** Run the client listener, starts listening for incoming messages. 
*/
+        /**
+         * Run the client listener, starts listening for incoming messages.
+         *
+         * Each time you call this method, a new thread will be started on 
the io_service object
+         * from asio. This means that it is possible to have multiple 
threads running the service,
+         * thus more threads will be able to run the handlers you implement.
+         *
+         * The drawback is, however, that if you run the service on multiple 
threads, then you must
+         * be aware that the execution of your handlers may occur 
concurrently and thus you have to
+         * prevent all of the troubles arising from this concurrency.
+         *
+         * Note that if you just call this method once, then you ensure 
mutual exclusion between
+         * your handlers, just make sure you don't block waiting for a call 
to a handler from one
+         * of your handlers, otherwise you'll always get a deadlock.
+         */
         virtual void run() = 0;
 
         /**

Modified: trunk/src/ana/src/asio_client.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_client.cpp?rev=44814&r1=44813&r2=44814&view=diff
==============================================================================
--- trunk/src/ana/src/asio_client.cpp (original)
+++ trunk/src/ana/src/asio_client.cpp Wed Jul 28 21:14:42 2010
@@ -44,7 +44,7 @@
 asio_client::asio_client(ana::address address, ana::port pt) :
     asio_listener(),
     io_service_(),
-    io_thread_(),
+    io_threads_(),
     work_( io_service_ ),
     socket_(io_service_),
     address_(address),
@@ -61,7 +61,16 @@
 {
     stop_logging();
     disconnect_listener();
-    io_thread_.join();
+
+    std::list< boost::thread* >::iterator it;
+
+    it = io_threads_.begin();
+
+    while (it != io_threads_.end())
+    {
+        (*it)->join();
+        it = io_threads_.erase( it );
+    }
 }
 
 ana::client* ana::client::create(ana::address address, ana::port pt)
@@ -71,7 +80,8 @@
 
 void asio_client::run()
 {
-    io_thread_ = boost::thread( boost::bind( &boost::asio::io_service::run, 
&io_service_) );
+    io_threads_.push_back(
+                new boost::thread( boost::bind( 
&boost::asio::io_service::run, &io_service_) ) );
 }
 
 void asio_client::handle_proxy_connection(const boost::system::error_code& 
ec,

Modified: trunk/src/ana/src/asio_client.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_client.hpp?rev=44814&r1=44813&r2=44814&view=diff
==============================================================================
--- trunk/src/ana/src/asio_client.hpp (original)
+++ trunk/src/ana/src/asio_client.hpp Wed Jul 28 21:14:42 2010
@@ -32,6 +32,8 @@
 
 #ifndef ASIO_CLIENT_HPP
 #define ASIO_CLIENT_HPP
+
+#include <list>
 
 #include <boost/asio.hpp>
 #include <memory>
@@ -121,7 +123,7 @@
 
         /*attr*/
         boost::asio::io_service       io_service_;
-        boost::thread                 io_thread_;
+        std::list<boost::thread*>     io_threads_;
         boost::asio::io_service::work work_;
 
         tcp::socket               socket_;

Modified: trunk/src/ana/src/asio_server.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_server.cpp?rev=44814&r1=44813&r2=44814&view=diff
==============================================================================
--- trunk/src/ana/src/asio_server.cpp (original)
+++ trunk/src/ana/src/asio_server.cpp Wed Jul 28 21:14:42 2010
@@ -46,7 +46,7 @@
 asio_server::asio_server() :
     io_service_(),
     work_( io_service_ ),
-    io_thread_(),
+    io_threads_(),
     acceptor_( NULL ),
     client_proxies_(),
     listening_(false),
@@ -61,7 +61,16 @@
 asio_server::~asio_server()
 {
     io_service_.stop();
-    io_thread_.join();
+
+    std::list< boost::thread* >::iterator it;
+
+    it = io_threads_.begin();
+
+    while (it != io_threads_.end())
+    {
+        (*it)->join();
+        it = io_threads_.erase( it );
+    }
 
     /* Since the asio_client_proxy destuctor removes the client from 
client_proxies_
        I'll just delete every proxy from a different list. */
@@ -100,7 +109,8 @@
 
     run_listener( );
 
-    io_thread_ = boost::thread( boost::bind( &boost::asio::io_service::run, 
&io_service_) );
+    io_threads_.push_back(
+            new boost::thread( boost::bind( &boost::asio::io_service::run, 
&io_service_) ) );
 }
 
 void asio_server::async_accept( connection_handler* handler )
@@ -370,7 +380,16 @@
 void asio_server::disconnect()
 {
     io_service_.stop();
-    io_thread_.join();
+
+    std::list< boost::thread* >::iterator it;
+
+    it = io_threads_.begin();
+
+    while (it != io_threads_.end())
+    {
+        (*it)->join();
+        it = io_threads_.erase( it );
+    }
 
     for (std::list<client_proxy*>::iterator it = client_proxies_.begin();
          it != client_proxies_.end();

Modified: trunk/src/ana/src/asio_server.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_server.hpp?rev=44814&r1=44813&r2=44814&view=diff
==============================================================================
--- trunk/src/ana/src/asio_server.hpp (original)
+++ trunk/src/ana/src/asio_server.hpp Wed Jul 28 21:14:42 2010
@@ -32,6 +32,8 @@
 
 #ifndef ASIO_SERVER_HPP
 #define ASIO_SERVER_HPP
+
+#include <list>
 
 #include <boost/asio.hpp>
 #include <boost/interprocess/smart_ptr/unique_ptr.hpp>
@@ -173,7 +175,7 @@
 
         boost::asio::io_service       io_service_;
         boost::asio::io_service::work work_;
-        boost::thread                 io_thread_;
+        std::list<boost::thread*>     io_threads_;
         std::auto_ptr<tcp::acceptor>  acceptor_;
         std::list<client_proxy*>      client_proxies_;
         bool                          listening_;




Related Messages


Powered by MHonArc, Updated Wed Jul 28 21:20:15 2010