#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
+
+#ifndef PLATFORM_WINDOWS
#include <poll.h>
+#endif
+
#include "pbd/error.h"
#include "pbd/pthread_utils.h"
#include "ardour/butler.h"
-#include "ardour/crossfade.h"
#include "ardour/io.h"
#include "ardour/midi_diskstream.h"
#include "ardour/session.h"
using namespace PBD;
-static float _read_data_rate;
-static float _write_data_rate;
-
namespace ARDOUR {
Butler::Butler(Session& s)
: SessionHandleRef (s)
- , thread(0)
+ , thread()
+ , have_thread (false)
, audio_dstream_capture_buffer_size(0)
, audio_dstream_playback_buffer_size(0)
, midi_dstream_buffer_size(0)
}
}
+#ifndef PLATFORM_WINDOWS
int
-Butler::start_thread()
+Butler::setup_request_pipe ()
{
- const float rate = (float)_session.frame_rate();
-
- /* size is in Samples, not bytes */
- audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
- audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
-
- /* size is in bytes
- * XXX: Jack needs to tell us the MIDI buffer size
- * (i.e. how many MIDI bytes we might see in a cycle)
- */
- midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
-
- MidiDiskstream::set_readahead_frames ((nframes_t)(Config->get_midi_readahead() * rate));
-
- Crossfade::set_buffer_size (audio_dstream_playback_buffer_size);
-
- should_run = false;
-
if (pipe (request_pipe)) {
error << string_compose(_("Cannot create transport request signal pipe (%1)"),
strerror (errno)) << endmsg;
strerror (errno)) << endmsg;
return -1;
}
+ return 0;
+}
+#endif
+
+int
+Butler::start_thread()
+{
+ const float rate = (float)_session.frame_rate();
+
+ /* size is in Samples, not bytes */
+ audio_dstream_capture_buffer_size = (uint32_t) floor (Config->get_audio_capture_buffer_seconds() * rate);
+ audio_dstream_playback_buffer_size = (uint32_t) floor (Config->get_audio_playback_buffer_seconds() * rate);
+
+ /* size is in bytes
+ * XXX: Jack needs to tell us the MIDI buffer size
+ * (i.e. how many MIDI bytes we might see in a cycle)
+ */
+ midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * rate);
+
+ MidiDiskstream::set_readahead_frames ((framecnt_t) (Config->get_midi_readahead() * rate));
+
+ should_run = false;
+
+#ifndef PLATFORM_WINDOWS
+ if (setup_request_pipe() != 0) return -1;
+#endif
if (pthread_create_and_store ("disk butler", &thread, _thread_work, this)) {
error << _("Session: could not create butler thread") << endmsg;
}
//pthread_detach (thread);
-
+ have_thread = true;
return 0;
}
void
Butler::terminate_thread ()
{
- if (thread) {
+ if (have_thread) {
void* status;
- const char c = Request::Quit;
- ::write (request_pipe[1], &c, 1);
+ queue_request (Request::Quit);
pthread_join (thread, &status);
}
}
void *
Butler::_thread_work (void* arg)
{
- SessionEvent::create_per_thread_pool ("butler events", 64);
+ SessionEvent::create_per_thread_pool ("butler events", 4096);
pthread_set_name (X_("butler"));
return ((Butler *) arg)->thread_work ();
}
-void *
-Butler::thread_work ()
+bool
+Butler::wait_for_requests ()
{
- uint32_t err = 0;
- int32_t bytes;
- bool compute_io;
- microseconds_t begin, end;
-
+#ifndef PLATFORM_WINDOWS
struct pollfd pfd[1];
- bool disk_work_outstanding = false;
- RouteList::iterator i;
- while (true) {
- pfd[0].fd = request_pipe[0];
- pfd[0].events = POLLIN|POLLERR|POLLHUP;
+ pfd[0].fd = request_pipe[0];
+ pfd[0].events = POLLIN|POLLERR|POLLHUP;
- if (poll (pfd, 1, (disk_work_outstanding ? 0 : -1)) < 0) {
+ while(true) {
+ if (poll (pfd, 1, -1) < 0) {
if (errno == EINTR) {
continue;
}
error << string_compose (_("poll on butler request pipe failed (%1)"),
- strerror (errno))
- << endmsg;
+ strerror (errno))
+ << endmsg;
break;
}
}
if (pfd[0].revents & POLLIN) {
+ return true;
+ }
+ }
+ return false;
+#else
+ m_request_sem.wait ();
+ return true;
+#endif
+}
- char req;
-
- /* empty the pipe of all current requests */
+bool
+Butler::dequeue_request (Request::Type& r)
+{
+#ifndef PLATFORM_WINDOWS
+ char req;
+ size_t nread = ::read (request_pipe[0], &req, sizeof (req));
+ if (nread == 1) {
+ r = (Request::Type) req;
+ return true;
+ } else if (nread == 0) {
+ return false;
+ } else if (errno == EAGAIN) {
+ return false;
+ } else {
+ fatal << _("Error reading from butler request pipe") << endmsg;
+ /*NOTREACHED*/
+ }
+#else
+ r = (Request::Type) m_request_state.get();
+#endif
+ return false;
+}
- while (1) {
- size_t nread = ::read (request_pipe[0], &req, sizeof (req));
- if (nread == 1) {
+ void *
+Butler::thread_work ()
+{
+ uint32_t err = 0;
- switch ((Request::Type) req) {
+ bool disk_work_outstanding = false;
+ RouteList::iterator i;
- case Request::Wake:
- break;
+ while (true) {
+ if(!disk_work_outstanding) {
+ if (wait_for_requests ()) {
+ Request::Type req;
+
+ /* empty the pipe of all current requests */
+#ifdef PLATFORM_WINDOWS
+ dequeue_request (req);
+ {
+#else
+ while(dequeue_request(req)) {
+#endif
+ switch (req) {
case Request::Run:
should_run = true;
break;
case Request::Quit:
- pthread_exit_pbd (0);
+ return 0;
/*NOTREACHED*/
break;
default:
break;
}
-
- } else if (nread == 0) {
- break;
- } else if (errno == EAGAIN) {
- break;
- } else {
- fatal << _("Error reading from butler request pipe") << endmsg;
- /*NOTREACHED*/
}
}
}
+
+restart:
+ disk_work_outstanding = false;
+
if (transport_work_requested()) {
_session.butler_transport_work ();
}
- disk_work_outstanding = false;
- bytes = 0;
- compute_io = true;
-
- begin = get_microseconds();
+ frameoffset_t audition_seek;
+ if (should_run && _session.is_auditioning()
+ && (audition_seek = _session.the_auditioner()->seek_frame()) >= 0) {
+ boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (_session.the_auditioner());
+ tr->seek(audition_seek);
+ _session.the_auditioner()->seek_response(audition_seek);
+ }
boost::shared_ptr<RouteList> rl = _session.get_routes();
for (i = rl_with_auditioner.begin(); !transport_work_requested() && should_run && i != rl_with_auditioner.end(); ++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 = tr->input ();
if (io && !io->active()) {
+ /* don't read inactive tracks */
continue;
}
switch (tr->do_refill ()) {
case 0:
- bytes += tr->read_data_count();
break;
+
case 1:
- bytes += tr->read_data_count();
disk_work_outstanding = true;
break;
default:
- compute_io = false;
error << string_compose(_("Butler read ahead failure on dstream %1"), (*i)->name()) << endmsg;
break;
}
}
if (!err && transport_work_requested()) {
- continue;
+ goto restart;
}
- if (compute_io) {
- 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;
- begin = get_microseconds();
-
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 (tr->do_flush (ButlerContext)) {
case 0:
- bytes += tr->write_data_count();
break;
+
case 1:
- bytes += tr->write_data_count();
disk_work_outstanding = true;
break;
default:
err++;
- compute_io = false;
error << string_compose(_("Butler write-behind failure on dstream %1"), (*i)->name()) << endmsg;
/* don't break - try to flush all streams in case they
are split across disks.
}
if (!err && transport_work_requested()) {
- continue;
- }
-
- 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);
- } else {
- _write_data_rate = 0; // Well, infinity would be better
- }
+ goto restart;
}
if (!disk_work_outstanding) {
{
- Glib::Mutex::Lock lm (request_lock);
+ Glib::Threads::Mutex::Lock lm (request_lock);
if (should_run && (disk_work_outstanding || transport_work_requested())) {
// 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;
// }
- continue;
+ goto restart;
}
paused.signal();
empty_pool_trash ();
}
- pthread_exit_pbd (0);
- /*NOTREACHED*/
return (0);
}
summon ();
}
+void
+Butler::queue_request (Request::Type r)
+{
+#ifndef PLATFORM_WINDOWS
+ char c = r;
+ (void) ::write (request_pipe[1], &c, 1);
+#else
+ m_request_state.set (r);
+ m_request_sem.post ();
+#endif
+}
+
void
Butler::summon ()
{
- char c = Request::Run;
- ::write (request_pipe[1], &c, 1);
+ queue_request (Request::Run);
}
void
Butler::stop ()
{
- Glib::Mutex::Lock lm (request_lock);
- char c = Request::Pause;
- ::write (request_pipe[1], &c, 1);
+ Glib::Threads::Mutex::Lock lm (request_lock);
+ queue_request (Request::Pause);
paused.wait(request_lock);
}
void
Butler::wait_until_finished ()
{
- Glib::Mutex::Lock lm (request_lock);
- char c = Request::Wake;
- ::write (request_pipe[1], &c, 1);
+ Glib::Threads::Mutex::Lock lm (request_lock);
+ queue_request (Request::Pause);
paused.wait(request_lock);
}
return g_atomic_int_get(&should_do_transport_work);
}
-float
-Butler::read_data_rate () const
-{
- /* disk i/o in excess of 10000MB/sec indicate the buffer cache
- in action. ignore it.
- */
- return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
-}
-
-float
-Butler::write_data_rate () const
-{
- /* disk i/o in excess of 10000MB/sec indicate the buffer cache
- in action. ignore it.
- */
- 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()) {
void
Butler::drop_references ()
{
+ cerr << "Butler drops pool trash\n";
SessionEvent::pool->set_trash (0);
}