From 357cf39470e485415aeee8270fe3a786cb0b6ec6 Mon Sep 17 00:00:00 2001 From: Torben Hohn Date: Thu, 3 Jun 2010 20:09:17 +0000 Subject: add missing graph.cc and friends :S git-svn-id: svn://localhost/ardour2/branches/3.0@7225 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/ardour/graph.h | 137 +++++++++++ libs/ardour/ardour/graphnode.h | 64 +++++ libs/ardour/graph.cc | 514 +++++++++++++++++++++++++++++++++++++++++ libs/ardour/graphnode.cc | 48 ++++ 4 files changed, 763 insertions(+) create mode 100644 libs/ardour/ardour/graph.h create mode 100644 libs/ardour/ardour/graphnode.h create mode 100644 libs/ardour/graph.cc create mode 100644 libs/ardour/graphnode.cc (limited to 'libs/ardour') diff --git a/libs/ardour/ardour/graph.h b/libs/ardour/ardour/graph.h new file mode 100644 index 0000000000..e8bae4a32c --- /dev/null +++ b/libs/ardour/ardour/graph.h @@ -0,0 +1,137 @@ +/* + Copyright (C) 2010 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +#ifndef __ardour_graph_h__ +#define __ardour_graph_h__ + + +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include + +#include +#include + +namespace ARDOUR +{ + +class GraphNode; +class Graph; + +class Route; +class Session; + +typedef boost::shared_ptr node_ptr_t; +typedef boost::shared_ptr graph_ptr_t; + +typedef std::list< node_ptr_t > node_list_t; +typedef std::set< node_ptr_t > node_set_t; + + + +class Graph : public SessionHandleRef +{ + public: + Graph( Session & session ); + + void prep(); + void trigger( GraphNode * n ); + void rechain( boost::shared_ptr r ); + + void dump( int chain ); + void process(); + void dec_ref(); + void restart_cycle(); + + bool run_one(); + void helper_thread(); + void main_thread(); + + int silent_process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, + bool can_record, bool rec_monitors_input, bool& need_butler ); + + int process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick, + bool can_record, bool rec_monitors_input, bool& need_butler ); + + int routes_no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, + bool non_rt_pending, bool can_record, int declick); + + void process_one_route( Route * route ); + + protected: + virtual void session_going_away (); + + private: + std::list _thread_list; + volatile bool _quit_threads; + + node_list_t _nodes; + node_list_t _nodes_rt[2]; + + node_list_t _init_trigger_list[2]; + + std::vector _trigger_queue; + pthread_mutex_t _trigger_mutex; + + + sem_t _execution_sem; + + sem_t _callback_start_sem; + sem_t _callback_done_sem; + + volatile gint _execution_tokens; + volatile gint _finished_refcount; + volatile gint _init_finished_refcount[2]; + + bool _graph_empty; + + // chain swapping + pthread_mutex_t _swap_mutex; + volatile int _current_chain; + volatile int _pending_chain; + volatile int _setup_chain; + + // parameter caches. + nframes_t _process_nframes; + sframes_t _process_start_frame; + sframes_t _process_end_frame; + bool _process_can_record; + bool _process_rec_monitors_input; + bool _process_non_rt_pending; + int _process_declick; + + bool _process_silent; + bool _process_noroll; + int _process_retval; + bool _process_need_butler; +}; + +} + +#endif diff --git a/libs/ardour/ardour/graphnode.h b/libs/ardour/ardour/graphnode.h new file mode 100644 index 0000000000..e5f2a78452 --- /dev/null +++ b/libs/ardour/ardour/graphnode.h @@ -0,0 +1,64 @@ +/* + Copyright (C) 2000 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +#ifndef __ardour_graphnode_h__ +#define __ardour_graphnode_h__ + +#include +#include +#include + +#include + +namespace ARDOUR +{ + +class Graph; +class GraphNode; + +typedef boost::shared_ptr node_ptr_t; +typedef std::set< node_ptr_t > node_set_t; +typedef std::list< node_ptr_t > node_list_t; + +class GraphNode +{ + public: + GraphNode( boost::shared_ptr Graph ); + + void prep( int chain ); + void dec_ref(); + void finish( int chain ); + + virtual void process(); + + private: + friend class Graph; + + node_set_t _activation_set[2]; + + boost::shared_ptr _graph; + + gint _refcount; + gint _init_refcount[2]; +}; + +} + +#endif diff --git a/libs/ardour/graph.cc b/libs/ardour/graph.cc new file mode 100644 index 0000000000..3c5fcc64f0 --- /dev/null +++ b/libs/ardour/graph.cc @@ -0,0 +1,514 @@ +/* + Copyright (C) 2010 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "ardour/graph.h" +#include "ardour/types.h" +#include "ardour/session.h" +#include "ardour/route.h" +#include "ardour/process_thread.h" +#include "ardour/audioengine.h" + +#include + +#ifdef __linux__ +#include +#elif defined(__APPLE__) || defined(__FreeBSD__) +#include +#include +#endif + + + +#include +#include + +using namespace ARDOUR; + +static unsigned int hardware_concurrency() + { +#if defined(PTW32_VERSION) || defined(__hpux) + return pthread_num_processors_np(); +#elif defined(__APPLE__) || defined(__FreeBSD__) + int count; + size_t size=sizeof(count); + return sysctlbyname("hw.ncpu",&count,&size,NULL,0)?0:count; +#elif defined(HAVE_UNISTD) && defined(_SC_NPROCESSORS_ONLN) + int const count=sysconf(_SC_NPROCESSORS_ONLN); + return (count>0)?count:0; +#else + return 0; +#endif + } +// ========================================== Graph + +Graph::Graph( Session & session ) + : SessionHandleRef( session ) +{ + pthread_mutex_init( &_trigger_mutex, NULL ); + sem_init( &_execution_sem, 0, 0 ); + + sem_init( &_callback_start_sem, 0, 0 ); + sem_init( &_callback_done_sem, 0, 0 ); + + _execution_tokens = 0; + + pthread_mutex_init( &_swap_mutex, NULL ); + _current_chain = 0; + _pending_chain = 0; + _setup_chain = 1; + _quit_threads = false; + _graph_empty = true; + + int num_cpu = hardware_concurrency(); + printf( "found %d cpus\n", num_cpu ); + _thread_list.push_back( Glib::Thread::create( sigc::mem_fun( *this, &Graph::main_thread ), 100000, true, true, Glib::THREAD_PRIORITY_NORMAL ) ); + for (int i=1; i::iterator i=_thread_list.begin(); i!=_thread_list.end(); i++) + { + (*i)->join(); + } + + // now drop all references on the nodes. + _nodes.clear(); + _nodes_rt[0].clear(); + _nodes_rt[1].clear(); + _init_trigger_list[0].clear(); + _init_trigger_list[1].clear(); + _trigger_queue.clear(); +} + +void +Graph::prep() +{ + node_list_t::iterator i; + int chain; + + if (pthread_mutex_trylock (&_swap_mutex) == 0) + { + // we got the swap mutex. + if (_current_chain != _pending_chain) + { + //printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain ); + _setup_chain = _current_chain; + _current_chain = _pending_chain; + } + pthread_mutex_unlock (&_swap_mutex); + } + + chain = _current_chain; + + _graph_empty = true; + for (i=_nodes_rt[chain].begin(); i!=_nodes_rt[chain].end(); i++) + { + (*i)->prep( chain ); + _graph_empty = false; + } + _finished_refcount = _init_finished_refcount[chain]; + + for (i=_init_trigger_list[chain].begin(); i!=_init_trigger_list[chain].end(); i++) + this->trigger( i->get() ); +} + +void +Graph::trigger( GraphNode * n ) +{ + pthread_mutex_lock( &_trigger_mutex ); + _trigger_queue.push_back( n ); + pthread_mutex_unlock( &_trigger_mutex ); +} + +void +Graph::dec_ref() +{ + if (g_atomic_int_dec_and_test( &_finished_refcount )) + { + // ok... this cycle is finished now. + // we are the only thread alive. + + this->restart_cycle(); + + } +} + +void +Graph::restart_cycle() +{ + //printf( "cycle_done chain: %d\n", _current_chain ); + + // we are through. wakeup our caller. +again: + sem_post( &_callback_done_sem ); + + // block until we are triggered. + sem_wait( &_callback_start_sem ); + if (_quit_threads) + return; + + //printf( "cycle_start\n" ); + + this->prep(); + if (_graph_empty) + goto again; + //printf( "cycle_start chain: %d\n", _current_chain ); + + // returning will restart the cycle. + // starting with waking up the others. +} + +static bool +is_feedback( boost::shared_ptr routelist, Route * from, boost::shared_ptr to ) +{ + for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) + { + if ((*ri).get() == from) + return false; + if ((*ri) == to) + return true; + } + assert(0); + return false; +} + +static bool +is_feedback( boost::shared_ptr routelist, boost::shared_ptr from, Route * to ) +{ + for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) + { + if ((*ri).get() == to) + return true; + if ((*ri) == from) + return false; + } + assert(0); + return false; +} + +void +Graph::rechain( boost::shared_ptr routelist ) +{ + node_list_t::iterator ni; + + pthread_mutex_lock (&_swap_mutex); + int chain = _setup_chain; + printf( "============== setup %d\n", chain ); + // set all refcounts to 0; + + _init_finished_refcount[chain] = 0; + _init_trigger_list[chain].clear(); + + _nodes_rt[chain].clear(); + + for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) + { + node_ptr_t n = boost::dynamic_pointer_cast (*ri); + + n->_init_refcount[chain] = 0; + n->_activation_set[chain].clear(); + _nodes_rt[chain].push_back(n); + } + + // now add refs for the connections. + + for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) + { + bool has_input = false; + bool has_output = false; + + boost::shared_ptr rp = boost::dynamic_pointer_cast( *ni ); + + for (RouteList::iterator ri=routelist->begin(); ri!=routelist->end(); ri++) + { + if (rp->direct_feeds( *ri )) + { + if (is_feedback( routelist, rp.get(), *ri )) + continue; + + has_output = true; + (*ni)->_activation_set[chain].insert( boost::dynamic_pointer_cast (*ri) ); + } + } + + for (Route::FedBy::iterator fi=rp->fed_by().begin(); fi!=rp->fed_by().end(); fi++) + { + if (boost::shared_ptr r = fi->r.lock()) + if (!is_feedback( routelist, r, rp.get() )) + has_input = true; + } + + for (node_set_t::iterator ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) + { + (*ai)->_init_refcount[chain] += 1; + } + + if (!has_input) + _init_trigger_list[chain].push_back( *ni ); + + if (!has_output) + _init_finished_refcount[chain] += 1; + } + + _pending_chain = chain; + dump(chain); + pthread_mutex_unlock (&_swap_mutex); +} + + +bool +Graph::run_one() +{ + GraphNode * to_run; + + pthread_mutex_lock( &_trigger_mutex ); + if (_trigger_queue.size()) { + to_run = _trigger_queue.back(); + _trigger_queue.pop_back(); + } + else + to_run = 0; + + int wakeup = std::min( (int) _execution_tokens, (int) _trigger_queue.size() ); + _execution_tokens -= wakeup; + + for( int i=0; iprocess(); + to_run->finish( _current_chain ); + + return false; +} + +static void get_rt() +{ + int priority = jack_client_real_time_priority( AudioEngine::instance()->jack() ); + + if (priority) + { + struct sched_param rtparam; + + memset (&rtparam, 0, sizeof (rtparam)); + rtparam.sched_priority = priority; + + pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam); + } +} + +void +Graph::helper_thread() +{ + ProcessThread *pt = new ProcessThread; + + pt->get_buffers(); + get_rt(); + + + while(1) + { + if (run_one()) + break; + } + pt->drop_buffers(); +} + +void +Graph::main_thread() +{ + ProcessThread *pt = new ProcessThread; + + pt->get_buffers(); + get_rt(); + +again: + sem_wait( &_callback_start_sem ); + + this->prep(); + + if (_graph_empty) { + sem_post( &_callback_done_sem ); + goto again; + } + + while(1) + { + if (run_one()) + break; + } + pt->drop_buffers(); +} + +void +Graph::dump( int chain ) +{ + node_list_t::iterator ni; + node_set_t::iterator ai; + + chain = _pending_chain; + + printf( "--------------------------------------------Graph dump:\n" ); + for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) + { + boost::shared_ptr rp = boost::dynamic_pointer_cast( *ni ); + printf( "GraphNode: %s refcount: %d\n", rp->name().c_str(), (*ni)->_init_refcount[chain] ); + for (ai=(*ni)->_activation_set[chain].begin(); ai!=(*ni)->_activation_set[chain].end(); ai++) + { + printf( " triggers: %s\n", boost::dynamic_pointer_cast(*ai)->name().c_str() ); + } + } + + printf( "------------- trigger list:\n" ); + for (ni=_init_trigger_list[chain].begin(); ni!=_init_trigger_list[chain].end(); ni++) + { + printf( "GraphNode: %s refcount: %d\n", boost::dynamic_pointer_cast(*ni)->name().c_str(), (*ni)->_init_refcount[chain] ); + } + + printf( "final activation refcount: %d\n", _init_finished_refcount[chain] ); +} + +int +Graph::silent_process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, + bool can_record, bool rec_monitors_input, bool& need_butler ) +{ + _process_nframes = nframes; + _process_start_frame = start_frame; + _process_end_frame = end_frame; + _process_can_record = can_record; + _process_rec_monitors_input = rec_monitors_input; + + _process_silent = true; + _process_noroll = false; + _process_retval = 0; + _process_need_butler = false; + + if (!_graph_empty) + { + sem_post( &_callback_start_sem ); + sem_wait( &_callback_done_sem ); + } + + need_butler = _process_need_butler; + + return _process_retval; +} + +int +Graph::process_routes (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick, + bool can_record, bool rec_monitors_input, bool& need_butler ) +{ + _process_nframes = nframes; + _process_start_frame = start_frame; + _process_end_frame = end_frame; + _process_can_record = can_record; + _process_rec_monitors_input = rec_monitors_input; + _process_declick = declick; + + _process_silent = false; + _process_noroll = false; + _process_retval = 0; + _process_need_butler = false; + + sem_post( &_callback_start_sem ); + sem_wait( &_callback_done_sem ); + + need_butler = _process_need_butler; + + return _process_retval; +} + +int +Graph::routes_no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, + bool non_rt_pending, bool can_record, int declick) +{ + _process_nframes = nframes; + _process_start_frame = start_frame; + _process_end_frame = end_frame; + _process_can_record = can_record; + _process_declick = declick; + _process_non_rt_pending = non_rt_pending; + + _process_silent = false; + _process_noroll = true; + _process_retval = 0; + _process_need_butler = false; + + sem_post( &_callback_start_sem ); + sem_wait( &_callback_done_sem ); + + return _process_retval; +} +void +Graph::process_one_route( Route * route ) +{ + bool need_butler = false; + int retval; + + assert( route ); + + if (_process_silent) + retval = route->silent_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_can_record, _process_rec_monitors_input, need_butler); + else if (_process_noroll) + { + route->set_pending_declick (_process_declick); + retval = route->no_roll (_process_nframes, _process_start_frame, _process_end_frame, _process_non_rt_pending, _process_can_record, _process_declick); + } + else + { + route->set_pending_declick (_process_declick); + retval = route->roll (_process_nframes, _process_start_frame, _process_end_frame, _process_declick, _process_can_record, _process_rec_monitors_input, need_butler); + } + + if (retval) + _process_retval = retval; + + if (need_butler) + _process_need_butler = true; +} + + + diff --git a/libs/ardour/graphnode.cc b/libs/ardour/graphnode.cc new file mode 100644 index 0000000000..c8f3e630d1 --- /dev/null +++ b/libs/ardour/graphnode.cc @@ -0,0 +1,48 @@ + +#include "ardour/graph.h" +#include "ardour/graphnode.h" +#include "ardour/route.h" + +using namespace ARDOUR; + +// ========================================== GraphNode + +GraphNode::GraphNode( graph_ptr_t graph ) + : _graph(graph) +{ } + +void +GraphNode::prep( int chain ) +{ + _refcount = _init_refcount[chain]; +} + +void +GraphNode::dec_ref() +{ + if (g_atomic_int_dec_and_test( &_refcount )) + _graph->trigger( this ); +} + +void +GraphNode::finish( int chain ) +{ + node_set_t::iterator i; + bool feeds_somebody = false; + for (i=_activation_set[chain].begin(); i!=_activation_set[chain].end(); i++) + { + (*i)->dec_ref(); + feeds_somebody = true; + } + if (!feeds_somebody) + { + _graph->dec_ref(); + } +} + + +void +GraphNode::process() +{ + _graph->process_one_route( dynamic_cast(this) ); +} -- cgit v1.2.3