Thread.cc revision ac2f0106
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> 13cd139dc7SShuo Chen#include <sys/syscall.h> 14cd139dc7SShuo Chen#include <sys/types.h> 15cd139dc7SShuo Chen#include <linux/unistd.h> 16cd139dc7SShuo Chen 17cd139dc7SShuo Chennamespace muduo 18cd139dc7SShuo Chen{ 19cd139dc7SShuo Chennamespace CurrentThread 20cd139dc7SShuo Chen{ 21cd139dc7SShuo Chen __thread const char* t_threadName = "unknown"; 22cd139dc7SShuo Chen} 23cd139dc7SShuo Chen} 24cd139dc7SShuo Chen 25cd139dc7SShuo Chennamespace 26cd139dc7SShuo Chen{ 27cd139dc7SShuo Chen__thread pid_t t_cachedTid = 0; 28cd139dc7SShuo Chen 29cd139dc7SShuo Chenpid_t gettid() 30cd139dc7SShuo Chen{ 31cd139dc7SShuo Chen return static_cast<pid_t>(::syscall(SYS_gettid)); 32cd139dc7SShuo Chen} 33cd139dc7SShuo Chen 3450da4f87SShuo Chenvoid afterFork() 3550da4f87SShuo Chen{ 3650da4f87SShuo Chen t_cachedTid = gettid(); 3750da4f87SShuo Chen muduo::CurrentThread::t_threadName = "main"; 3850da4f87SShuo Chen // no need to call pthread_atfork(NULL, NULL, &afterFork); 3950da4f87SShuo Chen} 4050da4f87SShuo Chen 41cd139dc7SShuo Chenclass ThreadNameInitializer 42cd139dc7SShuo Chen{ 43cd139dc7SShuo Chen public: 44cd139dc7SShuo Chen ThreadNameInitializer() 45cd139dc7SShuo Chen { 46cd139dc7SShuo Chen muduo::CurrentThread::t_threadName = "main"; 4750da4f87SShuo Chen pthread_atfork(NULL, NULL, &afterFork); 48cd139dc7SShuo Chen } 49cd139dc7SShuo Chen}; 50cd139dc7SShuo Chen 51cd139dc7SShuo ChenThreadNameInitializer init; 522b2bf878SShuo Chen 532b2bf878SShuo Chenstruct ThreadData 542b2bf878SShuo Chen{ 552b2bf878SShuo Chen typedef muduo::Thread::ThreadFunc ThreadFunc; 562b2bf878SShuo Chen ThreadFunc func_; 572b2bf878SShuo Chen std::string name_; 582b2bf878SShuo Chen boost::weak_ptr<pid_t> wkTid_; 592b2bf878SShuo Chen 602b2bf878SShuo Chen ThreadData(const ThreadFunc& func, 612b2bf878SShuo Chen const std::string& name, 622b2bf878SShuo Chen const boost::shared_ptr<pid_t>& tid) 632b2bf878SShuo Chen : func_(func), 642b2bf878SShuo Chen name_(name), 652b2bf878SShuo Chen wkTid_(tid) 662b2bf878SShuo Chen { } 672b2bf878SShuo Chen 682b2bf878SShuo Chen void runInThread() 692b2bf878SShuo Chen { 70ac2f0106SShuo Chen pid_t tid = muduo::CurrentThread::tid(); 71ac2f0106SShuo Chen boost::shared_ptr<pid_t> ptid = wkTid_.lock(); 72ac2f0106SShuo Chen 73ac2f0106SShuo Chen if (ptid) 742b2bf878SShuo Chen { 75ac2f0106SShuo Chen *ptid = tid; 76ac2f0106SShuo Chen ptid.reset(); 772b2bf878SShuo Chen } 782b2bf878SShuo Chen 792b2bf878SShuo Chen muduo::CurrentThread::t_threadName = name_.c_str(); 802b2bf878SShuo Chen func_(); // FIXME: surround with try-catch, see muduo 812b2bf878SShuo Chen muduo::CurrentThread::t_threadName = "finished"; 822b2bf878SShuo Chen } 832b2bf878SShuo Chen}; 842b2bf878SShuo Chen 852b2bf878SShuo Chenvoid* startThread(void* obj) 862b2bf878SShuo Chen{ 872b2bf878SShuo Chen ThreadData* data = static_cast<ThreadData*>(obj); 882b2bf878SShuo Chen data->runInThread(); 892b2bf878SShuo Chen delete data; 902b2bf878SShuo Chen return NULL; 912b2bf878SShuo Chen} 922b2bf878SShuo Chen 93cd139dc7SShuo Chen} 94cd139dc7SShuo Chen 95cd139dc7SShuo Chenusing namespace muduo; 96cd139dc7SShuo Chen 97cd139dc7SShuo Chenpid_t CurrentThread::tid() 98cd139dc7SShuo Chen{ 99cd139dc7SShuo Chen if (t_cachedTid == 0) 100cd139dc7SShuo Chen { 101cd139dc7SShuo Chen t_cachedTid = gettid(); 102cd139dc7SShuo Chen } 103cd139dc7SShuo Chen return t_cachedTid; 104cd139dc7SShuo Chen} 105cd139dc7SShuo Chen 106cd139dc7SShuo Chenconst char* CurrentThread::name() 107cd139dc7SShuo Chen{ 108cd139dc7SShuo Chen return t_threadName; 109cd139dc7SShuo Chen} 110cd139dc7SShuo Chen 111cd139dc7SShuo Chenbool CurrentThread::isMainThread() 112cd139dc7SShuo Chen{ 113cd139dc7SShuo Chen return tid() == ::getpid(); 114cd139dc7SShuo Chen} 115cd139dc7SShuo Chen 116cd139dc7SShuo ChenAtomicInt32 Thread::numCreated_; 117cd139dc7SShuo Chen 118cd139dc7SShuo ChenThread::Thread(const ThreadFunc& func, const std::string& n) 119cd139dc7SShuo Chen : started_(false), 1202b2bf878SShuo Chen joined_(false), 121cd139dc7SShuo Chen pthreadId_(0), 1222b2bf878SShuo Chen tid_(new pid_t(0)), 123cd139dc7SShuo Chen func_(func), 124cd139dc7SShuo Chen name_(n) 125cd139dc7SShuo Chen{ 126cd139dc7SShuo Chen numCreated_.increment(); 127cd139dc7SShuo Chen} 128cd139dc7SShuo Chen 129cd139dc7SShuo ChenThread::~Thread() 130cd139dc7SShuo Chen{ 1312b2bf878SShuo Chen if (started_ && !joined_) 1322b2bf878SShuo Chen { 1332b2bf878SShuo Chen pthread_detach(pthreadId_); 1342b2bf878SShuo Chen } 135cd139dc7SShuo Chen} 136cd139dc7SShuo Chen 137cd139dc7SShuo Chenvoid Thread::start() 138cd139dc7SShuo Chen{ 139cd139dc7SShuo Chen assert(!started_); 140cd139dc7SShuo Chen started_ = true; 1412b2bf878SShuo Chen 1422b2bf878SShuo Chen ThreadData* data = new ThreadData(func_, name_, tid_); 1432b2bf878SShuo Chen if (pthread_create(&pthreadId_, NULL, &startThread, data)) 1442b2bf878SShuo Chen { 1452b2bf878SShuo Chen started_ = false; 1462b2bf878SShuo Chen delete data; 1472b2bf878SShuo Chen abort(); 1482b2bf878SShuo Chen } 149cd139dc7SShuo Chen} 150cd139dc7SShuo Chen 151cd139dc7SShuo Chenvoid Thread::join() 152cd139dc7SShuo Chen{ 153cd139dc7SShuo Chen assert(started_); 1542b2bf878SShuo Chen assert(!joined_); 1552b2bf878SShuo Chen joined_ = true; 156cd139dc7SShuo Chen pthread_join(pthreadId_, NULL); 157cd139dc7SShuo Chen} 158