X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fsession_events.cc;h=9d11cb0b9194859c9bebbb3fffc954f120339cb6;hb=6e94b1fb9f79c5bfcf22cd5b88619afa9bedb1b2;hp=4df5111b03eaf797b83aba6861f680c7c77bf195;hpb=c11c01ef200ce01ec454ff1d8023732d2cd6b06e;p=ardour.git diff --git a/libs/ardour/session_events.cc b/libs/ardour/session_events.cc index 4df5111b03..9d11cb0b91 100644 --- a/libs/ardour/session_events.cc +++ b/libs/ardour/session_events.cc @@ -20,14 +20,11 @@ #include #include -#include "ardour/timestamps.h" - #include "pbd/error.h" #include "pbd/enumwriter.h" +#include "pbd/stacktrace.h" +#include "pbd/pthread_utils.h" -#include "ardour/ardour.h" -#include "ardour/audio_diskstream.h" -#include "ardour/butler.h" #include "ardour/debug.h" #include "ardour/session_event.h" @@ -45,6 +42,12 @@ SessionEvent::init_event_pool () pool = new PerThreadPool; } +bool +SessionEvent::has_per_thread_pool () +{ + return pool->has_per_thread_pool (); +} + void SessionEvent::create_per_thread_pool (const std::string& name, uint32_t nitems) { @@ -55,44 +58,71 @@ SessionEvent::create_per_thread_pool (const std::string& name, uint32_t nitems) pool->create_per_thread_pool (name, sizeof (SessionEvent), nitems); } +SessionEvent::SessionEvent (Type t, Action a, framepos_t when, framepos_t where, double spd, bool yn, bool yn2, bool yn3) + : type (t) + , action (a) + , action_frame (when) + , target_frame (where) + , speed (spd) + , yes_or_no (yn) + , second_yes_or_no (yn2) + , third_yes_or_no (yn3) + , event_loop (0) +{ + DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("NEW SESSION EVENT, type = %1 action = %2\n", enum_2_string (type), enum_2_string (action))); +} + void * -SessionEvent::operator new (size_t) +SessionEvent::operator new (size_t) { CrossThreadPool* p = pool->per_thread_pool (); SessionEvent* ev = static_cast (p->alloc ()); + DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("%1 Allocating SessionEvent from %2 ev @ %3 pool size %4 free %5 used %6\n", pthread_name(), p->name(), ev, + p->total(), p->available(), p->used())); + ev->own_pool = p; return ev; } - -void -SessionEvent::operator delete (void *ptr, size_t /*size*/) + +void +SessionEvent::operator delete (void *ptr, size_t /*size*/) { - Pool* p = pool->per_thread_pool (); + Pool* p = pool->per_thread_pool (false); SessionEvent* ev = static_cast (ptr); - if (p == ev->own_pool) { + DEBUG_TRACE (DEBUG::SessionEvents, string_compose ( + "%1 Deleting SessionEvent @ %2 type %3 action %4 ev thread pool = %5 ev pool = %6 size %7 free %8 used %9\n", + pthread_name(), ev, enum_2_string (ev->type), enum_2_string (ev->action), p->name(), ev->own_pool->name(), ev->own_pool->total(), ev->own_pool->available(), ev->own_pool->used() + )); + + if (p && p == ev->own_pool) { p->release (ptr); } else { + assert(ev->own_pool); ev->own_pool->push (ev); + DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("%1 was wrong thread for this pool, pushed event onto pending list, will be deleted on next alloc from %2 pool size %3 free %4 used %5 pending %6\n", + pthread_name(), ev->own_pool->name(), + ev->own_pool->total(), ev->own_pool->available(), ev->own_pool->used(), + ev->own_pool->pending_size())); } } void -SessionEventManager::add_event (nframes64_t frame, SessionEvent::Type type, nframes64_t target_frame) +SessionEventManager::add_event (framepos_t frame, SessionEvent::Type type, framepos_t target_frame) { SessionEvent* ev = new SessionEvent (type, SessionEvent::Add, frame, target_frame, 0); queue_event (ev); } void -SessionEventManager::remove_event (nframes64_t frame, SessionEvent::Type type) +SessionEventManager::remove_event (framepos_t frame, SessionEvent::Type type) { SessionEvent* ev = new SessionEvent (type, SessionEvent::Remove, frame, 0, 0); queue_event (ev); } void -SessionEventManager::replace_event (SessionEvent::Type type, nframes64_t frame, nframes64_t target) +SessionEventManager::replace_event (SessionEvent::Type type, framepos_t frame, framepos_t target) { SessionEvent* ev = new SessionEvent (type, SessionEvent::Replace, frame, target, 0); queue_event (ev); @@ -101,30 +131,49 @@ SessionEventManager::replace_event (SessionEvent::Type type, nframes64_t frame, void SessionEventManager::clear_events (SessionEvent::Type type) { - SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, 0, 0, 0); + SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, SessionEvent::Immediate, 0, 0); queue_event (ev); } +void +SessionEventManager::clear_events (SessionEvent::Type type, boost::function after) +{ + SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, SessionEvent::Immediate, 0, 0); + ev->rt_slot = after; + + /* in the calling thread, after the clear is complete, arrange to flush things from the event + pool pending list (i.e. to make sure they are really back in the free list and available + for future events). + */ + + ev->event_loop = PBD::EventLoop::get_event_loop_for_thread (); + if (ev->event_loop) { + ev->rt_return = boost::bind (&CrossThreadPool::flush_pending_with_ev, ev->event_pool(), _1); + } + + queue_event (ev); +} void SessionEventManager::dump_events () const { cerr << "EVENT DUMP" << endl; for (Events::const_iterator i = events.begin(); i != events.end(); ++i) { - cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl; + + cerr << "\tat " << (*i)->action_frame << ' ' << enum_2_string ((*i)->type) << " target = " << (*i)->target_frame << endl; } cerr << "Next event: "; - if ((Events::const_iterator) next_event == events.end()) { + if ((Events::const_iterator) next_event == events.end()) { cerr << "none" << endl; } else { cerr << "at " << (*next_event)->action_frame << ' ' - << (*next_event)->type << " target = " + << enum_2_string ((*next_event)->type) << " target = " << (*next_event)->target_frame << endl; } cerr << "Immediate events pending:\n"; for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) { - cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl; + cerr << "\tat " << (*i)->action_frame << ' ' << enum_2_string((*i)->type) << " target = " << (*i)->target_frame << endl; } cerr << "END EVENT_DUMP" << endl; } @@ -144,7 +193,16 @@ SessionEventManager::merge_event (SessionEvent* ev) case SessionEvent::Clear: _clear_event_type (ev->type); - delete ev; + /* run any additional realtime callback, if any */ + if (ev->rt_slot) { + ev->rt_slot (); + } + if (ev->event_loop) { + /* run non-realtime callback (in some other thread) */ + ev->event_loop->call_slot (MISSING_INVALIDATOR, boost::bind (ev->rt_return, ev)); + } else { + delete ev; + } return; case SessionEvent::Add: @@ -153,13 +211,14 @@ SessionEventManager::merge_event (SessionEvent* ev) /* try to handle immediate events right here */ - if (ev->action_frame == 0) { + if (ev->action_frame == SessionEvent::Immediate) { process_event (ev); return; } switch (ev->type) { case SessionEvent::AutoLoop: + case SessionEvent::AutoLoopDeclick: case SessionEvent::StopOnce: _clear_event_type (ev->type); break; @@ -229,7 +288,7 @@ SessionEventManager::_remove_event (SessionEvent* ev) if (i == next_event) { ++next_event; } - events.erase (i); + i = events.erase (i); break; } }