2 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "pbd/pthread_utils.h"
22 #include "ardour/audioengine.h"
23 #include "ardour/debug.h"
24 #include "ardour/rt_tasklist.h"
25 #include "ardour/utils.h"
29 using namespace ARDOUR;
31 RTTaskList::RTTaskList ()
33 , _task_run_sem ("rt_task_run", 0)
34 , _task_end_sem ("rt_task_done", 0)
39 RTTaskList::~RTTaskList ()
45 RTTaskList::drop_threads ()
47 Glib::Threads::Mutex::Lock pm (_process_mutex);
48 g_atomic_int_set (&_threads_active, 0);
50 uint32_t nt = _threads.size ();
51 for (uint32_t i = 0; i < nt; ++i) {
52 _task_run_sem.signal ();
54 for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i)
56 pthread_join (*i, NULL);
59 _task_run_sem.reset ();
60 _task_end_sem.reset ();
64 RTTaskList::_thread_run (void *arg)
66 RTTaskList *d = static_cast<RTTaskList *>(arg);
73 RTTaskList::reset_thread_list ()
77 const uint32_t num_threads = how_many_dsp_threads ();
78 if (num_threads < 2) {
82 Glib::Threads::Mutex::Lock pm (_process_mutex);
84 g_atomic_int_set (&_threads_active, 1);
85 for (uint32_t i = 0; i < num_threads; ++i) {
87 size_t stacksize = 100000;
88 if (!AudioEngine::instance()->is_realtime ()
90 pbd_realtime_pthread_create (PBD_SCHED_FIFO, AudioEngine::instance()->client_real_time_priority(), stacksize, &thread_id, _thread_run, this)) {
92 pthread_attr_init (&attr);
93 pthread_attr_setstacksize (&attr, stacksize);
94 if (pthread_create (&thread_id, &attr, _thread_run, this)) {
95 PBD::fatal << _("Cannot create thread for TaskList!") << endmsg;
98 pthread_attr_destroy (&attr);
100 pbd_mach_set_realtime_policy (thread_id, 5. * 1e-5);
101 _threads.push_back (thread_id);
108 Glib::Threads::Mutex::Lock tm (_tasklist_mutex, Glib::Threads::NOT_LOCK);
113 _task_run_sem.wait ();
116 if (0 == g_atomic_int_get (&_threads_active)) {
117 _task_end_sem.signal ();
123 boost::function<void ()> to_run;
125 if (_tasklist.size () > 0) {
126 to_run = _tasklist.front();
127 _tasklist.pop_front ();
131 if (!to_run.empty ()) {
137 _task_end_sem.signal ();
145 RTTaskList::process (TaskList const& tl)
147 Glib::Threads::Mutex::Lock pm (_process_mutex);
154 RTTaskList::process_tasklist ()
156 if (0 == g_atomic_int_get (&_threads_active) || _threads.size () == 0) {
157 for (TaskList::iterator i = _tasklist.begin (); i != _tasklist.end(); ++i) {
163 uint32_t nt = std::min (_threads.size (), _tasklist.size ());
165 for (uint32_t i = 0; i < nt; ++i) {
166 _task_run_sem.signal ();
168 for (uint32_t i = 0; i < nt; ++i) {
169 _task_end_sem.wait ();