Thread.cc revision cd139dc7
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
32cd139dc7SShuo Chenclass ThreadNameInitializer
33cd139dc7SShuo Chen{
34cd139dc7SShuo Chen public:
35cd139dc7SShuo Chen  ThreadNameInitializer()
36cd139dc7SShuo Chen  {
37cd139dc7SShuo Chen    muduo::CurrentThread::t_threadName = "main";
38cd139dc7SShuo Chen  }
39cd139dc7SShuo Chen};
40cd139dc7SShuo Chen
41cd139dc7SShuo ChenThreadNameInitializer init;
42cd139dc7SShuo Chen}
43cd139dc7SShuo Chen
44cd139dc7SShuo Chenusing namespace muduo;
45cd139dc7SShuo Chen
46cd139dc7SShuo Chenpid_t CurrentThread::tid()
47cd139dc7SShuo Chen{
48cd139dc7SShuo Chen  if (t_cachedTid == 0)
49cd139dc7SShuo Chen  {
50cd139dc7SShuo Chen    t_cachedTid = gettid();
51cd139dc7SShuo Chen  }
52cd139dc7SShuo Chen  return t_cachedTid;
53cd139dc7SShuo Chen}
54cd139dc7SShuo Chen
55cd139dc7SShuo Chenconst char* CurrentThread::name()
56cd139dc7SShuo Chen{
57cd139dc7SShuo Chen  return t_threadName;
58cd139dc7SShuo Chen}
59cd139dc7SShuo Chen
60cd139dc7SShuo Chenbool CurrentThread::isMainThread()
61cd139dc7SShuo Chen{
62cd139dc7SShuo Chen  return tid() == ::getpid();
63cd139dc7SShuo Chen}
64cd139dc7SShuo Chen
65cd139dc7SShuo ChenAtomicInt32 Thread::numCreated_;
66cd139dc7SShuo Chen
67cd139dc7SShuo ChenThread::Thread(const ThreadFunc& func, const std::string& n)
68cd139dc7SShuo Chen  : started_(false),
69cd139dc7SShuo Chen    pthreadId_(0),
70cd139dc7SShuo Chen    tid_(0),
71cd139dc7SShuo Chen    func_(func),
72cd139dc7SShuo Chen    name_(n)
73cd139dc7SShuo Chen{
74cd139dc7SShuo Chen  numCreated_.increment();
75cd139dc7SShuo Chen}
76cd139dc7SShuo Chen
77cd139dc7SShuo ChenThread::~Thread()
78cd139dc7SShuo Chen{
79cd139dc7SShuo Chen}
80cd139dc7SShuo Chen
81cd139dc7SShuo Chenvoid Thread::start()
82cd139dc7SShuo Chen{
83cd139dc7SShuo Chen  assert(!started_);
84cd139dc7SShuo Chen  started_ = true;
85cd139dc7SShuo Chen  pthread_create(&pthreadId_, NULL, &startThread, this);
86cd139dc7SShuo Chen}
87cd139dc7SShuo Chen
88cd139dc7SShuo Chenvoid Thread::join()
89cd139dc7SShuo Chen{
90cd139dc7SShuo Chen  assert(started_);
91cd139dc7SShuo Chen  pthread_join(pthreadId_, NULL);
92cd139dc7SShuo Chen}
93cd139dc7SShuo Chen
94cd139dc7SShuo Chenvoid* Thread::startThread(void* obj)
95cd139dc7SShuo Chen{
96cd139dc7SShuo Chen  Thread* thread = static_cast<Thread*>(obj);
97cd139dc7SShuo Chen  thread->runInThread();
98cd139dc7SShuo Chen  return NULL;
99cd139dc7SShuo Chen}
100cd139dc7SShuo Chen
101cd139dc7SShuo Chenvoid Thread::runInThread()
102cd139dc7SShuo Chen{
103cd139dc7SShuo Chen  tid_ = CurrentThread::tid();
104cd139dc7SShuo Chen  muduo::CurrentThread::t_threadName = name_.c_str();
105cd139dc7SShuo Chen  func_();
106cd139dc7SShuo Chen  muduo::CurrentThread::t_threadName = "finished";
107cd139dc7SShuo Chen}
108cd139dc7SShuo Chen
109