Consolidate code using pthread_attr_setstacksize

This also adds some stack constraint to rt and fallback threads
that didn't have those before (ALSA MIDI for example)
This commit is contained in:
Robin Gareus 2020-06-06 18:35:44 +02:00
parent 515ffbdfe2
commit 09aa0a3d1a
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
8 changed files with 28 additions and 61 deletions

View file

@ -86,17 +86,14 @@ RTTaskList::reset_thread_list ()
g_atomic_int_set (&_threads_active, 1); g_atomic_int_set (&_threads_active, 1);
for (uint32_t i = 0; i < num_threads; ++i) { for (uint32_t i = 0; i < num_threads; ++i) {
pthread_t thread_id; pthread_t thread_id;
if (!AudioEngine::instance()->is_realtime () int rv = AudioEngine::instance()->is_realtime () ?
|| pbd_realtime_pthread_create (PBD_SCHED_FIFO, AudioEngine::instance()->client_real_time_priority(), PBD_RT_STACKSIZE_HELP, &thread_id, _thread_run, this)
pbd_realtime_pthread_create (PBD_SCHED_FIFO, AudioEngine::instance()->client_real_time_priority(), PBD_RT_STACKSIZE_HELP, &thread_id, _thread_run, this)) { :
pthread_attr_t attr; pbd_pthread_create (PBD_RT_STACKSIZE_HELP, &thread_id, _thread_run, this);
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_HELP); if (rv) {
if (pthread_create (&thread_id, &attr, _thread_run, this)) { PBD::fatal << _("Cannot create thread for TaskList! (") << strerror(errno) << ')' << endmsg;
PBD::fatal << _("Cannot create thread for TaskList! (") << strerror(errno) << ')' << endmsg; /* NOT REACHED */
/* NOT REACHED */
}
pthread_attr_destroy (&attr);
} }
pbd_mach_set_realtime_policy (thread_id, 5. * 1e-5); pbd_mach_set_realtime_policy (thread_id, 5. * 1e-5);
_threads.push_back (thread_id); _threads.push_back (thread_id);

View file

@ -963,8 +963,7 @@ AlsaAudioBackend::_start (bool for_latency_measurement)
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_PROC, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_PROC,
&_main_thread, pthread_process, this)) &_main_thread, pthread_process, this))
{ {
if (pthread_create (&_main_thread, NULL, pthread_process, this)) if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &_main_thread, pthread_process, this)) {
{
PBD::error << _("AlsaAudioBackend: failed to create process thread.") << endmsg; PBD::error << _("AlsaAudioBackend: failed to create process thread.") << endmsg;
delete _pcmi; _pcmi = 0; delete _pcmi; _pcmi = 0;
_device_reservation.release_device(); _device_reservation.release_device();
@ -1145,21 +1144,14 @@ AlsaAudioBackend::alsa_process_thread (void *arg)
int int
AlsaAudioBackend::create_process_thread (boost::function<void()> func) AlsaAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr;
ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC, &thread_id, alsa_process_thread, td)) {
&thread_id, alsa_process_thread, td)) { if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &thread_id, alsa_process_thread, td)) {
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, alsa_process_thread, td)) {
PBD::error << _("AudioEngine: cannot create process thread.") << endmsg; PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
pthread_attr_destroy (&attr);
return -1; return -1;
} }
pthread_attr_destroy (&attr);
} }
_threads.push_back (thread_id); _threads.push_back (thread_id);

View file

