Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-
#include <stdio.h>
#include <cmath>
using namespace ARDOUR;
using namespace PBD;
+using namespace std;
Graph::Graph (Session & session)
: SessionHandleRef (session)
+ , _execution_sem ("graph_execution", 0)
+ , _callback_start_sem ("graph_start", 0)
+ , _callback_done_sem ("graph_done", 0)
+ , _cleanup_sem ("graph_cleanup", 0)
{
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;
int pu = Config->get_processor_usage ();
if (pu < 0) {
- /* use "pu" less cores for DSP than appear to be available
+ /* pu is negative: use "pu" less cores for DSP than appear to be available
*/
- if (pu < num_threads) {
- num_threads += pu; // pu is negative
+ if (-pu < num_threads) {
+ num_threads += pu;
} else {
num_threads = 1;
}
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));
+ pthread_t a_thread;
+
+ if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::main_thread, this), &a_thread, 100000) == 0) {
+ _thread_list.push_back (a_thread);
+ }
for (int i = 1; i < num_threads; ++i) {
- _thread_list.push_back (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), 100000));
+ if (AudioEngine::instance()->create_process_thread (boost::bind (&Graph::helper_thread, this), &a_thread, 100000) == 0) {
+ _thread_list.push_back (a_thread);
+ }
}
}
_quit_threads = true;
for (unsigned int i=0; i<_thread_list.size(); i++) {
- sem_post( &_execution_sem);
+ _execution_sem.signal ();
}
- sem_post( &_callback_start_sem);
+ _callback_start_sem.signal ();
- for (std::list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
+ for (list<pthread_t>::iterator i = _thread_list.begin(); i != _thread_list.end(); i++) {
void* status;
pthread_join (*i, &status);
}
_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;
// we are through. wakeup our caller.
again:
- sem_post( &_callback_done_sem);
+ _callback_done_sem.signal ();
// block until we are triggered.
- sem_wait( &_callback_start_sem);
+ _callback_start_sem.wait();
if (_quit_threads)
return;
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;
DEBUG_TRACE (DEBUG::Graph, string_compose ("============== setup %1\n", chain));
// set all refcounts to 0;
_pending_chain = chain;
dump(chain);
- pthread_mutex_unlock (&_swap_mutex);
}
-
bool
Graph::run_one()
{
to_run = 0;
}
- int wakeup = std::min ((int) _execution_tokens, (int) _trigger_queue.size());
+ int et = _execution_tokens;
+ int ts = _trigger_queue.size();
+
+ int wakeup = min (et, ts);
_execution_tokens -= wakeup;
for (int i=0; i<wakeup; i++ ) {
- sem_post (&_execution_sem);
+ _execution_sem.signal ();
}
while (to_run == 0) {
_execution_tokens += 1;
pthread_mutex_unlock (&_trigger_mutex);
DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 goes to sleep\n", pthread_self()));
- sem_wait (&_execution_sem);
+ _execution_sem.wait ();
if (_quit_threads)
return true;
DEBUG_TRACE (DEBUG::ProcessThreads, string_compose ("%1 is awake\n", pthread_self()));
get_rt();
again:
- sem_wait (&_callback_start_sem);
-
+ _callback_start_sem.wait ();
+ DEBUG_TRACE(DEBUG::Graph, "main thread is awake\n");
this->prep();
- if (_graph_empty) {
- sem_post (&_callback_done_sem);
+ if (_graph_empty && !_quit_threads) {
+ _callback_done_sem.signal ();
goto again;
}
while (1) {
+ DEBUG_TRACE(DEBUG::Graph, "main thread runs one graph node\n");
if (run_one()) {
break;
}
}
int
-Graph::silent_process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
+Graph::silent_process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
bool can_record, bool rec_monitors_input, bool& need_butler)
{
_process_nframes = nframes;
_process_need_butler = false;
if (!_graph_empty) {
- sem_post (&_callback_start_sem);
- sem_wait (&_callback_done_sem);
+ DEBUG_TRACE(DEBUG::Graph, "wake graph for silent process\n");
+ _callback_start_sem.signal ();
+ _callback_done_sem.wait ();
}
need_butler = _process_need_butler;
}
int
-Graph::process_routes (nframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
+Graph::process_routes (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick,
bool can_record, bool rec_monitors_input, bool& need_butler)
{
+ DEBUG_TRACE (DEBUG::Graph, string_compose ("graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
+
_process_nframes = nframes;
_process_start_frame = start_frame;
_process_end_frame = end_frame;
_process_retval = 0;
_process_need_butler = false;
- sem_post (&_callback_start_sem);
- sem_wait (&_callback_done_sem);
+ DEBUG_TRACE(DEBUG::Graph, "wake graph for non-silent process\n");
+ _callback_start_sem.signal ();
+ _callback_done_sem.wait ();
+
+ DEBUG_TRACE (DEBUG::Graph, "graph execution complete\n");
need_butler = _process_need_butler;
}
int
-Graph::routes_no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
+Graph::routes_no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
bool non_rt_pending, bool can_record, int declick)
{
+ DEBUG_TRACE (DEBUG::Graph, string_compose ("no-roll graph execution from %1 to %2 = %3\n", start_frame, end_frame, nframes));
+
_process_nframes = nframes;
_process_start_frame = start_frame;
_process_end_frame = end_frame;
_process_retval = 0;
_process_need_butler = false;
- sem_post (&_callback_start_sem);
- sem_wait (&_callback_done_sem);
+ DEBUG_TRACE(DEBUG::Graph, "wake graph for no-roll process\n");
+ _callback_start_sem.signal ();
+ _callback_done_sem.wait ();
return _process_retval;
}