Commit 484374f2 authored by Kymeta Corp's avatar Kymeta Corp Committed by Luca Boccassi

Problem: Threads don't have names which complicates debugging.

Solution:
1. Use optional name parameter in thread_t::start for operating
systems that have thread names.
2. Give start_thread() an optional name parameter for the
thread's name. If this parameter is set, it will be appended to "0MQ:".
If not set, "0MQ" will be used as the thread's name.
3. Give epoll the ability to name its thread. Then use this in
io_thread and reaper to name them.
parent c47b2af9
......@@ -419,13 +419,18 @@ zmq::thread_ctx_t::thread_ctx_t () :
void zmq::thread_ctx_t::start_thread (thread_t &thread_,
thread_fn *tfn_,
void *arg_) const
void *arg_,
const char *name_) const
{
static unsigned int nthreads_started = 0;
thread_.setSchedulingParameters (_thread_priority, _thread_sched_policy,
_thread_affinity_cpus);
thread_.start (tfn_, arg_);
char namebuf[16];
snprintf(namebuf, sizeof(namebuf), "0MQ%s%s",
name_ ? ":" : "", name_ ? name_ : "");
thread_.start(tfn_, arg_, namebuf);
#ifndef ZMQ_HAVE_ANDROID
std::ostringstream s;
if (!_thread_name_prefix.empty ())
......
......@@ -67,7 +67,10 @@ class thread_ctx_t
thread_ctx_t ();
// Start a new thread with proper scheduling parameters.
void start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const;
void start_thread (thread_t &thread_,
thread_fn *tfn_,
void *arg_,
const char *name_ = NULL) const;
int set (int option_, int optval_);
int get (int option_);
......
......@@ -56,8 +56,10 @@ zmq::io_thread_t::~io_thread_t ()
void zmq::io_thread_t::start ()
{
char name[16];
snprintf(name, sizeof(name), "IO %u", get_tid() - zmq::ctx_t::reaper_tid);
// Start the underlying I/O thread.
_poller->start ();
_poller->start (name);
}
void zmq::io_thread_t::stop ()
......
......@@ -121,10 +121,10 @@ void zmq::worker_poller_base_t::stop_worker ()
_worker.stop ();
}
void zmq::worker_poller_base_t::start ()
void zmq::worker_poller_base_t::start (const char *name_)
{
zmq_assert (get_load () > 0);
_ctx.start_thread (_worker, worker_routine, this);
_ctx.start_thread (_worker, worker_routine, this, name_);
}
void zmq::worker_poller_base_t::check_thread ()
......
......@@ -166,7 +166,7 @@ class worker_poller_base_t : public poller_base_t
worker_poller_base_t (const thread_ctx_t &ctx_);
// Methods from the poller concept.
void start ();
void start (const char *name = NULL);
protected:
// Checks whether the currently executing thread is the worker thread
......
......@@ -71,7 +71,7 @@ void zmq::reaper_t::start ()
zmq_assert (_mailbox.valid ());
// Start the thread.
_poller->start ();
_poller->start ("Reaper");
}
void zmq::reaper_t::stop ()
......
......@@ -52,7 +52,7 @@ static unsigned int __stdcall thread_routine (void *arg_)
}
}
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
{
_tfn = tfn_;
_arg = arg_;
......@@ -109,7 +109,7 @@ static void *thread_routine (void *arg_)
}
}
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
{
_tfn = tfn_;
_arg = arg_;
......@@ -179,15 +179,17 @@ static void *thread_routine (void *arg_)
#endif
zmq::thread_t *self = (zmq::thread_t *) arg_;
self->applySchedulingParameters ();
pthread_setname_np(pthread_self(), self->_name.c_str());
self->_tfn (self->_arg);
return NULL;
}
}
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
{
_tfn = tfn_;
_arg = arg_;
_name = name_;
int rc = pthread_create (&_descriptor, NULL, thread_routine, this);
posix_assert (rc);
_started = true;
......
......@@ -37,6 +37,7 @@
#include <pthread.h>
#endif
#include <set>
#include <string>
namespace zmq
{
......@@ -72,7 +73,7 @@ class thread_t
// Creates OS thread. 'tfn' is main thread function. It'll be passed
// 'arg' as an argument.
void start (thread_fn *tfn_, void *arg_);
void start (thread_fn *tfn_, void *arg_, const char *name_ = "0MQ");
// Returns whether the thread was started, i.e. start was called.
bool get_started () const;
......@@ -99,6 +100,9 @@ class thread_t
void applySchedulingParameters ();
thread_fn *_tfn;
void *_arg;
#ifndef ZMQ_HAVE_WINDOWS
std::string _name;
#endif
private:
bool _started;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment