From 265dfba3adba0a0aa86bd1fa56a28f4e52bbb8a3 Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 16 Dec 2009 17:27:19 +0000 Subject: [PATCH] name threads --- panda/src/net/config_net.cxx | 10 ++++++++++ panda/src/net/config_net.h | 1 + panda/src/net/connectionListener.cxx | 13 +++++++++++-- panda/src/net/connectionListener.h | 3 ++- panda/src/net/connectionReader.cxx | 15 +++++++++++---- panda/src/net/connectionReader.h | 6 ++++-- panda/src/net/connectionWriter.cxx | 15 +++++++++++---- panda/src/net/connectionWriter.h | 6 ++++-- 8 files changed, 54 insertions(+), 15 deletions(-) diff --git a/panda/src/net/config_net.cxx b/panda/src/net/config_net.cxx index d1f5f9d19f..2c2d6c0fe7 100644 --- a/panda/src/net/config_net.cxx +++ b/panda/src/net/config_net.cxx @@ -95,6 +95,16 @@ get_max_poll_cycle() { return *max_poll_cycle; } +// This function is used in the ReaderThread and WriterThread +// constructors to make a simple name for each thread. +string +make_thread_name(const string &thread_name, int thread_index) { + ostringstream stream; + stream << thread_name << "_" << thread_index; + return stream.str(); +} + + ConfigVariableInt net_max_read_per_epoch ("net-max-read-per-epoch", 1024, PRC_DESC("The maximum number of bytes to read from the net in a single " diff --git a/panda/src/net/config_net.h b/panda/src/net/config_net.h index 4bcd278cac..3d92527f7e 100644 --- a/panda/src/net/config_net.h +++ b/panda/src/net/config_net.h @@ -30,6 +30,7 @@ extern int get_net_max_write_queue(); extern int get_net_max_response_queue(); extern bool get_net_error_abort(); extern double get_max_poll_cycle(); +extern string make_thread_name(const string &thread_name, int thread_index); extern ConfigVariableInt net_max_read_per_epoch; extern ConfigVariableInt net_max_write_per_epoch; diff --git a/panda/src/net/connectionListener.cxx b/panda/src/net/connectionListener.cxx index 7b569fb758..b4059ecb71 100644 --- a/panda/src/net/connectionListener.cxx +++ b/panda/src/net/connectionListener.cxx @@ -19,14 +19,23 @@ #include "config_net.h" #include "socket_tcp_listen.h" +static string +listener_thread_name(const string &thread_name) { + if (!thread_name.empty()) { + return thread_name; + } + return "ListenerThread"; +} + //////////////////////////////////////////////////////////////////// // Function: ConnectionListener::Constructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// ConnectionListener:: -ConnectionListener(ConnectionManager *manager, int num_threads) : - ConnectionReader(manager, num_threads) +ConnectionListener(ConnectionManager *manager, int num_threads, + const string &thread_name) : + ConnectionReader(manager, num_threads, listener_thread_name(thread_name)) { } diff --git a/panda/src/net/connectionListener.h b/panda/src/net/connectionListener.h index 9b1232bb51..e2792834e1 100644 --- a/panda/src/net/connectionListener.h +++ b/panda/src/net/connectionListener.h @@ -34,7 +34,8 @@ class NetAddress; //////////////////////////////////////////////////////////////////// class EXPCL_PANDA_NET ConnectionListener : public ConnectionReader { PUBLISHED: - ConnectionListener(ConnectionManager *manager, int num_threads); + ConnectionListener(ConnectionManager *manager, int num_threads, + const string &thread_name = string()); protected: virtual void receive_datagram(const NetDatagram &datagram); diff --git a/panda/src/net/connectionReader.cxx b/panda/src/net/connectionReader.cxx index 6f38dc8e73..b7ccdb3e7c 100644 --- a/panda/src/net/connectionReader.cxx +++ b/panda/src/net/connectionReader.cxx @@ -70,8 +70,10 @@ get_socket() const { // Description: //////////////////////////////////////////////////////////////////// ConnectionReader::ReaderThread:: -ReaderThread(ConnectionReader *reader, int thread_index) : - Thread("ReaderThread", "ReaderThread"), +ReaderThread(ConnectionReader *reader, const string &thread_name, + int thread_index) : + Thread(make_thread_name(thread_name, thread_index), + make_thread_name(thread_name, thread_index)), _reader(reader), _thread_index(thread_index) { @@ -97,7 +99,8 @@ thread_main() { // (QueuedConnectionReader will do this automatically.) //////////////////////////////////////////////////////////////////// ConnectionReader:: -ConnectionReader(ConnectionManager *manager, int num_threads) : +ConnectionReader(ConnectionManager *manager, int num_threads, + const string &thread_name) : _manager(manager) { if (!Thread::is_threading_supported()) { @@ -123,9 +126,13 @@ ConnectionReader(ConnectionManager *manager, int num_threads) : _currently_polling_thread = -1; + string reader_thread_name = thread_name; + if (thread_name.empty()) { + reader_thread_name = "ReaderThread"; + } int i; for (i = 0; i < num_threads; i++) { - PT(ReaderThread) thread = new ReaderThread(this, i); + PT(ReaderThread) thread = new ReaderThread(this, reader_thread_name, i); _threads.push_back(thread); } for (i = 0; i < num_threads; i++) { diff --git a/panda/src/net/connectionReader.h b/panda/src/net/connectionReader.h index 74dc812fd1..6cd2fbbbc5 100644 --- a/panda/src/net/connectionReader.h +++ b/panda/src/net/connectionReader.h @@ -67,7 +67,8 @@ PUBLISHED: // the arrays returned by a previous call to PR_Poll(), or (b) // execute (and possibly block on) a new call to PR_Poll(). - ConnectionReader(ConnectionManager *manager, int num_threads); + ConnectionReader(ConnectionManager *manager, int num_threads, + const string &thread_name = string()); virtual ~ConnectionReader(); bool add_connection(Connection *connection); @@ -140,7 +141,8 @@ private: class ReaderThread : public Thread { public: - ReaderThread(ConnectionReader *reader, int thread_index); + ReaderThread(ConnectionReader *reader, const string &thread_name, + int thread_index); virtual void thread_main(); ConnectionReader *_reader; diff --git a/panda/src/net/connectionWriter.cxx b/panda/src/net/connectionWriter.cxx index 1363a64a03..13a9be4ee8 100644 --- a/panda/src/net/connectionWriter.cxx +++ b/panda/src/net/connectionWriter.cxx @@ -27,8 +27,10 @@ // Description: //////////////////////////////////////////////////////////////////// ConnectionWriter::WriterThread:: -WriterThread(ConnectionWriter *writer, int thread_index) : - Thread("WriterThread", "WriterThread"), +WriterThread(ConnectionWriter *writer, const string &thread_name, + int thread_index) : + Thread(make_thread_name(thread_name, thread_index), + make_thread_name(thread_name, thread_index)), _writer(writer), _thread_index(thread_index) { @@ -55,7 +57,8 @@ thread_main() { // transmission by a thread. //////////////////////////////////////////////////////////////////// ConnectionWriter:: -ConnectionWriter(ConnectionManager *manager, int num_threads) : +ConnectionWriter(ConnectionManager *manager, int num_threads, + const string &thread_name) : _manager(manager) { if (!Thread::is_threading_supported()) { @@ -75,9 +78,13 @@ ConnectionWriter(ConnectionManager *manager, int num_threads) : _immediate = (num_threads <= 0); _shutdown = false; + string writer_thread_name = thread_name; + if (thread_name.empty()) { + writer_thread_name = "WriterThread"; + } int i; for (i = 0; i < num_threads; i++) { - PT(WriterThread) thread = new WriterThread(this, i); + PT(WriterThread) thread = new WriterThread(this, writer_thread_name, i); _threads.push_back(thread); } for (i = 0; i < num_threads; i++) { diff --git a/panda/src/net/connectionWriter.h b/panda/src/net/connectionWriter.h index f8b13fbb32..39f073bee0 100644 --- a/panda/src/net/connectionWriter.h +++ b/panda/src/net/connectionWriter.h @@ -37,7 +37,8 @@ class NetAddress; //////////////////////////////////////////////////////////////////// class EXPCL_PANDA_NET ConnectionWriter { PUBLISHED: - ConnectionWriter(ConnectionManager *manager, int num_threads); + ConnectionWriter(ConnectionManager *manager, int num_threads, + const string &thread_name = string()); ~ConnectionWriter(); void set_max_queue_size(int max_size); @@ -85,7 +86,8 @@ private: class WriterThread : public Thread { public: - WriterThread(ConnectionWriter *writer, int thread_index); + WriterThread(ConnectionWriter *writer, const string &thread_name, + int thread_index); virtual void thread_main(); ConnectionWriter *_writer;