const correctness.
[ardour.git] / libs / ardour / graph.cc
index 4cb8d56da4e40ff4688daaafd6cda2c58fd972a2..0987b979a2e5162609296c783bb42b533dee62a9 100644 (file)
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 */
-
-#ifdef __linux__
-#include <unistd.h>
-#elif defined(__APPLE__) || defined(__FreeBSD__)
-#include <sys/types.h>
-#include <sys/sysctl.h>
-#endif
-
 #include <stdio.h>
 #include <cmath>
 
 #include "pbd/compose.h"
+#include "pbd/cpus.h"
 
 #include "ardour/debug.h"
 #include "ardour/graph.h"
 
 using namespace ARDOUR;
 using namespace PBD;
+using namespace std;
 
-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 (Session & session) 
         : SessionHandleRef (session) 
@@ -70,10 +48,10 @@ Graph::Graph (Session & session)
 
         sem_init( &_callback_start_sem, 0, 0 );
         sem_init( &_callback_done_sem,  0, 0 );
+        sem_init( &_cleanup_sem,  0, 0 );
 
         _execution_tokens = 0;
 
-        pthread_mutex_init (&_swap_mutex, NULL);
         _current_chain = 0;
         _pending_chain = 0;
         _setup_chain   = 1;
@@ -81,10 +59,34 @@ Graph::Graph (Session & session)
         _graph_empty = true;
 
         int num_cpu = hardware_concurrency();
-        info << string_compose (_("Using %1 CPUs via %1 threads\n"), num_cpu) << endmsg;
-        _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<num_cpu; i++)
-                _thread_list.push_back( Glib::Thread::create( sigc::mem_fun( *this, &Graph::helper_thread), 100000, true, true, Glib::THREAD_PRIORITY_NORMAL) );
+        int num_threads = num_cpu;
+        int pu = Config->get_processor_usage ();
+
+        if (pu < 0) {
+                /* pu is negative: use "pu" less cores for DSP than appear to be available
+                 */
+
+                if (-pu < num_threads) {
+                        num_threads += pu; 
+                } else {
+                        num_threads = 1;
+                }
+        } else {
+                /* use "pu" cores, if available
+                 */
+
+                if (pu <= num_threads) {
+                        num_threads = pu;
+                } 
+        }
+
+        info << string_compose (_("Using %2 threads on %1 CPUs"), num_cpu, num_threads) << endmsg;
+
+        _thread_list.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), 100000));
+
+        for (int i = 1; i < num_threads; ++i) {
+                _thread_list.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), 100000));
+        }
 }
 
 void
@@ -98,12 +100,12 @@ Graph::session_going_away()
 
         sem_post( &_callback_start_sem);
 
-        for (std::list<Glib::Thread *>::iterator i=_thread_list.begin(); i!=_thread_list.end(); i++) {
-                (*i)->join();
+        for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
+                void* status;
+                pthread_join (*i, &status);
         }
 
         // now drop all references on the nodes.
-        _nodes.clear();
         _nodes_rt[0].clear();
         _nodes_rt[1].clear();
         _init_trigger_list[0].clear();
@@ -111,21 +113,45 @@ Graph::session_going_away()
         _trigger_queue.clear();
 }
 
+void
+Graph::clear_other_chain ()
+{
+        Glib::Mutex::Lock ls (_swap_mutex);
+
+        while (1) {
+                if (_setup_chain != _pending_chain) {
+
+                        for (node_list_t::iterator ni=_nodes_rt[_setup_chain].begin(); ni!=_nodes_rt[_setup_chain].end(); ni++) {
+                                (*ni)->_activation_set[_setup_chain].clear();
+                        }
+
+                        _nodes_rt[_setup_chain].clear ();
+                        _init_trigger_list[_setup_chain].clear ();
+                        break;
+                }
+                /* setup chain == pending chain - we have
+                   to wait till this is no longer true.
+                */
+                _cleanup_cond.wait (_swap_mutex);                
+        }
+}
+
 void
 Graph::prep()
 {
         node_list_t::iterator i;
         int chain;
 
-        if (pthread_mutex_trylock (&_swap_mutex) == 0) {
+        if (_swap_mutex.trylock()) {
                 // we got the swap mutex.
                 if (_current_chain != _pending_chain)
                 {
-                        //printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
+                        // printf ("chain swap ! %d -> %d\n", _current_chain, _pending_chain);
                         _setup_chain = _current_chain;
                         _current_chain = _pending_chain;
+                        _cleanup_cond.signal ();
                 }
-                pthread_mutex_unlock (&_swap_mutex);
+                _swap_mutex.unlock ();
         }
 
         chain = _current_chain;
@@ -217,10 +243,10 @@ void
 Graph::rechain (boost::shared_ptr<RouteList> routelist)
 {
         node_list_t::iterator ni;
+        Glib::Mutex::Lock ls (_swap_mutex);
 
-        pthread_mutex_lock (&_swap_mutex);
         int chain = _setup_chain;
-        printf ("============== setup %d\n", chain);
+        DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
         // set all refcounts to 0;
 
         _init_finished_refcount[chain] = 0;
@@ -276,10 +302,8 @@ Graph::rechain (boost::shared_ptr<RouteList> routelist)
 
         _pending_chain = chain;
         dump(chain);
-        pthread_mutex_unlock (&_swap_mutex);
 }
 
-
 bool
 Graph::run_one()
 {
@@ -293,7 +317,7 @@ Graph::run_one()
                 to_run = 0;
         }
 
-        int wakeup = std::min ((int) _execution_tokens, (int) _trigger_queue.size());
+        int wakeup = min ((int) _execution_tokens, (int) _trigger_queue.size());
         _execution_tokens -= wakeup;
 
         for (int i=0; i<wakeup; i++ ) {
@@ -370,7 +394,7 @@ Graph::main_thread()
 
         this->prep();
 
-        if (_graph_empty) {
+        if (_graph_empty && !_quit_threads) {
                 sem_post (&_callback_done_sem);
                 goto again;
         }
@@ -393,21 +417,21 @@ Graph::dump (int chain)
 
         chain = _pending_chain;
 
-        printf ("--------------------------------------------Graph dump:\n" );
+        DEBUG_TRACE (DEBUG::Graph, "--------------------------------------------Graph dump:\n");
         for (ni=_nodes_rt[chain].begin(); ni!=_nodes_rt[chain].end(); ni++) {
                 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route>( *ni);
-                printf ("GraphNode: %s  refcount: %d\n", rp->name().c_str(), (*ni)->_init_refcount[chain] );
+                DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1  refcount: %2\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<Route>(*ai)->name().c_str() );
+                        DEBUG_TRACE (DEBUG::Graph, string_compose ("  triggers: %1\n", boost::dynamic_pointer_cast<Route>(*ai)->name().c_str()));
                 }
         }
 
-        printf ("------------- trigger list:\n" );
+        DEBUG_TRACE (DEBUG::Graph, "------------- 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<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain] );
+                DEBUG_TRACE (DEBUG::Graph, string_compose ("GraphNode: %1  refcount: %2\n", boost::dynamic_pointer_cast<Route>(*ni)->name().c_str(), (*ni)->_init_refcount[chain]));
         }
 
-        printf ("final activation refcount: %d\n", _init_finished_refcount[chain] );
+        DEBUG_TRACE (DEBUG::Graph, string_compose ("final activation refcount: %1\n", _init_finished_refcount[chain]));
 #endif
 }