Thread.cc revision 50da4f87
1cd139dc7SShuo Chen// excerpts from http://code.google.com/p/muduo/
2cd139dc7SShuo Chen//
3cd139dc7SShuo Chen// Use of this source code is governed by a BSD-style license
4cd139dc7SShuo Chen// that can be found in the License file.
5cd139dc7SShuo Chen//
6cd139dc7SShuo Chen// Author: Shuo Chen (giantchen at gmail dot com)
7cd139dc7SShuo Chen
8cd139dc7SShuo Chen#include "Thread.h"
9cd139dc7SShuo Chen
10cd139dc7SShuo Chen#include <unistd.h>
11cd139dc7SShuo Chen#include <sys/syscall.h>
12cd139dc7SShuo Chen#include <sys/types.h>
13cd139dc7SShuo Chen#include <linux/unistd.h>
14cd139dc7SShuo Chen
15cd139dc7SShuo Chennamespace muduo
16cd139dc7SShuo Chen{
17cd139dc7SShuo Chennamespace CurrentThread
18cd139dc7SShuo Chen{
19cd139dc7SShuo Chen  __thread const char* t_threadName = "unknown";
20cd139dc7SShuo Chen}
21cd139dc7SShuo Chen}
22cd139dc7SShuo Chen
23cd139dc7SShuo Chennamespace
24cd139dc7SShuo Chen{
25cd139dc7SShuo Chen__thread pid_t t_cachedTid = 0;
26cd139dc7SShuo Chen
27cd139dc7SShuo Chenpid_t gettid()
28cd139dc7SShuo Chen{
29cd139dc7SShuo Chen  return static_cast<pid_t>(::syscall(SYS_gettid));
30cd139dc7SShuo Chen}
31cd139dc7SShuo Chen
3250da4f87SShuo Chenvoid afterFork()
3350da4f87SShuo Chen{
3450da4f87SShuo Chen  t_cachedTid = gettid();
3550da4f87SShuo Chen  muduo::CurrentThread::t_threadName = "main";
3650da4f87SShuo Chen  // no need to call pthread_atfork(NULL, NULL, &afterFork);
3750da4f87SShuo Chen}
3850da4f87SShuo Chen
39cd139dc7SShuo Chenclass ThreadNameInitializer
40cd139dc7SShuo Chen{
41cd139dc7SShuo Chen public:
42cd139dc7SShuo Chen  ThreadNameInitializer()
43cd139dc7SShuo Chen  {
44cd139dc7SShuo Chen    muduo::CurrentThread::t_threadName = "main";
4550da4f87SShuo Chen    pthread_atfork(NULL, NULL, &afterFork);
46cd139dc7SShuo Chen  }
47cd139dc7SShuo Chen};
48cd139dc7SShuo Chen
49cd139dc7SShuo ChenThreadNameInitializer init;
50cd139dc7SShuo Chen}
51cd139dc7SShuo Chen
52cd139dc7SShuo Chenusing namespace muduo;
53cd139dc7SShuo Chen
54cd139dc7SShuo Chenpid_t CurrentThread::tid()
55cd139dc7SShuo Chen{
56cd139dc7SShuo Chen  if (t_cachedTid == 0)
57cd139dc7SShuo Chen  {
58cd139dc7SShuo Chen    t_cachedTid = gettid();
59cd139dc7SShuo Chen  }
60cd139dc7SShuo Chen  return t_cachedTid;
61cd139dc7SShuo Chen}
62cd139dc7SShuo Chen
63cd139dc7SShuo Chenconst char* CurrentThread::name()
64cd139dc7SShuo Chen{
65cd139dc7SShuo Chen  return t_threadName;
66cd139dc7SShuo Chen}
67cd139dc7SShuo Chen
68cd139dc7SShuo Chenbool CurrentThread::isMainThread()
69cd139dc7SShuo Chen{
70cd139dc7SShuo Chen  return tid() == ::getpid();
71cd139dc7SShuo Chen}
72cd139dc7SShuo Chen
73cd139dc7SShuo ChenAtomicInt32 Thread::numCreated_;
74cd139dc7SShuo Chen
75cd139dc7SShuo ChenThread::Thread(const ThreadFunc& func, const std::string& n)
76cd139dc7SShuo Chen  : started_(false),
77cd139dc7SShuo Chen    pthreadId_(0),
78cd139dc7SShuo Chen    tid_(0),
79cd139dc7SShuo Chen    func_(func),
80cd139dc7SShuo Chen    name_(n)
81cd139dc7SShuo Chen{
82cd139dc7SShuo Chen  numCreated_.increment();
83cd139dc7SShuo Chen}
84cd139dc7SShuo Chen
85cd139dc7SShuo ChenThread::~Thread()
86cd139dc7SShuo Chen{
87cd139dc7SShuo Chen}
88cd139dc7SShuo Chen
89cd139dc7SShuo Chenvoid Thread::start()
90cd139dc7SShuo Chen{
91cd139dc7SShuo Chen  assert(!started_);
92cd139dc7SShuo Chen  started_ = true;
93cd139dc7SShuo Chen  pthread_create(&pthreadId_, NULL, &startThread, this);
94cd139dc7SShuo Chen}
95cd139dc7SShuo Chen
96cd139dc7SShuo Chenvoid Thread::join()
97cd139dc7SShuo Chen{
98cd139dc7SShuo Chen  assert(started_);
99cd139dc7SShuo Chen  pthread_join(pthreadId_, NULL);
100cd139dc7SShuo Chen}
101cd139dc7SShuo Chen
102cd139dc7SShuo Chenvoid* Thread::startThread(void* obj)
103cd139dc7SShuo Chen{
104cd139dc7SShuo Chen  Thread* thread = static_cast<Thread*>(obj);
105cd139dc7SShuo Chen  thread->runInThread();
106cd139dc7SShuo Chen  return NULL;
107cd139dc7SShuo Chen}
108cd139dc7SShuo Chen
109cd139dc7SShuo Chenvoid Thread::runInThread()
110cd139dc7SShuo Chen{
111cd139dc7SShuo Chen  tid_ = CurrentThread::tid();
112cd139dc7SShuo Chen  muduo::CurrentThread::t_threadName = name_.c_str();
113cd139dc7SShuo Chen  func_();
114cd139dc7SShuo Chen  muduo::CurrentThread::t_threadName = "finished";
115cd139dc7SShuo Chen}
116cd139dc7SShuo Chen
117