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;
}
// 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 "

View File

@ -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;

View File

@ -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))
{
}

View File

@ -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);

View File

@ -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++) {

View File

@ -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;

View File

@ -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++) {

View File

@ -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;