Consolidate stack-size and priority of rt-threads

This commit is contained in:
Robin Gareus 2020-04-23 01:28:58 +02:00
parent 485ac45477
commit 3d166c7789
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
13 changed files with 36 additions and 34 deletions

View file

@ -37,6 +37,7 @@
#include <glibmm/threads.h> #include <glibmm/threads.h>
#include "pbd/signals.h" #include "pbd/signals.h"
#include "pbd/pthread_utils.h"
#include "pbd/stacktrace.h" #include "pbd/stacktrace.h"
#include "ardour/ardour.h" #include "ardour/ardour.h"
@ -117,7 +118,7 @@ class LIBARDOUR_API AudioEngine : public PortManager, public SessionHandlePtr
* - pbd_realtime_pthread_create * - pbd_realtime_pthread_create
* - pbd_set_thread_priority * - pbd_set_thread_priority
*/ */
virtual int client_real_time_priority () { return -22; } virtual int client_real_time_priority () { return PBD_RT_PRI_PROC; }
int backend_reset_requested(); int backend_reset_requested();
void request_backend_reset(); void request_backend_reset();

View file

@ -84,13 +84,12 @@ 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;
size_t stacksize = 100000;
if (!AudioEngine::instance()->is_realtime () if (!AudioEngine::instance()->is_realtime ()
|| ||
pbd_realtime_pthread_create (PBD_SCHED_FIFO, AudioEngine::instance()->client_real_time_priority(), stacksize, &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; pthread_attr_t attr;
pthread_attr_init (&attr); pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, stacksize); pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_HELP);
if (pthread_create (&thread_id, &attr, _thread_run, this)) { if (pthread_create (&thread_id, &attr, _thread_run, this)) {
PBD::fatal << _("Cannot create thread for TaskList!") << endmsg; PBD::fatal << _("Cannot create thread for TaskList!") << endmsg;
/* NOT REACHED */ /* NOT REACHED */

View file

@ -937,7 +937,7 @@ AlsaAudioBackend::_start (bool for_latency_measurement)
_run = true; _run = true;
_port_change_flag = false; _port_change_flag = false;
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -20, 100000, 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 (pthread_create (&_main_thread, NULL, pthread_process, this))
@ -1124,14 +1124,13 @@ AlsaAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr; pthread_attr_t attr;
size_t stacksize = 100000;
ThreadData* td = new ThreadData (this, func, stacksize); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -22, stacksize, 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)) {
pthread_attr_init (&attr); pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, stacksize); pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, alsa_process_thread, td)) { 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); pthread_attr_destroy (&attr);

View file

@ -73,7 +73,7 @@ static void * pthread_process (void *arg)
int int
AlsaMidiIO::start () AlsaMidiIO::start ()
{ {
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -21, 100000, 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 (pthread_create (&_main_thread, NULL, pthread_process, this)) {

View file

@ -108,7 +108,7 @@ AlsaAudioSlave::start ()
} }
_run = true; _run = true;
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -20, 100000, 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 (pthread_create (&_thread, NULL, _process_thread, this)) {

View file

@ -847,14 +847,13 @@ CoreAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr; pthread_attr_t attr;
size_t stacksize = 100000;
ThreadData* td = new ThreadData (this, func, stacksize); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -22, stacksize, 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); pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, stacksize); pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, coreaudio_process_thread, td)) { 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); pthread_attr_destroy (&attr);

View file

