rationale pathways that add notes to Sequence<T> so that there is only final insertio...
[ardour.git] / libs / ardour / butler.cc
index 2fd4c1d9c1f313f515a567ff6f13db2d0bd5dba6..9847d55e40a5455c2e691c191dac9816ddc4ac85 100644 (file)
@@ -28,6 +28,8 @@
 #include "ardour/io.h"
 #include "ardour/midi_diskstream.h"
 #include "ardour/session.h"
+#include "ardour/track.h"
+#include "ardour/auditioner.h"
 
 #include "i18n.h"
 
@@ -43,12 +45,15 @@ Butler::Butler(Session& s)
        , thread(0)
        , audio_dstream_buffer_size(0)
        , midi_dstream_buffer_size(0)
+       , pool_trash(16)
 {
        g_atomic_int_set(&should_do_transport_work, 0);
+       SessionEvent::pool->set_trash (&pool_trash);
 }
 
 Butler::~Butler()
 {
+       terminate_thread ();
 }
 
 int
@@ -114,6 +119,7 @@ void *
 Butler::_thread_work (void* arg)
 {
        SessionEvent::create_per_thread_pool ("butler events", 64);
+       pthread_set_name (X_("butler"));
        return ((Butler *) arg)->thread_work ();
 }
 
@@ -127,7 +133,7 @@ Butler::thread_work ()
 
        struct pollfd pfd[1];
        bool disk_work_outstanding = false;
-       Session::DiskstreamList::iterator i;
+       RouteList::iterator i;
 
        while (true) {
                pfd[0].fd = request_pipe[0];
@@ -203,30 +209,36 @@ Butler::thread_work ()
 
                begin = get_microseconds();
 
-               boost::shared_ptr<Session::DiskstreamList> dsl = _session.diskstream_list().reader ();
+               boost::shared_ptr<RouteList> rl = _session.get_routes();
+
+               RouteList rl_with_auditioner = *rl;
+               rl_with_auditioner.push_back (_session.the_auditioner());
 
 //             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() && should_run && i != dsl->end(); ++i) {
+               for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++i) {
 
-                       boost::shared_ptr<Diskstream> ds = *i;
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                       if (!tr) {
+                               continue;
+                       }
 
                        /* don't read inactive tracks */
 
-                       boost::shared_ptr<IO> io = ds->io();
+                       boost::shared_ptr<IO> io = tr->input ();
 
                        if (io && !io->active()) {
                                continue;
                        }
 
-                       switch (ds->do_refill ()) {
+                       switch (tr->do_refill ()) {
                        case 0:
-                               bytes += ds->read_data_count();
+                               bytes += tr->read_data_count();
                                break;
                        case 1:
-                               bytes += ds->read_data_count();
+                               bytes += tr->read_data_count();
                                disk_work_outstanding = true;
                                break;
 
@@ -238,7 +250,7 @@ Butler::thread_work ()
 
                }
 
-               if (i != dsl->end()) {
+               if (i != rl_with_auditioner.begin() && i != rl_with_auditioner.end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -260,18 +272,23 @@ Butler::thread_work ()
                compute_io = true;
                begin = get_microseconds();
 
-               for (i = dsl->begin(); !transport_work_requested() && should_run && i != dsl->end(); ++i) {
+               for (i = rl->begin(); !transport_work_requested() && should_run && i != rl->end(); ++i) {
                        // cerr << "write behind for " << (*i)->name () << endl;
 
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                       if (!tr) {
+                               continue;
+                       }
+                       
                        /* note that we still try to flush diskstreams attached to inactive routes
                         */
 
-                       switch ((*i)->do_flush (ButlerContext)) {
+                       switch (tr->do_flush (ButlerContext)) {
                        case 0:
-                               bytes += (*i)->write_data_count();
+                               bytes += tr->write_data_count();
                                break;
                        case 1:
-                               bytes += (*i)->write_data_count();
+                               bytes += tr->write_data_count();
                                disk_work_outstanding = true;
                                break;
 
@@ -292,7 +309,7 @@ Butler::thread_work ()
                        _session.request_stop ();
                }
 
-               if (i != dsl->end()) {
+               if (i != rl->begin() && i != rl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -329,6 +346,8 @@ Butler::thread_work ()
 
                        paused.signal();
                }
+
+               empty_pool_trash ();
        }
 
        pthread_exit_pbd (0);
@@ -392,4 +411,42 @@ Butler::write_data_rate () const
        return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
 }
 
+void
+Butler::empty_pool_trash ()
+{
+       /* look in the trash, deleting empty pools until we come to one that is not empty */
+       
+       RingBuffer<CrossThreadPool*>::rw_vector vec;
+       pool_trash.get_read_vector (&vec);
+
+       guint deleted = 0;
+       
+       for (int i = 0; i < 2; ++i) {
+               for (guint j = 0; j < vec.len[i]; ++j) {
+                       if (vec.buf[i][j]->empty()) {
+                               delete vec.buf[i][j];
+                               ++deleted;
+                       } else {
+                               /* found a non-empty pool, so stop deleting */
+                               if (deleted) {
+                                       pool_trash.increment_read_idx (deleted);
+                               }
+                               return;
+                       }
+               }
+       }
+
+       if (deleted) {
+               pool_trash.increment_read_idx (deleted);
+       }
+}
+
+void
+Butler::drop_references ()
+{
+       SessionEvent::pool->set_trash (0);
+}
+
+
 } // namespace ARDOUR
+