name threads

This commit is contained in:
David Rose 2009-12-16 17:27:19 +00:00
parent 5755ae390c
commit 265dfba3ad
8 changed files with 54 additions and 15 deletions

View File

@ -95,6 +95,16 @@ get_max_poll_cycle() {
return *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 ConfigVariableInt net_max_read_per_epoch
("net-max-read-per-epoch", 1024, ("net-max-read-per-epoch", 1024,
PRC_DESC("The maximum number of bytes to read from the net in a single " PRC_DESC("The maximum number of bytes to read from the net in a single "

View File

@ -30,6 +30,7 @@ extern int get_net_max_write_queue();
extern int get_net_max_response_queue(); extern int get_net_max_response_queue();
extern bool get_net_error_abort(); extern bool get_net_error_abort();
extern double get_max_poll_cycle(); 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_read_per_epoch;
extern ConfigVariableInt net_max_write_per_epoch; extern ConfigVariableInt net_max_write_per_epoch;

View File

@ -19,14 +19,23 @@
#include "config_net.h" #include "config_net.h"
#include "socket_tcp_listen.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 // Function: ConnectionListener::Constructor
// Access: Public // Access: Public
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
ConnectionListener:: ConnectionListener::
ConnectionListener(ConnectionManager *manager, int num_threads) : ConnectionListener(ConnectionManager *manager, int num_threads,
ConnectionReader(manager, num_threads) const string &thread_name) :
ConnectionReader(manager, num_threads, listener_thread_name(thread_name))
{ {
} }

View File

@ -34,7 +34,8 @@ class NetAddress;
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
class EXPCL_PANDA_NET ConnectionListener : public ConnectionReader { class EXPCL_PANDA_NET ConnectionListener : public ConnectionReader {
PUBLISHED: PUBLISHED:
ConnectionListener(ConnectionManager *manager, int num_threads); ConnectionListener(ConnectionManager *manager, int num_threads,
const string &thread_name = string());
protected: protected:
virtual void receive_datagram(const NetDatagram &datagram); virtual void receive_datagram(const NetDatagram &datagram);

View File

@ -70,8 +70,10 @@ get_socket() const {
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
ConnectionReader::ReaderThread:: ConnectionReader::ReaderThread::
ReaderThread(ConnectionReader *reader, int thread_index) : ReaderThread(ConnectionReader *reader, const string &thread_name,
Thread("ReaderThread", "ReaderThread"), int thread_index) :
Thread(make_thread_name(thread_name, thread_index),
make_thread_name(thread_name, thread_index)),
_reader(reader), _reader(reader),
_thread_index(thread_index) _thread_index(thread_index)
{ {
@ -97,7 +99,8 @@ thread_main() {
// (QueuedConnectionReader will do this automatically.) // (QueuedConnectionReader will do this automatically.)
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
ConnectionReader:: ConnectionReader::
ConnectionReader(ConnectionManager *manager, int num_threads) : ConnectionReader(ConnectionManager *manager, int num_threads,
const string &thread_name) :
_manager(manager) _manager(manager)
{ {
if (!Thread::is_threading_supported()) { if (!Thread::is_threading_supported()) {
@ -123,9 +126,13 @@ ConnectionReader(ConnectionManager *manager, int num_threads) :
_currently_polling_thread = -1; _currently_polling_thread = -1;
string reader_thread_name = thread_name;
if (thread_name.empty()) {
reader_thread_name = "ReaderThread";
}
int i; int i;
for (i = 0; i < num_threads; 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); _threads.push_back(thread);
} }
for (i = 0; i < num_threads; i++) { for (i = 0; i < num_threads; i++) {

View File

@ -67,7 +67,8 @@ PUBLISHED:
// the arrays returned by a previous call to PR_Poll(), or (b) // the arrays returned by a previous call to PR_Poll(), or (b)
// execute (and possibly block on) a new call to PR_Poll(). // 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(); virtual ~ConnectionReader();
bool add_connection(Connection *connection); bool add_connection(Connection *connection);
@ -140,7 +141,8 @@ private:
class ReaderThread : public Thread { class ReaderThread : public Thread {
public: public:
ReaderThread(ConnectionReader *reader, int thread_index); ReaderThread(ConnectionReader *reader, const string &thread_name,
int thread_index);
virtual void thread_main(); virtual void thread_main();
ConnectionReader *_reader; ConnectionReader *_reader;

View File

@ -27,8 +27,10 @@
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
ConnectionWriter::WriterThread:: ConnectionWriter::WriterThread::
WriterThread(ConnectionWriter *writer, int thread_index) : WriterThread(ConnectionWriter *writer, const string &thread_name,
Thread("WriterThread", "WriterThread"), int thread_index) :
Thread(make_thread_name(thread_name, thread_index),
make_thread_name(thread_name, thread_index)),
_writer(writer), _writer(writer),
_thread_index(thread_index) _thread_index(thread_index)
{ {
@ -55,7 +57,8 @@ thread_main() {
// transmission by a thread. // transmission by a thread.
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
ConnectionWriter:: ConnectionWriter::
ConnectionWriter(ConnectionManager *manager, int num_threads) : ConnectionWriter(ConnectionManager *manager, int num_threads,
const string &thread_name) :
_manager(manager) _manager(manager)
{ {
if (!Thread::is_threading_supported()) { if (!Thread::is_threading_supported()) {
@ -75,9 +78,13 @@ ConnectionWriter(ConnectionManager *manager, int num_threads) :
_immediate = (num_threads <= 0); _immediate = (num_threads <= 0);
_shutdown = false; _shutdown = false;
string writer_thread_name = thread_name;
if (thread_name.empty()) {
writer_thread_name = "WriterThread";
}
int i; int i;
for (i = 0; i < num_threads; 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); _threads.push_back(thread);
} }
for (i = 0; i < num_threads; i++) { for (i = 0; i < num_threads; i++) {

View File

@ -37,7 +37,8 @@ class NetAddress;
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
class EXPCL_PANDA_NET ConnectionWriter { class EXPCL_PANDA_NET ConnectionWriter {
PUBLISHED: PUBLISHED:
ConnectionWriter(ConnectionManager *manager, int num_threads); ConnectionWriter(ConnectionManager *manager, int num_threads,
const string &thread_name = string());
~ConnectionWriter(); ~ConnectionWriter();
void set_max_queue_size(int max_size); void set_max_queue_size(int max_size);
@ -85,7 +86,8 @@ private:
class WriterThread : public Thread { class WriterThread : public Thread {
public: public:
WriterThread(ConnectionWriter *writer, int thread_index); WriterThread(ConnectionWriter *writer, const string &thread_name,
int thread_index);
virtual void thread_main(); virtual void thread_main();
ConnectionWriter *_writer; ConnectionWriter *_writer;