Move Diskstream ownership to Track, so that Session no longer holds lists of Diskstre...
[ardour.git] / libs / ardour / butler.cc
index 05ac7316ec1af3f9b2a7e9e53e16ccc140a9bb59..35fb6bcc035c9f70bce30a47855fd4824ceea76e 100644 (file)
@@ -28,6 +28,7 @@
 #include "ardour/io.h"
 #include "ardour/midi_diskstream.h"
 #include "ardour/session.h"
+#include "ardour/track.h"
 
 #include "i18n.h"
 
@@ -38,23 +39,26 @@ static float _write_data_rate;
 
 namespace ARDOUR {
 
-Butler::Butler(Session* s)
-       : session(s)
+Butler::Butler(Session& s)
+       : SessionHandleRef (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
 Butler::start_thread()
 {
-       const float rate = (float)session->frame_rate();
+       const float rate = (float)_session.frame_rate();
 
        /* size is in Samples, not bytes */
        audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * rate);
@@ -114,6 +118,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 +132,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];
@@ -194,7 +199,7 @@ Butler::thread_work ()
                }
 
                if (transport_work_requested()) {
-                       session->butler_transport_work ();
+                       _session.butler_transport_work ();
                }
 
                disk_work_outstanding = false;
@@ -203,30 +208,33 @@ Butler::thread_work ()
 
                begin = get_microseconds();
 
-               boost::shared_ptr<Session::DiskstreamList> dsl = session->diskstream_list().reader ();
+               boost::shared_ptr<RouteList> rl = _session.get_routes();
 
 //             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->begin(); !transport_work_requested() && should_run && i != rl->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 +246,7 @@ Butler::thread_work ()
 
                }
 
-               if (i != dsl->end()) {
+               if (i != rl->begin() && i != rl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -260,18 +268,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;
 
@@ -285,14 +298,14 @@ Butler::thread_work ()
                        }
                }
 
-               if (err && session->actively_recording()) {
+               if (err && _session.actively_recording()) {
                        /* stop the transport and try to catch as much possible
                           captured state as we can.
                        */
-                       session->request_stop ();
+                       _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;
                }
@@ -304,15 +317,15 @@ Butler::thread_work ()
                if (compute_io) {
                        // there are no apparent users for this calculation?
                        end = get_microseconds();
-                       if(end-begin > 0) {
-                       _write_data_rate = (float) bytes / (float) (end - begin);
+                       if (end - begin > 0) {
+                               _write_data_rate = (float) bytes / (float) (end - begin);
                        } else {
-                       _write_data_rate = 0; // Well, infinity would be better
+                               _write_data_rate = 0; // Well, infinity would be better
                        }
                }
 
                if (!disk_work_outstanding) {
-                       session->refresh_disk_space ();
+                       _session.refresh_disk_space ();
                }
 
 
@@ -329,6 +342,8 @@ Butler::thread_work ()
 
                        paused.signal();
                }
+
+               empty_pool_trash ();
        }
 
        pthread_exit_pbd (0);
@@ -392,4 +407,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
+