Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[ardour.git] / libs / ardour / session_butler.cc
index 6509a783bb8ad8481050480cea09241087001c81..148ff9273993cf50fbf9dd7b6d358ccdc1c6d013 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <algorithm>
@@ -104,10 +103,12 @@ Session::start_butler_thread ()
 void
 Session::terminate_butler_thread ()
 {
-       void* status;
-       char c = ButlerRequest::Quit;
-       ::write (butler_request_pipe[1], &c, 1);
-       pthread_join (butler_thread, &status);
+       if (butler_thread) {
+               void* status;
+               char c = ButlerRequest::Quit;
+               ::write (butler_request_pipe[1], &c, 1);
+               pthread_join (butler_thread, &status);
+       }
 }
 
 void
@@ -157,8 +158,6 @@ Session::_butler_thread_work (void* arg)
        return 0;
 }
 
-#define transport_work_requested() g_atomic_int_get(&butler_should_do_transport_work)
-
 void *
 Session::butler_thread_work ()
 {
@@ -187,7 +186,7 @@ Session::butler_thread_work ()
                }
 
                if (pfd[0].revents & ~POLLIN) {
-                       error << _("Error on butler thread request pipe") << endmsg;
+                       error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
                        break;
                }
                
@@ -234,10 +233,6 @@ Session::butler_thread_work ()
                        }
                }
 
-               //for (i = diskstreams.begin(); i != diskstreams.end(); ++i) {
-                       // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
-               //}
-
                if (transport_work_requested()) {
                        butler_transport_work ();
                }
@@ -248,11 +243,24 @@ Session::butler_thread_work ()
 
                gettimeofday (&begin, 0);
 
-               Glib::RWLock::ReaderLock dsm (diskstream_lock);
-               
-               for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != diskstreams.end(); ++i) {
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
+
+//             for (i = dsl->begin(); i != dsl->end(); ++i) {
+//                     cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
+//             }
 
-                       Diskstream* const ds = *i;
+               for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
+
+                       boost::shared_ptr<Diskstream> ds = *i;
+
+                       /* don't read inactive tracks */
+
+                       /*IO* io = ds->io();
+                       
+                       if (ds->io() && !ds->io()->active()) {
+                               cerr << "Skip inactive diskstream " << ds->io()->name() << endl;
+                               continue;
+                       }*/
 
                        switch (ds->do_refill ()) {
                        case 0:
@@ -271,7 +279,7 @@ Session::butler_thread_work ()
 
                }
 
-               if (i != diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -293,8 +301,11 @@ Session::butler_thread_work ()
                compute_io = true;
                gettimeofday (&begin, 0);
 
-               for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != diskstreams.end(); ++i) {
+               for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
                        // cerr << "write behind for " << (*i)->name () << endl;
+
+                       /* note that we still try to flush diskstreams attached to inactive routes
+                        */
                        
                        switch ((*i)->do_flush (Session::ButlerContext)) {
                        case 0:
@@ -322,7 +333,7 @@ Session::butler_thread_work ()
                        request_stop ();
                }
 
-               if (i != diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -349,7 +360,7 @@ Session::butler_thread_work ()
                        Glib::Mutex::Lock lm (butler_request_lock);
 
                        if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
-//                             for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
+//                             for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
 //                                     cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
 //                             }
 
@@ -388,8 +399,8 @@ Session::overwrite_some_buffers (Diskstream* ds)
 
        } else {
 
-               Glib::RWLock::ReaderLock dm (diskstream_lock);
-               for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                        (*i)->set_pending_overwrite (true);
                }
        }