From 3666a49022f84b3d1888f630a78f3f8f28aadde1 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Sat, 29 Aug 2009 10:32:21 +0200 Subject: bug in identifying current thread fixed --- src/app_thread.cpp | 8 +++++--- src/app_thread.hpp | 9 ++++++--- src/thread.cpp | 20 ++++++++++++++++++++ src/thread.hpp | 9 +++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/app_thread.cpp b/src/app_thread.cpp index db73ec1..e4e5b19 100644 --- a/src/app_thread.cpp +++ b/src/app_thread.cpp @@ -44,7 +44,7 @@ zmq::app_thread_t::app_thread_t (dispatcher_t *dispatcher_, int thread_slot_) : object_t (dispatcher_, thread_slot_), - tid (0), + associated (false), last_processing_time (0) { } @@ -63,7 +63,8 @@ zmq::i_signaler *zmq::app_thread_t::get_signaler () bool zmq::app_thread_t::is_current () { - return !sockets.empty () && tid == getpid (); + return !sockets.empty () && associated && + thread_t::equal (tid, thread_t::id ()); } bool zmq::app_thread_t::make_current () @@ -73,7 +74,8 @@ bool zmq::app_thread_t::make_current () if (!sockets.empty ()) return false; - tid = getpid (); + associated = true; + tid = thread_t::id (); return true; } diff --git a/src/app_thread.hpp b/src/app_thread.hpp index e45b1b2..0f95de9 100644 --- a/src/app_thread.hpp +++ b/src/app_thread.hpp @@ -25,6 +25,7 @@ #include "stdint.hpp" #include "object.hpp" #include "ypollset.hpp" +#include "thread.hpp" namespace zmq { @@ -69,10 +70,12 @@ namespace zmq typedef std::vector sockets_t; sockets_t sockets; + // If false, app_thread_t object is not associated with any OS thread. + // In such case, 'tid' member contains a bogus value. + bool associated; + // Thread ID associated with this slot. - // TODO: Virtualise pid_t! - // TODO: Check whether getpid returns unique ID for each thread. - int tid; + thread_t::id_t tid; // App thread's signaler object. ypollset_t pollset; diff --git a/src/thread.cpp b/src/thread.cpp index 77993e2..d5b889d 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -38,6 +38,16 @@ void zmq::thread_t::stop () win_assert (rc != WAIT_FAILED); } +zmq::thread_t::id_t zmq::thread_t::id () +{ + return GetCurrentThreadId (); +} + +bool zmq::thread_t::equal (id_t id1_, id_t id2_) +{ + return id1_ == id2_; +} + unsigned int __stdcall zmq::thread_t::thread_routine (void *arg_) { thread_t *self = (thread_t*) arg_; @@ -63,6 +73,16 @@ void zmq::thread_t::stop () errno_assert (rc == 0); } +zmq::thread_t::id_t zmq::thread_t::id () +{ + return pthread_self (); +} + +bool zmq::thread_t::equal (id_t id1_, id_t id2_) +{ + return pthread_equal (id1_, id2_) != 0; +} + void *zmq::thread_t::thread_routine (void *arg_) { #if !defined ZMQ_HAVE_OPENVMS diff --git a/src/thread.hpp b/src/thread.hpp index 01f1f78..a1117b2 100644 --- a/src/thread.hpp +++ b/src/thread.hpp @@ -55,6 +55,15 @@ namespace zmq // Waits for thread termination. void stop (); +#ifdef ZMQ_HAVE_WINDOWS + typedef DWORD id_t; +#else + typedef pthread_t id_t; +#endif + + static id_t id (); + static bool equal (id_t id1_, id_t id2_); + private: #ifdef ZMQ_HAVE_WINDOWS -- cgit v1.2.3