Enable debugging for stored threads

This commit is contained in:
Robin Gareus 2024-09-27 23:05:02 +02:00
parent 301777e7fe
commit c4fdd5356c
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04

View file

@ -28,6 +28,8 @@
#include <dlfcn.h> #include <dlfcn.h>
#endif #endif
#include "pbd/compose.h"
#include "pbd/debug.h"
#include "pbd/failed_constructor.h" #include "pbd/failed_constructor.h"
#include "pbd/pthread_utils.h" #include "pbd/pthread_utils.h"
@ -47,7 +49,7 @@ DECLARE_DEFAULT_COMPARISONS (pthread_t) // Needed for 'DECLARE_DEFAULT_COMPARISO
using namespace std; using namespace std;
typedef std::list<pthread_t> ThreadMap; typedef std::map<pthread_t, std::string> ThreadMap;
static ThreadMap all_threads; static ThreadMap all_threads;
static pthread_mutex_t thread_map_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t thread_map_lock = PTHREAD_MUTEX_INITIALIZER;
static Glib::Threads::Private<char> thread_name (free); static Glib::Threads::Private<char> thread_name (free);
@ -94,6 +96,8 @@ fake_thread_start (void* arg)
/* name will be deleted by the default handler for GStaticPrivate, when the thread exits */ /* name will be deleted by the default handler for GStaticPrivate, when the thread exits */
pthread_set_name (ts->name.c_str ()); pthread_set_name (ts->name.c_str ());
DEBUG_TRACE (PBD::DEBUG::Threads, string_compose ("Started: '%1'\n", ts->name));
/* we don't need this object anymore */ /* we don't need this object anymore */
delete ts; delete ts;
@ -103,13 +107,13 @@ fake_thread_start (void* arg)
/* cleanup */ /* cleanup */
pthread_mutex_lock (&thread_map_lock); pthread_mutex_lock (&thread_map_lock);
for (ThreadMap::iterator i = all_threads.begin (); i != all_threads.end (); ++i) { for (auto const& t : all_threads) {
if (pthread_equal ((*i), pthread_self ())) { if (pthread_equal (t.first, pthread_self ())) {
all_threads.erase (i); DEBUG_TRACE (PBD::DEBUG::Threads, string_compose ("Terminated: '%1'\n", t.second));
all_threads.erase (t.first);
break; break;
} }
} }
pthread_mutex_unlock (&thread_map_lock); pthread_mutex_unlock (&thread_map_lock);
/* done */ /* done */
@ -132,7 +136,7 @@ pthread_create_and_store (string name, pthread_t* thread, void* (*start_routine)
if ((ret = pthread_create (thread, &default_attr, fake_thread_start, ts)) == 0) { if ((ret = pthread_create (thread, &default_attr, fake_thread_start, ts)) == 0) {
pthread_mutex_lock (&thread_map_lock); pthread_mutex_lock (&thread_map_lock);
all_threads.push_back (*thread); all_threads[*thread] = name;
pthread_mutex_unlock (&thread_map_lock); pthread_mutex_unlock (&thread_map_lock);
} }
@ -171,10 +175,12 @@ void
pthread_kill_all (int signum) pthread_kill_all (int signum)
{ {
pthread_mutex_lock (&thread_map_lock); pthread_mutex_lock (&thread_map_lock);
for (ThreadMap::iterator i = all_threads.begin (); i != all_threads.end (); ++i) { for (auto const& t : all_threads) {
if (!pthread_equal ((*i), pthread_self ())) { if (pthread_equal (t.first, pthread_self ())) {
pthread_kill ((*i), signum); continue;
} }
DEBUG_TRACE (PBD::DEBUG::Threads, string_compose ("Kill: '%1'\n", t.second));
pthread_kill (t.first, signum);
} }
all_threads.clear (); all_threads.clear ();
pthread_mutex_unlock (&thread_map_lock); pthread_mutex_unlock (&thread_map_lock);
@ -184,16 +190,12 @@ void
pthread_cancel_all () pthread_cancel_all ()
{ {
pthread_mutex_lock (&thread_map_lock); pthread_mutex_lock (&thread_map_lock);
for (auto const& t : all_threads) {
for (ThreadMap::iterator i = all_threads.begin (); i != all_threads.end ();) { if (pthread_equal (t.first, pthread_self ())) {
ThreadMap::iterator nxt = i; continue;
++nxt;
if (!pthread_equal ((*i), pthread_self ())) {
pthread_cancel ((*i));
} }
DEBUG_TRACE (PBD::DEBUG::Threads, string_compose ("Cancel: '%1'\n", t.second));
i = nxt; pthread_cancel (t.first);
} }
all_threads.clear (); all_threads.clear ();
pthread_mutex_unlock (&thread_map_lock); pthread_mutex_unlock (&thread_map_lock);
@ -203,9 +205,9 @@ void
pthread_cancel_one (pthread_t thread) pthread_cancel_one (pthread_t thread)
{ {
pthread_mutex_lock (&thread_map_lock); pthread_mutex_lock (&thread_map_lock);
for (ThreadMap::iterator i = all_threads.begin (); i != all_threads.end (); ++i) { for (auto const& t : all_threads) {
if (pthread_equal ((*i), thread)) { if (pthread_equal (t.first, thread)) {
all_threads.erase (i); all_threads.erase (t.first);
break; break;
} }
} }