Background thread names (#2784)

* Add ZMQ_THREAD_NAME_PREFIX ctx option
This commit is contained in:
f18m 2017-10-17 13:06:50 +02:00 committed by Luca Boccassi
parent 9af03e2214
commit f25cd6e7be
5 changed files with 26 additions and 2 deletions

View File

@ -613,6 +613,7 @@ ZMQ_EXPORT void zmq_threadclose (void* thread);
#define ZMQ_MSG_T_SIZE 6
#define ZMQ_THREAD_AFFINITY 7
#define ZMQ_THREAD_AFFINITY_DFLT -1
#define ZMQ_THREAD_NAME_PREFIX 8
/* DRAFT Socket methods. */
ZMQ_EXPORT int zmq_join (void *s, const char *group);

View File

@ -36,6 +36,7 @@
#include <limits>
#include <climits>
#include <new>
#include <sstream>
#include <string.h>
#include "ctx.hpp"
@ -256,6 +257,13 @@ int zmq::ctx_t::set (int option_, int optval_)
thread_affinity = optval_;
}
else
if (option_ == ZMQ_THREAD_NAME_PREFIX && optval_ >= 0) {
std::ostringstream s;
s << optval_;
scoped_lock_t locker(opt_sync);
thread_name_prefix = s.str();
}
else
if (option_ == ZMQ_BLOCKY && optval_ >= 0) {
scoped_lock_t locker(opt_sync);
blocky = (optval_ != 0);
@ -401,11 +409,16 @@ zmq::object_t *zmq::ctx_t::get_reaper ()
void zmq::ctx_t::start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const
{
static unsigned int nthreads_started = 0;
thread_.setSchedulingParameters(thread_priority, thread_sched_policy, thread_affinity);
thread_.start(tfn_, arg_);
#ifndef ZMQ_HAVE_ANDROID
thread_.setThreadName ("ZMQ background");
std::ostringstream s;
s << thread_name_prefix << "/ZMQbg/" << nthreads_started;
thread_.setThreadName (s.str().c_str());
#endif
nthreads_started++;
}
void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_)

View File

@ -211,10 +211,11 @@ namespace zmq
// Is IPv6 enabled on this context?
bool ipv6;
// Thread scheduling parameters.
// Thread parameters.
int thread_priority;
int thread_sched_policy;
int thread_affinity;
std::string thread_name_prefix;
// Synchronisation of access to context options.
mutex_t opt_sync;

View File

@ -93,6 +93,7 @@
#define ZMQ_MSG_T_SIZE 6
#define ZMQ_THREAD_AFFINITY 7
#define ZMQ_THREAD_AFFINITY_DFLT -1
#define ZMQ_THREAD_NAME_PREFIX 8
/* DRAFT Socket methods. */
int zmq_join (void *s, const char *group);

View File

@ -124,6 +124,14 @@ void test_ctx_thread_opts(void* ctx)
rc = zmq_ctx_set(ctx, ZMQ_THREAD_AFFINITY, cpu_affinity_test);
assert (rc == 0);
#endif
#ifdef ZMQ_THREAD_NAME_PREFIX
// test thread name prefix:
rc = zmq_ctx_set(ctx, ZMQ_THREAD_NAME_PREFIX, 1234);
assert (rc == 0);
#endif
}