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
This commit is contained in:
pwestin@webrtc.org 2012-01-11 08:28:04 +00:00
parent e3eaf44ccf
commit b54d72778c
10 changed files with 75 additions and 94 deletions

View File

@ -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.
@ -55,6 +58,9 @@ public:
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.
virtual void SetNotAlive() = 0;
@ -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.

View File

@ -22,6 +22,10 @@
#include <sys/prctl.h>
#endif
#if defined(WEBRTC_MAC)
#include <mach/mach.h>
#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<uint32_t>(syscall(__NR_gettid));
#elif defined(WEBRTC_MAC)
return static_cast<uint32_t>(mach_thread_self());
#else
return reinterpret_cast<uint32_t>(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))
{
const unsigned int amountOfProcessors) {
if (!processorNumbers || (amountOfProcessors == 0)) {
return false;
}
cpu_set_t mask;
CPU_ZERO(&mask);
for (unsigned int processor = 0;
processor < amountOfProcessors;
processor++)
{
processor++) {
CPU_SET(processorNumbers[processor], &mask);
}
const int result = sched_setaffinity(_linuxPid, (unsigned int)sizeof(mask),
#if defined(WEBRTC_ANDROID)
// Android.
const int result = syscall(__NR_sched_setaffinity,
_pid,
sizeof(mask),
&mask);
if (result != 0)
{
#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
{
#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
{

View File

@ -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

View File

@ -68,6 +68,10 @@ ThreadWindows::~ThreadWindows()
}
}
uint32_t ThreadWrapper::GetThreadId() {
return GetCurrentThreadId();
}
unsigned int WINAPI ThreadWindows::StartThread(LPVOID lpParameter)
{
static_cast<ThreadWindows*>(lpParameter)->Run();

View File

@ -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;
}

View File

@ -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;

View File

@ -15,9 +15,6 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#ifdef __linux__
#include <sys/syscall.h>
#endif
#ifdef WEBRTC_ANDROID
#include <pthread.h>
#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<long long unsigned int>(threadId));
#endif
// 12 bytes are written.
return 12;
}
WebRtc_Word32 TracePosix::AddTime(char* traceMessage,
const TraceLevel level) const
{

View File

@ -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;

View File

@ -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
{

View File

@ -22,7 +22,6 @@ public:
TraceWindows();
virtual ~TraceWindows();
virtual WebRtc_Word32 AddThreadId(char *traceMessage) const;
virtual WebRtc_Word32 AddTime(char* traceMessage,
const TraceLevel level) const;