@ -76,7 +76,7 @@ AlsaMidiIO::start ()
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MIDI, PBD_RT_STACKSIZE_HELP, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MIDI, PBD_RT_STACKSIZE_HELP,
&_main_thread, pthread_process, this)) &_main_thread, pthread_process, this))
{ {
if (pthread_create (&_main_thread, NULL, pthread_process, this)) { if (pbd_pthread_create (PBD_RT_STACKSIZE_HELP, &_main_thread, pthread_process, this)) {
PBD::error << _("AlsaMidiIO: Failed to create process thread.") << endmsg; PBD::error << _("AlsaMidiIO: Failed to create process thread.") << endmsg;
return -1; return -1;
} else { } else {

View file

@ -113,7 +113,7 @@ AlsaAudioSlave::start ()
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_HELP, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_HELP,
&_thread, _process_thread, this)) &_thread, _process_thread, this))
{ {
if (pthread_create (&_thread, NULL, _process_thread, this)) { if (pbd_pthread_create (PBD_RT_STACKSIZE_HELP, &_thread, _process_thread, this)) {
_run = false; _run = false;
PBD::error << _("AlsaAudioBackend: failed to create slave process thread.") << endmsg; PBD::error << _("AlsaAudioBackend: failed to create slave process thread.") << endmsg;
return false; return false;

View file

@ -845,22 +845,16 @@ CoreAudioBackend::coreaudio_process_thread (void *arg)
int int
CoreAudioBackend::create_process_thread (boost::function<void()> func) CoreAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr;
ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC,
&thread_id, coreaudio_process_thread, td)) { &thread_id, coreaudio_process_thread, td)) {
pthread_attr_init (&attr); if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &thread_id, coreaudio_process_thread, td)) {
pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, coreaudio_process_thread, td)) {
PBD::error << _("AudioEngine: cannot create process thread.") << endmsg; PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
pthread_attr_destroy (&attr);
return -1; return -1;
} }
PBD::warning << _("AudioEngine: process thread failed to acquire realtime permissions.") << endmsg; PBD::warning << _("AudioEngine: process thread failed to acquire realtime permissions.") << endmsg;
pthread_attr_destroy (&attr);
} }
if (pbd_mach_set_realtime_policy (thread_id, 1e9 * _samples_per_period / _samplerate)) { if (pbd_mach_set_realtime_policy (thread_id, 1e9 * _samples_per_period / _samplerate)) {

View file

@ -36,10 +36,12 @@
#include "pbd/error.h" #include "pbd/error.h"
#include "pbd/compose.h" #include "pbd/compose.h"
#include "pbd/i18n.h" #include "pbd/pthread_utils.h"
#include "ardour/port_manager.h" #include "ardour/port_manager.h"
#include "pbd/i18n.h"
using namespace ARDOUR; using namespace ARDOUR;
static std::string s_instance_name; static std::string s_instance_name;
@ -463,7 +465,7 @@ DummyAudioBackend::_start (bool /*for_latency_measurement*/)
engine.reconnect_ports (); engine.reconnect_ports ();
_port_change_flag = false; _port_change_flag = false;
if (pthread_create (&_main_thread, NULL, pthread_process, this)) { if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &_main_thread, pthread_process, this)) {
PBD::error << _("DummyAudioBackend: cannot start.") << endmsg; PBD::error << _("DummyAudioBackend: cannot start.") << endmsg;
} }
@ -553,19 +555,13 @@ DummyAudioBackend::dummy_process_thread (void *arg)
int int
DummyAudioBackend::create_process_thread (boost::function<void()> func) DummyAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, dummy_process_thread, td)) { if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &thread_id, dummy_process_thread, td)) {
PBD::error << _("AudioEngine: cannot create process thread.") << endmsg; PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
pthread_attr_destroy (&attr);
return -1; return -1;
} }
pthread_attr_destroy (&attr);
_threads.push_back (thread_id); _threads.push_back (thread_id);
return 0; return 0;

View file

@ -788,7 +788,7 @@ PortAudioBackend::start_blocking_process_thread ()
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_PROC, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_PROC,
&_main_blocking_thread, blocking_thread_func, this)) &_main_blocking_thread, blocking_thread_func, this))
{ {
if (pthread_create (&_main_blocking_thread, NULL, blocking_thread_func, this)) if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &_main_blocking_thread, blocking_thread_func, this))
{ {
DEBUG_AUDIO("Failed to create main audio thread\n"); DEBUG_AUDIO("Failed to create main audio thread\n");
_run = false; _run = false;
@ -1108,21 +1108,15 @@ PortAudioBackend::portaudio_process_thread (void *arg)
int int
PortAudioBackend::create_process_thread (boost::function<void()> func) PortAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr;
ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC,
&thread_id, portaudio_process_thread, td)) { &thread_id, portaudio_process_thread, td)) {
pthread_attr_init (&attr); if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &thread_id, portaudio_process_thread, td)) {
pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, portaudio_process_thread, td)) {
DEBUG_AUDIO("Cannot create process thread."); DEBUG_AUDIO("Cannot create process thread.");
pthread_attr_destroy (&attr);
return -1; return -1;
} }
pthread_attr_destroy (&attr);
} }
_threads.push_back (thread_id); _threads.push_back (thread_id);

View file

@ -636,7 +636,7 @@ PulseAudioBackend::_start (bool /*for_latency_measurement*/)
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_PROC, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_PROC,
&_main_thread, pthread_process, this)) { &_main_thread, pthread_process, this)) {
if (pthread_create (&_main_thread, NULL, pthread_process, this)) { if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &_main_thread, pthread_process, this)) {
PBD::error << _("PulseAudioBackend: failed to create process thread.") << endmsg; PBD::error << _("PulseAudioBackend: failed to create process thread.") << endmsg;
stop (); stop ();
_run = false; _run = false;
@ -748,21 +748,15 @@ PulseAudioBackend::pulse_process_thread (void* arg)
int int
PulseAudioBackend::create_process_thread (boost::function<void()> func) PulseAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr;
ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC,
&thread_id, pulse_process_thread, td)) { &thread_id, pulse_process_thread, td)) {
pthread_attr_init (&attr); if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &thread_id, pulse_process_thread, td)) {
pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, pulse_process_thread, td)) {
PBD::error << _("AudioEngine: cannot create process thread.") << endmsg; PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
pthread_attr_destroy (&attr);
return -1; return -1;
} }
pthread_attr_destroy (&attr);
} }
_threads.push_back (thread_id); _threads.push_back (thread_id);