Commit f25cd6e7 authored by f18m's avatar f18m Committed by Luca Boccassi

Background thread names (#2784)

* Add ZMQ_THREAD_NAME_PREFIX ctx option
parent 9af03e22
...@@ -613,6 +613,7 @@ ZMQ_EXPORT void zmq_threadclose (void* thread); ...@@ -613,6 +613,7 @@ ZMQ_EXPORT void zmq_threadclose (void* thread);
#define ZMQ_MSG_T_SIZE 6 #define ZMQ_MSG_T_SIZE 6
#define ZMQ_THREAD_AFFINITY 7 #define ZMQ_THREAD_AFFINITY 7
#define ZMQ_THREAD_AFFINITY_DFLT -1 #define ZMQ_THREAD_AFFINITY_DFLT -1
#define ZMQ_THREAD_NAME_PREFIX 8
/* DRAFT Socket methods. */ /* DRAFT Socket methods. */
ZMQ_EXPORT int zmq_join (void *s, const char *group); ZMQ_EXPORT int zmq_join (void *s, const char *group);
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <limits> #include <limits>
#include <climits> #include <climits>
#include <new> #include <new>
#include <sstream>
#include <string.h> #include <string.h>
#include "ctx.hpp" #include "ctx.hpp"
...@@ -256,6 +257,13 @@ int zmq::ctx_t::set (int option_, int optval_) ...@@ -256,6 +257,13 @@ int zmq::ctx_t::set (int option_, int optval_)
thread_affinity = optval_; thread_affinity = optval_;
} }
else 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) { if (option_ == ZMQ_BLOCKY && optval_ >= 0) {
scoped_lock_t locker(opt_sync); scoped_lock_t locker(opt_sync);
blocky = (optval_ != 0); blocky = (optval_ != 0);
...@@ -401,11 +409,16 @@ zmq::object_t *zmq::ctx_t::get_reaper () ...@@ -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 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_.setSchedulingParameters(thread_priority, thread_sched_policy, thread_affinity);
thread_.start(tfn_, arg_); thread_.start(tfn_, arg_);
#ifndef ZMQ_HAVE_ANDROID #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 #endif
nthreads_started++;
} }
void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_) void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_)
......
...@@ -211,10 +211,11 @@ namespace zmq ...@@ -211,10 +211,11 @@ namespace zmq
// Is IPv6 enabled on this context? // Is IPv6 enabled on this context?
bool ipv6; bool ipv6;
// Thread scheduling parameters. // Thread parameters.
int thread_priority; int thread_priority;
int thread_sched_policy; int thread_sched_policy;
int thread_affinity; int thread_affinity;
std::string thread_name_prefix;
// Synchronisation of access to context options. // Synchronisation of access to context options.
mutex_t opt_sync; mutex_t opt_sync;
......
...@@ -93,6 +93,7 @@ ...@@ -93,6 +93,7 @@
#define ZMQ_MSG_T_SIZE 6 #define ZMQ_MSG_T_SIZE 6
#define ZMQ_THREAD_AFFINITY 7 #define ZMQ_THREAD_AFFINITY 7
#define ZMQ_THREAD_AFFINITY_DFLT -1 #define ZMQ_THREAD_AFFINITY_DFLT -1
#define ZMQ_THREAD_NAME_PREFIX 8
/* DRAFT Socket methods. */ /* DRAFT Socket methods. */
int zmq_join (void *s, const char *group); int zmq_join (void *s, const char *group);
......
...@@ -124,6 +124,14 @@ void test_ctx_thread_opts(void* ctx) ...@@ -124,6 +124,14 @@ void test_ctx_thread_opts(void* ctx)
rc = zmq_ctx_set(ctx, ZMQ_THREAD_AFFINITY, cpu_affinity_test); rc = zmq_ctx_set(ctx, ZMQ_THREAD_AFFINITY, cpu_affinity_test);
assert (rc == 0); assert (rc == 0);
#endif #endif
#ifdef ZMQ_THREAD_NAME_PREFIX
// test thread name prefix:
rc = zmq_ctx_set(ctx, ZMQ_THREAD_NAME_PREFIX, 1234);
assert (rc == 0);
#endif
} }
......
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