PluginInfo::type added to copy constructor. But why is the copy constructor defined...
[ardour.git] / libs / ardour / session_butler.cc
index 832284901c12580fd0e8e5a406206429733c4ae2..9d4ee5678b68cf62f79bd06f40db9a8998ba63c2 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>
@@ -30,6 +29,7 @@
 
 #include <pbd/error.h>
 #include <pbd/pthread_utils.h>
+#include <pbd/stacktrace.h>
 
 #include <ardour/configuration.h>
 #include <ardour/audioengine.h>
@@ -104,10 +104,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
@@ -129,6 +131,7 @@ Session::summon_butler ()
 {
        char c = ButlerRequest::Run;
        ::write (butler_request_pipe[1], &c, 1);
+       // PBD::stacktrace (cerr);
 }
 
 void
@@ -152,20 +155,19 @@ Session::wait_till_butler_finished ()
 void *
 Session::_butler_thread_work (void* arg)
 {
-       PBD::ThreadCreated (pthread_self(), X_("Butler"));
+       PBD::notify_gui_about_thread_creation (pthread_self(), X_("Butler"));
        return ((Session *) arg)->butler_thread_work ();
        return 0;
 }
 
-#define transport_work_requested() g_atomic_int_get(&butler_should_do_transport_work)
-
 void *
 Session::butler_thread_work ()
 {
        uint32_t err = 0;
        int32_t bytes;
        bool compute_io;
-       struct timeval begin, end;
+       microseconds_t begin, end;
+
        struct pollfd pfd[1];
        bool disk_work_outstanding = false;
        DiskstreamList::iterator i;
@@ -234,10 +236,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 ();
                }
@@ -246,14 +244,26 @@ Session::butler_thread_work ()
                bytes = 0;
                compute_io = true;
 
-               gettimeofday (&begin, 0);
+               begin = get_microseconds();
 
                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;
+//             }
+
                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 (io && !io->active()) {
+                               continue;
+                       }
+
                        switch (ds->do_refill ()) {
                        case 0:
                                bytes += ds->read_data_count();
@@ -281,20 +291,22 @@ Session::butler_thread_work ()
                }
 
                if (compute_io) {
-                       gettimeofday (&end, 0);
-                       
-                       double b = begin.tv_sec  + (begin.tv_usec/1000000.0);
-                       double e = end.tv_sec + (end.tv_usec / 1000000.0);
-                       
-                       _read_data_rate = bytes / (e - b);
+                       end = get_microseconds(); 
+                       if(end-begin > 0) {
+                       _read_data_rate = (float) bytes / (float) (end - begin);
+                       } else { _read_data_rate = 0; // infinity better
+                        }
                }
 
                bytes = 0;
                compute_io = true;
-               gettimeofday (&begin, 0);
+               begin = get_microseconds();
 
                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:
@@ -332,12 +344,13 @@ Session::butler_thread_work ()
                }
 
                if (compute_io) {
-                       gettimeofday (&end, 0);
-                       
-                       double b = begin.tv_sec  + (begin.tv_usec/1000000.0);
-                       double e = end.tv_sec + (end.tv_usec / 1000000.0);
-                       
-                       _write_data_rate = bytes / (e - b);
+                       // there are no apparent users for this calculation?
+                       end = get_microseconds();
+                       if(end-begin > 0) {
+                       _write_data_rate = (float) bytes / (float) (end - begin);
+                       } else {
+                       _write_data_rate = 0; // Well, infinity would be better
+                       }
                }
                
                if (!disk_work_outstanding) {
@@ -404,7 +417,7 @@ Session::read_data_rate () const
        /* disk i/o in excess of 10000MB/sec indicate the buffer cache
           in action. ignore it.
        */
-       return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
+       return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
 }
 
 float
@@ -413,7 +426,7 @@ Session::write_data_rate () const
        /* disk i/o in excess of 10000MB/sec indicate the buffer cache
           in action. ignore it.
        */
-       return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
+       return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
 }
 
 uint32_t