Thread.cc revision 2b2bf878
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 { 702b2bf878SShuo Chen boost::shared_ptr<pid_t> tid = wkTid_.lock(); 712b2bf878SShuo Chen if (tid) 722b2bf878SShuo Chen { 732b2bf878SShuo Chen *tid = muduo::CurrentThread::tid(); 742b2bf878SShuo Chen tid.reset(); 752b2bf878SShuo Chen } 762b2bf878SShuo Chen 772b2bf878SShuo Chen muduo::CurrentThread::t_threadName = name_.c_str(); 782b2bf878SShuo Chen func_(); // FIXME: surround with try-catch, see muduo 792b2bf878SShuo Chen muduo::CurrentThread::t_threadName = "finished"; 802b2bf878SShuo Chen } 812b2bf878SShuo Chen}; 822b2bf878SShuo Chen 832b2bf878SShuo Chenvoid* startThread(void* obj) 842b2bf878SShuo Chen{ 852b2bf878SShuo Chen ThreadData* data = static_cast<ThreadData*>(obj); 862b2bf878SShuo Chen data->runInThread(); 872b2bf878SShuo Chen delete data; 882b2bf878SShuo Chen return NULL; 892b2bf878SShuo Chen} 902b2bf878SShuo Chen 91cd139dc7SShuo Chen} 92cd139dc7SShuo Chen 93cd139dc7SShuo Chenusing namespace muduo; 94cd139dc7SShuo Chen 95cd139dc7SShuo Chenpid_t CurrentThread::tid() 96cd139dc7SShuo Chen{ 97cd139dc7SShuo Chen if (t_cachedTid == 0) 98cd139dc7SShuo Chen { 99cd139dc7SShuo Chen t_cachedTid = gettid(); 100cd139dc7SShuo Chen } 101cd139dc7SShuo Chen return t_cachedTid; 102cd139dc7SShuo Chen} 103cd139dc7SShuo Chen 104cd139dc7SShuo Chenconst char* CurrentThread::name() 105cd139dc7SShuo Chen{ 106cd139dc7SShuo Chen return t_threadName; 107cd139dc7SShuo Chen} 108cd139dc7SShuo Chen 109cd139dc7SShuo Chenbool CurrentThread::isMainThread() 110cd139dc7SShuo Chen{ 111cd139dc7SShuo Chen return tid() == ::getpid(); 112cd139dc7SShuo Chen} 113cd139dc7SShuo Chen 114cd139dc7SShuo ChenAtomicInt32 Thread::numCreated_; 115cd139dc7SShuo Chen 116cd139dc7SShuo ChenThread::Thread(const ThreadFunc& func, const std::string& n) 117cd139dc7SShuo Chen : started_(false), 1182b2bf878SShuo Chen joined_(false), 119cd139dc7SShuo Chen pthreadId_(0), 1202b2bf878SShuo Chen tid_(new pid_t(0)), 121cd139dc7SShuo Chen func_(func), 122cd139dc7SShuo Chen name_(n) 123cd139dc7SShuo Chen{ 124cd139dc7SShuo Chen numCreated_.increment(); 125cd139dc7SShuo Chen} 126cd139dc7SShuo Chen 127cd139dc7SShuo ChenThread::~Thread() 128cd139dc7SShuo Chen{ 1292b2bf878SShuo Chen if (started_ && !joined_) 1302b2bf878SShuo Chen { 1312b2bf878SShuo Chen pthread_detach(pthreadId_); 1322b2bf878SShuo Chen } 133cd139dc7SShuo Chen} 134cd139dc7SShuo Chen 135cd139dc7SShuo Chenvoid Thread::start() 136cd139dc7SShuo Chen{ 137cd139dc7SShuo Chen assert(!started_); 138cd139dc7SShuo Chen started_ = true; 1392b2bf878SShuo Chen 1402b2bf878SShuo Chen ThreadData* data = new ThreadData(func_, name_, tid_); 1412b2bf878SShuo Chen if (pthread_create(&pthreadId_, NULL, &startThread, data)) 1422b2bf878SShuo Chen { 1432b2bf878SShuo Chen started_ = false; 1442b2bf878SShuo Chen delete data; 1452b2bf878SShuo Chen abort(); 1462b2bf878SShuo Chen } 147cd139dc7SShuo Chen} 148cd139dc7SShuo Chen 149cd139dc7SShuo Chenvoid Thread::join() 150cd139dc7SShuo Chen{ 151cd139dc7SShuo Chen assert(started_); 1522b2bf878SShuo Chen assert(!joined_); 1532b2bf878SShuo Chen joined_ = true; 154cd139dc7SShuo Chen pthread_join(pthreadId_, NULL); 155cd139dc7SShuo Chen} 156