Problem: new thread naming breaks build on !Linux and overwrites prefix

Solution: use the thread class function rather than one of the pthread
functions, and take into account the thread prefix context option
This commit is contained in:
Luca Boccassi 2019-02-17 18:55:24 +00:00
parent 484374f2b6
commit de76789ac6
2 changed files with 14 additions and 17 deletions

View File

@ -422,23 +422,15 @@ void zmq::thread_ctx_t::start_thread (thread_t &thread_,
void *arg_,
const char *name_) const
{
static unsigned int nthreads_started = 0;
thread_.setSchedulingParameters (_thread_priority, _thread_sched_policy,
_thread_affinity_cpus);
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 ())
s << _thread_name_prefix << "/";
s << "ZMQbg/" << nthreads_started;
thread_.setThreadName (s.str ().c_str ());
#endif
nthreads_started++;
snprintf (namebuf, sizeof (namebuf), "%s%sZMQbg%s%s",
_thread_name_prefix.empty () ? "" : _thread_name_prefix.c_str (),
_thread_name_prefix.empty () ? "" : "/", name_ ? "/" : "",
name_ ? name_ : "");
thread_.start (tfn_, arg_, namebuf);
}
int zmq::thread_ctx_t::set (int option_, int optval_)

View File

@ -179,7 +179,7 @@ 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->setThreadName (self->_name.c_str ());
self->_tfn (self->_arg);
return NULL;
}
@ -311,20 +311,25 @@ void zmq::thread_t::setThreadName (const char *name_)
if (!name_)
return;
/* Fails with permission denied on Android 5/6 */
#if defined(ZMQ_HAVE_ANDROID)
return;
#endif
#if defined(ZMQ_HAVE_PTHREAD_SETNAME_1)
int rc = pthread_setname_np (name_);
if (rc)
return;
#elif defined(ZMQ_HAVE_PTHREAD_SETNAME_2)
int rc = pthread_setname_np (_descriptor, name_);
int rc = pthread_setname_np (pthread_self (), name_);
if (rc)
return;
#elif defined(ZMQ_HAVE_PTHREAD_SETNAME_3)
int rc = pthread_setname_np (_descriptor, name_, NULL);
int rc = pthread_setname_np (pthread_self (), name_, NULL);
if (rc)
return;
#elif defined(ZMQ_HAVE_PTHREAD_SET_NAME)
pthread_set_name_np (_descriptor, name_);
pthread_set_name_np (pthread_self (), name_);
#endif
}