From b54d72778c00b27293a8718a7813515308af365a Mon Sep 17 00:00:00 2001 From: "pwestin@webrtc.org" Date: Wed, 11 Jan 2012 08:28:04 +0000 Subject: [PATCH] Changed thread Id handling in trace. Review URL: http://webrtc-codereview.appspot.com/331020 git-svn-id: http://webrtc.googlecode.com/svn/trunk@1383 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../interface/thread_wrapper.h | 17 ++- src/system_wrappers/source/thread_posix.cc | 104 ++++++++---------- src/system_wrappers/source/thread_posix.h | 5 +- src/system_wrappers/source/thread_win.cc | 4 + src/system_wrappers/source/trace_impl.cc | 8 +- src/system_wrappers/source/trace_impl.h | 3 +- src/system_wrappers/source/trace_posix.cc | 16 --- src/system_wrappers/source/trace_posix.h | 1 - src/system_wrappers/source/trace_win.cc | 8 -- src/system_wrappers/source/trace_win.h | 3 +- 10 files changed, 75 insertions(+), 94 deletions(-) diff --git a/src/system_wrappers/interface/thread_wrapper.h b/src/system_wrappers/interface/thread_wrapper.h index 72a06e8bd..030ac8a6f 100644 --- a/src/system_wrappers/interface/thread_wrapper.h +++ b/src/system_wrappers/interface/thread_wrapper.h @@ -16,6 +16,9 @@ #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ +#include "common_types.h" +#include "typedefs.h" + namespace webrtc { // Object that will be passed by the spawned thread when it enters the callback // function. @@ -51,9 +54,12 @@ public: // threadName NULL terminated thread name, will be visable in the Windows // debugger. static ThreadWrapper* CreateThread(ThreadRunFunction func = 0, - ThreadObj obj= 0, - ThreadPriority prio = kNormalPriority, - const char* threadName = 0); + ThreadObj obj= 0, + ThreadPriority prio = kNormalPriority, + const char* threadName = 0); + + // Get the current thread's kernel thread ID. + static uint32_t GetThreadId(); // Non blocking termination of the spawned thread. Note that it is not safe // to delete this class until the spawned thread has been reclaimed. @@ -69,8 +75,9 @@ public: // should be lower than (number of CPUs - 1). amountOfProcessors should be // equal to the number of processors listed in processorNumbers virtual bool SetAffinity(const int* /*processorNumbers*/, - const unsigned int /*amountOfProcessors*/) - {return false;} + const unsigned int /*amountOfProcessors*/) { + return false; + } // Stops the spawned thread and waits for it to be reclaimed with a timeout // of two seconds. Will return false if the thread was not reclaimed. diff --git a/src/system_wrappers/source/thread_posix.cc b/src/system_wrappers/source/thread_posix.cc index eb0e8f46a..333c3e791 100644 --- a/src/system_wrappers/source/thread_posix.cc +++ b/src/system_wrappers/source/thread_posix.cc @@ -22,6 +22,10 @@ #include #endif +#if defined(WEBRTC_MAC) +#include +#endif + #include "event_wrapper.h" #include "trace.h" @@ -35,17 +39,6 @@ extern "C" } } -#if (defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)) -static pid_t gettid() -{ -#if defined(__NR_gettid) - return syscall(__NR_gettid); -#else - return -1; -#endif -} -#endif - ThreadWrapper* ThreadPosix::Create(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio, const char* threadName) { @@ -71,11 +64,9 @@ ThreadPosix::ThreadPosix(ThreadRunFunction func, ThreadObj obj, _dead(true), _prio(prio), _event(EventWrapper::Create()), - _setThreadName(false) + _setThreadName(false), + _pid(-1) { -#ifdef WEBRTC_LINUX - _linuxPid = -1; -#endif if (threadName != NULL) { _setThreadName = true; @@ -83,6 +74,16 @@ ThreadPosix::ThreadPosix(ThreadRunFunction func, ThreadObj obj, } } +uint32_t ThreadWrapper::GetThreadId() { +#if defined(WEBRTC_ANDROID) || defined(WEBRTC_LINUX) + return static_cast(syscall(__NR_gettid)); +#elif defined(WEBRTC_MAC) + return static_cast(mach_thread_self()); +#else + return reinterpret_cast(pthread_self()); +#endif +} + int ThreadPosix::Construct() { int result = 0; @@ -104,7 +105,6 @@ int ThreadPosix::Construct() { return -1; } - return 0; } @@ -191,33 +191,38 @@ bool ThreadPosix::Start(unsigned int& /*threadID*/) return true; } -#if (defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)) +#if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)) bool ThreadPosix::SetAffinity(const int* processorNumbers, - const unsigned int amountOfProcessors) -{ - if (!processorNumbers || (amountOfProcessors == 0)) - { - return false; - } + const unsigned int amountOfProcessors) { + if (!processorNumbers || (amountOfProcessors == 0)) { + return false; + } + cpu_set_t mask; + CPU_ZERO(&mask); - cpu_set_t mask; - CPU_ZERO(&mask); - - for(unsigned int processor = 0; - processor < amountOfProcessors; - processor++) - { - CPU_SET(processorNumbers[processor], &mask); - } - const int result = sched_setaffinity(_linuxPid, (unsigned int)sizeof(mask), - &mask); - if (result != 0) - { - return false; - - } - return true; + for (unsigned int processor = 0; + processor < amountOfProcessors; + processor++) { + CPU_SET(processorNumbers[processor], &mask); + } +#if defined(WEBRTC_ANDROID) + // Android. + const int result = syscall(__NR_sched_setaffinity, + _pid, + sizeof(mask), + &mask); +#else + // "Normal" Linux. + const int result = sched_setaffinity(_pid, + sizeof(mask), + &mask); +#endif + if (result != 0) { + return false; + } + return true; } + #else // NOTE: On Mac OS X, use the Thread affinity API in // /usr/include/mach/thread_policy.h: thread_policy_set and mach_thread_self() @@ -274,34 +279,21 @@ void ThreadPosix::Run() { _alive = true; _dead = false; -#ifdef WEBRTC_LINUX - if(_linuxPid == -1) - { - _linuxPid = gettid(); - } -#endif + _pid = GetThreadId(); // The event the Start() is waiting for. _event->Set(); if (_setThreadName) { #ifdef WEBRTC_LINUX - WEBRTC_TRACE(kTraceStateInfo, kTraceUtility,-1, - "Thread with id:%d name:%s started ", _linuxPid, _name); prctl(PR_SET_NAME, (unsigned long)_name, 0, 0, 0); -#else +#endif WEBRTC_TRACE(kTraceStateInfo, kTraceUtility,-1, "Thread with name:%s started ", _name); -#endif - }else + } else { -#ifdef WEBRTC_LINUX - WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, - "Thread with id:%d without name started", _linuxPid); -#else WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Thread without name started"); -#endif } do { diff --git a/src/system_wrappers/source/thread_posix.h b/src/system_wrappers/source/thread_posix.h index f664a52e7..85613de47 100644 --- a/src/system_wrappers/source/thread_posix.h +++ b/src/system_wrappers/source/thread_posix.h @@ -59,10 +59,7 @@ private: // handle to thread pthread_attr_t _attr; pthread_t _thread; -#ifdef WEBRTC_LINUX - pid_t _linuxPid; -#endif - + pid_t _pid; }; } // namespace webrtc diff --git a/src/system_wrappers/source/thread_win.cc b/src/system_wrappers/source/thread_win.cc index 705ca261b..4599bedc2 100644 --- a/src/system_wrappers/source/thread_win.cc +++ b/src/system_wrappers/source/thread_win.cc @@ -68,6 +68,10 @@ ThreadWindows::~ThreadWindows() } } +uint32_t ThreadWrapper::GetThreadId() { + return GetCurrentThreadId(); +} + unsigned int WINAPI ThreadWindows::StartThread(LPVOID lpParameter) { static_cast(lpParameter)->Run(); diff --git a/src/system_wrappers/source/trace_impl.cc b/src/system_wrappers/source/trace_impl.cc index 1156519eb..adf6fcfd2 100644 --- a/src/system_wrappers/source/trace_impl.cc +++ b/src/system_wrappers/source/trace_impl.cc @@ -147,6 +147,12 @@ TraceImpl::~TraceImpl() } } +WebRtc_Word32 TraceImpl::AddThreadId(char* traceMessage) const { + WebRtc_UWord32 threadId = ThreadWrapper::GetThreadId(); + // Messages is 12 characters. + return sprintf(traceMessage, "%10u; ", threadId); +} + WebRtc_Word32 TraceImpl::AddLevel(char* szMessage, const TraceLevel level) const { switch (level) @@ -637,7 +643,7 @@ void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module, ackLen += len; len = AddThreadId(meassagePtr); - if(len == -1) + if(len < 0) { return; } diff --git a/src/system_wrappers/source/trace_impl.h b/src/system_wrappers/source/trace_impl.h index 455a3d552..7e990481c 100644 --- a/src/system_wrappers/source/trace_impl.h +++ b/src/system_wrappers/source/trace_impl.h @@ -69,8 +69,9 @@ protected: static TraceImpl* StaticInstance(CountOperation count_operation, const TraceLevel level = kTraceAll); + WebRtc_Word32 AddThreadId(char* traceMessage) const; + // OS specific implementations - virtual WebRtc_Word32 AddThreadId(char* traceMessage) const = 0; virtual WebRtc_Word32 AddTime(char* traceMessage, const TraceLevel level) const = 0; diff --git a/src/system_wrappers/source/trace_posix.cc b/src/system_wrappers/source/trace_posix.cc index 198c4349b..877e87d70 100644 --- a/src/system_wrappers/source/trace_posix.cc +++ b/src/system_wrappers/source/trace_posix.cc @@ -15,9 +15,6 @@ #include #include #include -#ifdef __linux__ - #include -#endif #ifdef WEBRTC_ANDROID #include #else @@ -50,19 +47,6 @@ TracePosix::~TracePosix() StopThread(); } -WebRtc_Word32 TracePosix::AddThreadId(char* traceMessage) const { -#ifdef __linux__ - pid_t threadId = (pid_t) syscall(__NR_gettid); - sprintf(traceMessage, "%10d; ", threadId); -#else - WebRtc_UWord64 threadId = (WebRtc_UWord64)pthread_self(); - sprintf(traceMessage, "%10llu; ", - static_cast(threadId)); -#endif - // 12 bytes are written. - return 12; -} - WebRtc_Word32 TracePosix::AddTime(char* traceMessage, const TraceLevel level) const { diff --git a/src/system_wrappers/source/trace_posix.h b/src/system_wrappers/source/trace_posix.h index 099bcc874..8c37cd2b4 100644 --- a/src/system_wrappers/source/trace_posix.h +++ b/src/system_wrappers/source/trace_posix.h @@ -21,7 +21,6 @@ public: TracePosix(); virtual ~TracePosix(); - virtual WebRtc_Word32 AddThreadId(char *traceMessage) const; virtual WebRtc_Word32 AddTime(char* traceMessage, const TraceLevel level) const; diff --git a/src/system_wrappers/source/trace_win.cc b/src/system_wrappers/source/trace_win.cc index ea8e9ff5e..db3da7606 100644 --- a/src/system_wrappers/source/trace_win.cc +++ b/src/system_wrappers/source/trace_win.cc @@ -41,14 +41,6 @@ TraceWindows::~TraceWindows() StopThread(); } -WebRtc_Word32 TraceWindows::AddThreadId(char* traceMessage) const -{ - WebRtc_UWord32 threadId= GetCurrentThreadId(); - sprintf (traceMessage, "%10u; ", threadId); - // Messages is 12 characters. - return 12; -} - WebRtc_Word32 TraceWindows::AddTime(char* traceMessage, const TraceLevel level) const { diff --git a/src/system_wrappers/source/trace_win.h b/src/system_wrappers/source/trace_win.h index 2ead70183..803198e0f 100644 --- a/src/system_wrappers/source/trace_win.h +++ b/src/system_wrappers/source/trace_win.h @@ -20,9 +20,8 @@ class TraceWindows : public TraceImpl { public: TraceWindows(); - virtual ~TraceWindows(); + virtual ~TraceWindows(); - virtual WebRtc_Word32 AddThreadId(char *traceMessage) const; virtual WebRtc_Word32 AddTime(char* traceMessage, const TraceLevel level) const;