@ -555,11 +555,10 @@ DummyAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr; pthread_attr_t attr;
size_t stacksize = 100000;
pthread_attr_init (&attr); pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, stacksize); pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
ThreadData* td = new ThreadData (this, func, stacksize); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, dummy_process_thread, td)) { if (pthread_create (&thread_id, &attr, dummy_process_thread, td)) {
PBD::error << _("AudioEngine: cannot create process thread.") << endmsg; PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;

View file

@ -785,7 +785,7 @@ PortAudioBackend::process_callback(const float* input,
bool bool
PortAudioBackend::start_blocking_process_thread () PortAudioBackend::start_blocking_process_thread ()
{ {
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -20, 100000, 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 (pthread_create (&_main_blocking_thread, NULL, blocking_thread_func, this))
@ -1110,14 +1110,13 @@ PortAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr; pthread_attr_t attr;
size_t stacksize = 100000;
ThreadData* td = new ThreadData (this, func, stacksize); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -22, stacksize, 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); pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, stacksize); pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, portaudio_process_thread, td)) { 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); pthread_attr_destroy (&attr);

View file

@ -228,11 +228,8 @@ WinMMEMidiOutputDevice::start_midi_output_thread ()
{ {
m_thread_quit = false; m_thread_quit = false;
//pthread_attr_t attr;
size_t stacksize = 100000;
// TODO Use native threads // TODO Use native threads
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -21, stacksize, if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MIDI, PBD_RT_STACKSIZE_HELP,
&m_output_thread_handle, midi_output_thread, this)) { &m_output_thread_handle, midi_output_thread, this)) {
return false; return false;
} }

View file

@ -628,7 +628,7 @@ PulseAudioBackend::_start (bool /*for_latency_measurement*/)
_run = true; _run = true;
_port_change_flag = false; _port_change_flag = false;
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -20, 100000, 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 (pthread_create (&_main_thread, NULL, pthread_process, this)) {
PBD::error << _("PulseAudioBackend: failed to create process thread.") << endmsg; PBD::error << _("PulseAudioBackend: failed to create process thread.") << endmsg;
@ -744,14 +744,13 @@ PulseAudioBackend::create_process_thread (boost::function<void()> func)
{ {
pthread_t thread_id; pthread_t thread_id;
pthread_attr_t attr; pthread_attr_t attr;
size_t stacksize = 100000;
ThreadData* td = new ThreadData (this, func, stacksize); ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -22, stacksize, 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); pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, stacksize); pthread_attr_setstacksize (&attr, PBD_RT_STACKSIZE_PROC);
if (pthread_create (&thread_id, &attr, pulse_process_thread, td)) { 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); pthread_attr_destroy (&attr);

View file

@ -48,6 +48,16 @@
#include "pbd/libpbd_visibility.h" #include "pbd/libpbd_visibility.h"
#include "pbd/signals.h" #include "pbd/signals.h"
#define PBD_RT_STACKSIZE_PROC 0x20000 // 128kB
#define PBD_RT_STACKSIZE_HELP 0x08000 // 32kB
/* these are relative to sched_get_priority_max()
* see pbd_absolute_rt_priority()
*/
#define PBD_RT_PRI_MAIN -20
#define PBD_RT_PRI_MIDI -21
#define PBD_RT_PRI_PROC -22
LIBPBD_API int pthread_create_and_store (std::string name, pthread_t *thread, void * (*start_routine)(void *), void * arg); LIBPBD_API int pthread_create_and_store (std::string name, pthread_t *thread, void * (*start_routine)(void *), void * arg);
LIBPBD_API void pthread_cancel_one (pthread_t thread); LIBPBD_API void pthread_cancel_one (pthread_t thread);
LIBPBD_API void pthread_cancel_all (); LIBPBD_API void pthread_cancel_all ();

View file

@ -132,7 +132,7 @@ pthread_create_and_store (string name, pthread_t *thread, void * (*start_routin
// set default stack size to sensible default for memlocking // set default stack size to sensible default for memlocking
pthread_attr_init(&default_attr); pthread_attr_init(&default_attr);
pthread_attr_setstacksize(&default_attr, 500000); pthread_attr_setstacksize(&default_attr, 0x80000); // 512kB
ThreadStartWithName* ts = new ThreadStartWithName (start_routine, arg, name); ThreadStartWithName* ts = new ThreadStartWithName (start_routine, arg, name);

View file

@ -621,7 +621,7 @@ Convlevel::start (int abspri, int policy)
pthread_attr_setschedparam (&attr, &parm); pthread_attr_setschedparam (&attr, &parm);
pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setstacksize (&attr, 0x10000); pthread_attr_setstacksize (&attr, 0x10000); // 64kB
pthread_create (&_pthr, &attr, static_main, this); pthread_create (&_pthr, &attr, static_main, this);
pthread_attr_destroy (&attr); pthread_attr_destroy (&attr);
} }