Thread.cc revision 926c960e
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
102b2bf878SShuo Chen#include <boost/weak_ptr.hpp>
112b2bf878SShuo Chen
12cd139dc7SShuo Chen#include <unistd.h>
13926c960eSShuo Chen#include <sys/prctl.h>
14cd139dc7SShuo Chen#include <sys/syscall.h>
15cd139dc7SShuo Chen#include <sys/types.h>
16cd139dc7SShuo Chen#include <linux/unistd.h>
17cd139dc7SShuo Chen
18cd139dc7SShuo Chennamespace muduo
19cd139dc7SShuo Chen{
20cd139dc7SShuo Chennamespace CurrentThread
21cd139dc7SShuo Chen{
22cd139dc7SShuo Chen  __thread const char* t_threadName = "unknown";
23cd139dc7SShuo Chen}
24cd139dc7SShuo Chen}
25cd139dc7SShuo Chen
26cd139dc7SShuo Chennamespace
27cd139dc7SShuo Chen{
28cd139dc7SShuo Chen__thread pid_t t_cachedTid = 0;
29cd139dc7SShuo Chen
30cd139dc7SShuo Chenpid_t gettid()
31cd139dc7SShuo Chen{
32cd139dc7SShuo Chen  return static_cast<pid_t>(::syscall(SYS_gettid));
33cd139dc7SShuo Chen}
34cd139dc7SShuo Chen
3550da4f87SShuo Chenvoid afterFork()
3650da4f87SShuo Chen{
3750da4f87SShuo Chen  t_cachedTid = gettid();
3850da4f87SShuo Chen  muduo::CurrentThread::t_threadName = "main";
3950da4f87SShuo Chen  // no need to call pthread_atfork(NULL, NULL, &afterFork);
4050da4f87SShuo Chen}
4150da4f87SShuo Chen
42cd139dc7SShuo Chenclass ThreadNameInitializer
43cd139dc7SShuo Chen{
44cd139dc7SShuo Chen public:
45cd139dc7SShuo Chen  ThreadNameInitializer()
46cd139dc7SShuo Chen  {
47cd139dc7SShuo Chen    muduo::CurrentThread::t_threadName = "main";
4850da4f87SShuo Chen    pthread_atfork(NULL, NULL, &afterFork);
49cd139dc7SShuo Chen  }
50cd139dc7SShuo Chen};
51cd139dc7SShuo Chen
52cd139dc7SShuo ChenThreadNameInitializer init;
532b2bf878SShuo Chen
542b2bf878SShuo Chenstruct ThreadData
552b2bf878SShuo Chen{
562b2bf878SShuo Chen  typedef muduo::Thread::ThreadFunc ThreadFunc;
572b2bf878SShuo Chen  ThreadFunc func_;
582b2bf878SShuo Chen  std::string name_;
592b2bf878SShuo Chen  boost::weak_ptr<pid_t> wkTid_;
602b2bf878SShuo Chen
612b2bf878SShuo Chen  ThreadData(const ThreadFunc& func,
622b2bf878SShuo Chen             const std::string& name,
632b2bf878SShuo Chen             const boost::shared_ptr<pid_t>& tid)
642b2bf878SShuo Chen    : func_(func),
652b2bf878SShuo Chen      name_(name),
662b2bf878SShuo Chen      wkTid_(tid)
672b2bf878SShuo Chen  { }
682b2bf878SShuo Chen
692b2bf878SShuo Chen  void runInThread()
702b2bf878SShuo Chen  {
71ac2f0106SShuo Chen    pid_t tid = muduo::CurrentThread::tid();
72ac2f0106SShuo Chen    boost::shared_ptr<pid_t> ptid = wkTid_.lock();
73ac2f0106SShuo Chen
74ac2f0106SShuo Chen    if (ptid)
752b2bf878SShuo Chen    {
76ac2f0106SShuo Chen      *ptid = tid;
77ac2f0106SShuo Chen      ptid.reset();
782b2bf878SShuo Chen    }
792b2bf878SShuo Chen
80926c960eSShuo Chen    muduo::CurrentThread::t_threadName = name_.empty() ? "muduoThread" : name_.c_str();
81926c960eSShuo Chen    ::prctl(PR_SET_NAME, muduo::CurrentThread::t_threadName);
822b2bf878SShuo Chen    func_(); // FIXME: surround with try-catch, see muduo
832b2bf878SShuo Chen    muduo::CurrentThread::t_threadName = "finished";
842b2bf878SShuo Chen  }
852b2bf878SShuo Chen};
862b2bf878SShuo Chen
872b2bf878SShuo Chenvoid* startThread(void* obj)
882b2bf878SShuo Chen{
892b2bf878SShuo Chen  ThreadData* data = static_cast<ThreadData*>(obj);
902b2bf878SShuo Chen  data->runInThread();
912b2bf878SShuo Chen  delete data;
922b2bf878SShuo Chen  return NULL;
932b2bf878SShuo Chen}
942b2bf878SShuo Chen
95cd139dc7SShuo Chen}
96cd139dc7SShuo Chen
97cd139dc7SShuo Chenusing namespace muduo;
98cd139dc7SShuo Chen
99cd139dc7SShuo Chenpid_t CurrentThread::tid()
100cd139dc7SShuo Chen{
101cd139dc7SShuo Chen  if (t_cachedTid == 0)
102cd139dc7SShuo Chen  {
103cd139dc7SShuo Chen    t_cachedTid = gettid();
104cd139dc7SShuo Chen  }
105cd139dc7SShuo Chen  return t_cachedTid;
106cd139dc7SShuo Chen}
107cd139dc7SShuo Chen
108cd139dc7SShuo Chenconst char* CurrentThread::name()
109cd139dc7SShuo Chen{
110cd139dc7SShuo Chen  return t_threadName;
111cd139dc7SShuo Chen}
112cd139dc7SShuo Chen
113cd139dc7SShuo Chenbool CurrentThread::isMainThread()
114cd139dc7SShuo Chen{
115cd139dc7SShuo Chen  return tid() == ::getpid();
116cd139dc7SShuo Chen}
117cd139dc7SShuo Chen
118cd139dc7SShuo ChenAtomicInt32 Thread::numCreated_;
119cd139dc7SShuo Chen
120cd139dc7SShuo ChenThread::Thread(const ThreadFunc& func, const std::string& n)
121cd139dc7SShuo Chen  : started_(false),
1222b2bf878SShuo Chen    joined_(false),
123cd139dc7SShuo Chen    pthreadId_(0),
1242b2bf878SShuo Chen    tid_(new pid_t(0)),
125cd139dc7SShuo Chen    func_(func),
126cd139dc7SShuo Chen    name_(n)
127cd139dc7SShuo Chen{
128cd139dc7SShuo Chen  numCreated_.increment();
129cd139dc7SShuo Chen}
130cd139dc7SShuo Chen
131cd139dc7SShuo ChenThread::~Thread()
132cd139dc7SShuo Chen{
1332b2bf878SShuo Chen  if (started_ && !joined_)
1342b2bf878SShuo Chen  {
1352b2bf878SShuo Chen    pthread_detach(pthreadId_);
1362b2bf878SShuo Chen  }
137cd139dc7SShuo Chen}
138cd139dc7SShuo Chen
139cd139dc7SShuo Chenvoid Thread::start()
140cd139dc7SShuo Chen{
141cd139dc7SShuo Chen  assert(!started_);
142cd139dc7SShuo Chen  started_ = true;
1432b2bf878SShuo Chen
1442b2bf878SShuo Chen  ThreadData* data = new ThreadData(func_, name_, tid_);
1452b2bf878SShuo Chen  if (pthread_create(&pthreadId_, NULL, &startThread, data))
1462b2bf878SShuo Chen  {
1472b2bf878SShuo Chen    started_ = false;
1482b2bf878SShuo Chen    delete data;
1492b2bf878SShuo Chen    abort();
1502b2bf878SShuo Chen  }
151cd139dc7SShuo Chen}
152cd139dc7SShuo Chen
153cd139dc7SShuo Chenvoid Thread::join()
154cd139dc7SShuo Chen{
155cd139dc7SShuo Chen  assert(started_);
1562b2bf878SShuo Chen  assert(!joined_);
1572b2bf878SShuo Chen  joined_ = true;
158cd139dc7SShuo Chen  pthread_join(pthreadId_, NULL);
159cd139dc7SShuo Chen}
160