Merge pull request #3411 from bluca/fix_threads

Problem: non-linux build broken by last PR
This commit is contained in:
Simon Giesecke 2019-02-17 22:39:06 +01:00 committed by GitHub
commit be20089675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 23 deletions

View File

@ -422,23 +422,19 @@ 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 ());
char namebuf[16] = "";
#ifdef ZMQ_HAVE_WINDOWS
LIBZMQ_UNUSED (name_);
#else
snprintf (namebuf, sizeof (namebuf), "%s%sZMQbg%s%s",
_thread_name_prefix.empty () ? "" : _thread_name_prefix.c_str (),
_thread_name_prefix.empty () ? "" : "/", name_ ? "/" : "",
name_ ? name_ : "");
#endif
nthreads_started++;
thread_.start (tfn_, arg_, namebuf);
}
int zmq::thread_ctx_t::set (int option_, int optval_)

View File

@ -56,8 +56,11 @@ 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);
char name[16] = "";
#ifndef ZMQ_HAVE_WINDOWS
snprintf (name, sizeof (name), "IO/%u",
get_tid () - zmq::ctx_t::reaper_tid - 1);
#endif
// Start the underlying I/O thread.
_poller->start (name);
}

View File

@ -54,6 +54,7 @@ static unsigned int __stdcall thread_routine (void *arg_)
void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
{
LIBZMQ_UNUSED (name_);
_tfn = tfn_;
_arg = arg_;
#if defined _WIN32_WCE
@ -111,6 +112,7 @@ static void *thread_routine (void *arg_)
void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
{
LIBZMQ_UNUSED (name_);
_tfn = tfn_;
_arg = arg_;
_descriptor = taskSpawn (NULL, DEFAULT_PRIORITY, DEFAULT_OPTIONS,
@ -179,7 +181,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 +313,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
}

View File

@ -56,6 +56,7 @@ class thread_t
inline thread_t () :
_tfn (NULL),
_arg (NULL),
_name (""),
_started (false),
_thread_priority (ZMQ_THREAD_PRIORITY_DFLT),
_thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT)
@ -73,7 +74,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_, const char *name_ = "0MQ");
void start (thread_fn *tfn_, void *arg_, const char *name_);
// Returns whether the thread was started, i.e. start was called.
bool get_started () const;
@ -100,9 +101,7 @@ class thread_t
void applySchedulingParameters ();
thread_fn *_tfn;
void *_arg;
#ifndef ZMQ_HAVE_WINDOWS
std::string _name;
#endif
private:
bool _started;

View File

@ -84,7 +84,7 @@ void *zmq_threadstart (zmq_thread_fn *func_, void *arg_)
{
zmq::thread_t *thread = new (std::nothrow) zmq::thread_t;
alloc_assert (thread);
thread->start (func_, arg_);
thread->start (func_, arg_, "ZMQapp");
return thread;
}