Fix loss of host's encoding threads when servers list changes.
authorCarl Hetherington <cth@carlh.net>
Wed, 19 Aug 2015 18:24:03 +0000 (19:24 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 19 Aug 2015 18:24:03 +0000 (19:24 +0100)
src/lib/encoder.cc
src/lib/encoder.h

index 08f4e66d482bbf4ec8f2c437cdf5a9d6cd3c83f1..195b10f443bf65f66508dd885bb74a17747de4c8 100644 (file)
@@ -71,32 +71,9 @@ Encoder::~Encoder ()
        terminate_threads ();
 }
 
-/** Add a worker thread for a each thread on a remote server.  Caller must hold
- *  a lock on _mutex, or know that one is not currently required to
- *  safely modify _threads.
- */
-void
-Encoder::add_worker_threads (ServerDescription d)
-{
-       LOG_GENERAL (N_("Adding %1 worker threads for remote %2"), d.threads(), d.host_name ());
-       for (int i = 0; i < d.threads(); ++i) {
-               _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, d)));
-       }
-
-       _writer->set_encoder_threads (_threads.size ());
-}
-
 void
 Encoder::begin ()
 {
-       if (!Config::instance()->only_servers_encode ()) {
-               for (int i = 0; i < Config::instance()->num_local_encoding_threads (); ++i) {
-                       _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, optional<ServerDescription> ())));
-               }
-       }
-
-       _writer->set_encoder_threads (_threads.size ());
-
        if (!ServerFinder::instance()->disabled ()) {
                _server_found_connection = ServerFinder::instance()->ServersListChanged.connect (boost::bind (&Encoder::servers_list_changed, this));
        }
@@ -292,6 +269,12 @@ void
 Encoder::encoder_thread (optional<ServerDescription> server)
 try
 {
+       if (server) {
+               LOG_TIMING ("start-encoder-thread thread=%1 server=%2", boost::this_thread::get_id (), server->host_name ());
+       } else {
+               LOG_TIMING ("start-encoder-thread thread=%1 server=localhost", boost::this_thread::get_id ());
+       }
+
        /* Number of seconds that we currently wait between attempts
           to connect to the server; not relevant for localhost
           encodings.
@@ -380,7 +363,21 @@ void
 Encoder::servers_list_changed ()
 {
        terminate_threads ();
+
+       /* XXX: could re-use threads */
+
+       if (!Config::instance()->only_servers_encode ()) {
+               for (int i = 0; i < Config::instance()->num_local_encoding_threads (); ++i) {
+                       _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, optional<ServerDescription> ())));
+               }
+       }
+
        BOOST_FOREACH (ServerDescription i, ServerFinder::instance()->servers ()) {
-               add_worker_threads (i);
+               LOG_GENERAL (N_("Adding %1 worker threads for remote %2"), i.threads(), i.host_name ());
+               for (int j = 0; j < i.threads(); ++j) {
+                       _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, i)));
+               }
        }
+
+       _writer->set_encoder_threads (_threads.size ());
 }
index 06a963a21f1dfeea16e1043734340ae8d28d0506..e9daab52151bb9a2efcabe287f1e2ea14f35b193 100644 (file)
@@ -77,7 +77,6 @@ private:
 
        void encoder_thread (boost::optional<ServerDescription>);
        void terminate_threads ();
-       void add_worker_threads (ServerDescription);
        void servers_list_changed ();
 
        /** Film that we are encoding */