From 538a8cbccc81d4759e78826f6b50deff8afe0990 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 27 Sep 2024 23:04:32 +0200 Subject: [PATCH] Consolidate calls to `pthread_create' (2/2) --- libs/ardour/ardour/export_handler.h | 3 ++- libs/ardour/control_protocol_manager.cc | 2 +- libs/ardour/export_handler.cc | 15 +++++---------- libs/ardour/session.cc | 6 +++--- libs/ardour/session_process.cc | 4 +--- .../audiographer/sndfile/tmp_file_rt.h | 3 +-- libs/pbd/file_archive.cc | 10 +++++++--- libs/pbd/system_exec.cc | 8 ++------ 8 files changed, 22 insertions(+), 29 deletions(-) diff --git a/libs/ardour/ardour/export_handler.h b/libs/ardour/ardour/export_handler.h index 71c1872632..97479540f6 100644 --- a/libs/ardour/ardour/export_handler.h +++ b/libs/ardour/ardour/export_handler.h @@ -30,6 +30,7 @@ #include #include "pbd/gstdio_compat.h" +#include "pbd/pthread_utils.h" #include "ardour/export_pointers.h" #include "ardour/session.h" @@ -149,7 +150,7 @@ class LIBARDOUR_API ExportHandler : public ExportElementFactory, public sigc::tr void timespan_thread_wakeup (); static void* _timespan_thread_run (void*); - pthread_t _timespan_thread; + PBD::Thread* _timespan_thread; std::atomic _timespan_thread_active; pthread_mutex_t _timespan_mutex; pthread_cond_t _timespan_cond; diff --git a/libs/ardour/control_protocol_manager.cc b/libs/ardour/control_protocol_manager.cc index fe570c8dff..43ed625498 100644 --- a/libs/ardour/control_protocol_manager.cc +++ b/libs/ardour/control_protocol_manager.cc @@ -185,7 +185,7 @@ ControlProtocolManager::set_session (Session* s) usb_hotplug_cb, this, &_hpcp)) { _hotplug_thread_run = true; - if (pthread_create (&_hotplug_thread, NULL, usb_hotplug_thread, this)) { + if (pthread_create_and_store ("Ctrl USB Hotplug", &_hotplug_thread, usb_hotplug_thread, this, 0)) { _hotplug_thread_run = false; } } diff --git a/libs/ardour/export_handler.cc b/libs/ardour/export_handler.cc index fe566a0108..91954eccfe 100644 --- a/libs/ardour/export_handler.cc +++ b/libs/ardour/export_handler.cc @@ -25,7 +25,6 @@ #include "pbd/gstdio_compat.h" #include #include -#include #include "pbd/convert.h" @@ -125,7 +124,8 @@ ExportHandler::ExportHandler (Session & session) pthread_mutex_init (&_timespan_mutex, 0); pthread_cond_init (&_timespan_cond, 0); _timespan_thread_active.store (1); - if (pthread_create (&_timespan_thread, NULL, _timespan_thread_run, this)) { + _timespan_thread = PBD::Thread::create (boost::bind (_timespan_thread_run, this), "ExportHandler"); + if (!_timespan_thread) { _timespan_thread_active.store (0); fatal << "Cannot create export handler helper thread" << endmsg; abort(); /* NOTREACHED*/ @@ -145,8 +145,7 @@ ExportHandler::~ExportHandler () pthread_cond_signal (&_timespan_cond); pthread_mutex_unlock (&_timespan_mutex); - void *status; - pthread_join (_timespan_thread, &status); + _timespan_thread->join (); pthread_cond_destroy (&_timespan_cond); pthread_mutex_destroy (&_timespan_mutex); @@ -155,13 +154,10 @@ ExportHandler::~ExportHandler () void* ExportHandler::_timespan_thread_run (void* me) { - char name[64]; - snprintf (name, 64, "Export-TS-%p", (void*)DEBUG_THREAD_SELF); - pthread_set_name (name); ExportHandler* self = static_cast (me); - SessionEvent::create_per_thread_pool (name, 512); - PBD::notify_event_loops_about_thread_creation (pthread_self(), name, 512); + SessionEvent::create_per_thread_pool ("ExportHandler", 512); + PBD::notify_event_loops_about_thread_creation (pthread_self(), "ExportHandler", 512); pthread_mutex_lock (&self->_timespan_mutex); while (self->_timespan_thread_active.load ()) { @@ -178,7 +174,6 @@ ExportHandler::_timespan_thread_run (void* me) } } pthread_mutex_unlock (&self->_timespan_mutex); - pthread_exit (0); return 0; } diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index 969c2cd88e..6e2b8d6921 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -7845,8 +7845,10 @@ Session::auto_connect_thread_start () lx.release (); _ac_thread_active.store (1); - if (pthread_create (&_auto_connect_thread, NULL, auto_connect_thread, this)) { + if (pthread_create_and_store ("AutoConnect", &_auto_connect_thread, auto_connect_thread, this, 0)) { _ac_thread_active.store (0); + fatal << "Cannot create 'session auto connect' thread" << endmsg; + abort(); /* NOTREACHED*/ } } @@ -7881,9 +7883,7 @@ void * Session::auto_connect_thread (void *arg) { Session *s = static_cast(arg); - pthread_set_name (X_("autoconnect")); s->auto_connect_thread_run (); - pthread_exit (0); return 0; } diff --git a/libs/ardour/session_process.cc b/libs/ardour/session_process.cc index e3003127a1..1335464731 100644 --- a/libs/ardour/session_process.cc +++ b/libs/ardour/session_process.cc @@ -1199,7 +1199,7 @@ Session::emit_thread_start () } _rt_thread_active = true; - if (pthread_create (&_rt_emit_thread, NULL, emit_thread, this)) { + if (pthread_create_and_store ("SessionSignals", &_rt_emit_thread, emit_thread, this, 0)) { _rt_thread_active = false; } } @@ -1225,9 +1225,7 @@ void * Session::emit_thread (void *arg) { Session *s = static_cast(arg); - pthread_set_name ("SessionSignals"); s->emit_thread_run (); - pthread_exit (0); return 0; } diff --git a/libs/audiographer/audiographer/sndfile/tmp_file_rt.h b/libs/audiographer/audiographer/sndfile/tmp_file_rt.h index 52c44f7759..c1c63b8322 100644 --- a/libs/audiographer/audiographer/sndfile/tmp_file_rt.h +++ b/libs/audiographer/audiographer/sndfile/tmp_file_rt.h @@ -138,7 +138,6 @@ class TmpFileRt TmpFileRt *d = static_cast(arg); pthread_set_name ("ExportDiskIO"); d->disk_thread (); - pthread_exit (0); return 0; } @@ -158,7 +157,7 @@ class TmpFileRt pthread_mutex_init (&_disk_thread_lock, 0); pthread_cond_init (&_data_ready, 0); - if (pthread_create (&_thread_id, NULL, _disk_thread, this)) { + if (pthread_create_and_store ("ExportDiskIO", &_thread_id, _disk_thread, this, 0)) { _capture = false; if (SndfileWriter::throw_level (ThrowStrict)) { throw Exception (*this, "Cannot create export disk writer"); diff --git a/libs/pbd/file_archive.cc b/libs/pbd/file_archive.cc index 122d22b6d9..770822d285 100644 --- a/libs/pbd/file_archive.cc +++ b/libs/pbd/file_archive.cc @@ -59,7 +59,6 @@ write_callback (void* buffer, size_t size, size_t nmemb, void* d) static void* get_url (void* arg) { - pthread_set_name ("FileArchiveURL"); FileArchive::Request* r = (FileArchive::Request*) arg; CURL* curl; @@ -328,7 +327,10 @@ std::vector FileArchive::contents_url () { _req.mp.reset (); - pthread_create (&_tid, NULL, get_url, (void*)&_req); + + if (pthread_create_and_store ("FileArchiveHTTP", &_tid, get_url, (void*)&_req, 0)) { + return std::vector (); + } struct archive* a = setup_archive (); archive_read_open (a, (void*)&_req.mp, NULL, ar_read, NULL); @@ -359,7 +361,9 @@ int FileArchive::extract_url () { _req.mp.reset (); - pthread_create (&_tid, NULL, get_url, (void*)&_req); + if (pthread_create_and_store ("FileArchiveHTTP", &_tid, get_url, (void*)&_req)) { + return -1; + } struct archive* a = setup_archive (); archive_read_open (a, (void*)&_req.mp, NULL, ar_read, NULL); int rv = do_extract (a); diff --git a/libs/pbd/system_exec.cc b/libs/pbd/system_exec.cc index 0c100ab5b9..8570dfa598 100644 --- a/libs/pbd/system_exec.cc +++ b/libs/pbd/system_exec.cc @@ -278,9 +278,7 @@ SystemExec::~SystemExec () static void* interposer_thread (void *arg) { SystemExec *sex = static_cast(arg); - pthread_set_name ("ExecStdOut"); sex->output_interposer(); - pthread_exit(0); return 0; } @@ -522,7 +520,7 @@ SystemExec::start (StdErrMode stderr_mode, const char * /*vfork_exec_wrapper*/) return -1; } - int rv = pthread_create (&thread_id_tt, NULL, interposer_thread, this); + int rv = pthread_create_and_store ("ExecStdOut", &thread_id_tt, interposer_thread, this, 0); thread_active=true; if (rv) { thread_active=false; @@ -560,7 +558,6 @@ SystemExec::output_interposer() ReadStdout(rv, bytesRead); /* EMIT SIGNAL */ } Terminated(); /* EMIT SIGNAL */ - pthread_exit(0); } void @@ -808,7 +805,7 @@ SystemExec::start (StdErrMode stderr_mode, const char *vfork_exec_wrapper) close_fd (pout[1]); close_fd (pin[0]); - int rv = pthread_create (&thread_id_tt, NULL, interposer_thread, this); + int rv = pthread_create_and_store ("ExecStdOut", &thread_id_tt, interposer_thread, this, 0); thread_active=true; if (rv) { @@ -917,7 +914,6 @@ again: ReadStdout (rv, r); /* EMIT SIGNAL */ } Terminated (); /* EMIT SIGNAL */ - pthread_exit (0); } void