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_, void *arg_,
const char *name_) const const char *name_) const
{ {
static unsigned int nthreads_started = 0;
thread_.setSchedulingParameters (_thread_priority, _thread_sched_policy, thread_.setSchedulingParameters (_thread_priority, _thread_sched_policy,
_thread_affinity_cpus); _thread_affinity_cpus);
char namebuf[16]; char namebuf[16] = "";
snprintf(namebuf, sizeof(namebuf), "0MQ%s%s", #ifdef ZMQ_HAVE_WINDOWS
name_ ? ":" : "", name_ ? name_ : ""); LIBZMQ_UNUSED (name_);
thread_.start(tfn_, arg_, namebuf); #else
#ifndef ZMQ_HAVE_ANDROID snprintf (namebuf, sizeof (namebuf), "%s%sZMQbg%s%s",
std::ostringstream s; _thread_name_prefix.empty () ? "" : _thread_name_prefix.c_str (),
if (!_thread_name_prefix.empty ()) _thread_name_prefix.empty () ? "" : "/", name_ ? "/" : "",
s << _thread_name_prefix << "/"; name_ ? name_ : "");
s << "ZMQbg/" << nthreads_started;
thread_.setThreadName (s.str ().c_str ());
#endif #endif
nthreads_started++; thread_.start (tfn_, arg_, namebuf);
} }
int zmq::thread_ctx_t::set (int option_, int optval_) 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 () void zmq::io_thread_t::start ()
{ {
char name[16]; char name[16] = "";
snprintf(name, sizeof(name), "IO %u", get_tid() - zmq::ctx_t::reaper_tid); #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. // Start the underlying I/O thread.
_poller->start (name); _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_) void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
{ {
LIBZMQ_UNUSED (name_);
_tfn = tfn_; _tfn = tfn_;
_arg = arg_; _arg = arg_;
#if defined _WIN32_WCE #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_) void zmq::thread_t::start (thread_fn *tfn_, void *arg_, const char *name_)
{ {
LIBZMQ_UNUSED (name_);
_tfn = tfn_; _tfn = tfn_;
_arg = arg_; _arg = arg_;
_descriptor = taskSpawn (NULL, DEFAULT_PRIORITY, DEFAULT_OPTIONS, _descriptor = taskSpawn (NULL, DEFAULT_PRIORITY, DEFAULT_OPTIONS,
@ -179,7 +181,7 @@ static void *thread_routine (void *arg_)
#endif #endif
zmq::thread_t *self = (zmq::thread_t *) arg_; zmq::thread_t *self = (zmq::thread_t *) arg_;
self->applySchedulingParameters (); self->applySchedulingParameters ();
pthread_setname_np(pthread_self(), self->_name.c_str()); self->setThreadName (self->_name.c_str ());
self->_tfn (self->_arg); self->_tfn (self->_arg);
return NULL; return NULL;
} }
@ -311,20 +313,25 @@ void zmq::thread_t::setThreadName (const char *name_)
if (!name_) if (!name_)
return; return;
/* Fails with permission denied on Android 5/6 */
#if defined(ZMQ_HAVE_ANDROID)
return;
#endif
#if defined(ZMQ_HAVE_PTHREAD_SETNAME_1) #if defined(ZMQ_HAVE_PTHREAD_SETNAME_1)
int rc = pthread_setname_np (name_); int rc = pthread_setname_np (name_);
if (rc) if (rc)
return; return;
#elif defined(ZMQ_HAVE_PTHREAD_SETNAME_2) #elif defined(ZMQ_HAVE_PTHREAD_SETNAME_2)
int rc = pthread_setname_np (_descriptor, name_); int rc = pthread_setname_np (pthread_self (), name_);
if (rc) if (rc)
return; return;
#elif defined(ZMQ_HAVE_PTHREAD_SETNAME_3) #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) if (rc)
return; return;
#elif defined(ZMQ_HAVE_PTHREAD_SET_NAME) #elif defined(ZMQ_HAVE_PTHREAD_SET_NAME)
pthread_set_name_np (_descriptor, name_); pthread_set_name_np (pthread_self (), name_);
#endif #endif
} }

View File

@ -56,6 +56,7 @@ class thread_t
inline thread_t () : inline thread_t () :
_tfn (NULL), _tfn (NULL),
_arg (NULL), _arg (NULL),
_name (""),
_started (false), _started (false),
_thread_priority (ZMQ_THREAD_PRIORITY_DFLT), _thread_priority (ZMQ_THREAD_PRIORITY_DFLT),
_thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_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 // Creates OS thread. 'tfn' is main thread function. It'll be passed
// 'arg' as an argument. // '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. // Returns whether the thread was started, i.e. start was called.
bool get_started () const; bool get_started () const;
@ -100,9 +101,7 @@ class thread_t
void applySchedulingParameters (); void applySchedulingParameters ();
thread_fn *_tfn; thread_fn *_tfn;
void *_arg; void *_arg;
#ifndef ZMQ_HAVE_WINDOWS
std::string _name; std::string _name;
#endif
private: private:
bool _started; 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; zmq::thread_t *thread = new (std::nothrow) zmq::thread_t;
alloc_assert (thread); alloc_assert (thread);
thread->start (func_, arg_); thread->start (func_, arg_, "ZMQapp");
return thread; return thread;
} }