Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 1 | #include "PosixThreadFactory.h" |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <pthread.h> |
| 5 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 6 | #include <iostream> |
| 7 | |
| 8 | #include <boost/weak_ptr.hpp> |
| 9 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 10 | namespace facebook { namespace thrift { namespace concurrency { |
| 11 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 12 | using namespace boost; |
| 13 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 14 | /** |
| 15 | * The POSIX thread class. |
| 16 | * |
| 17 | * @author marc |
| 18 | * @version $Id:$ |
| 19 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | class PthreadThread: public Thread { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 21 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 23 | enum STATE { |
| 24 | uninitialized, |
| 25 | starting, |
| 26 | started, |
| 27 | stopping, |
| 28 | stopped |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static const int MB = 1024 * 1024; |
| 32 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 33 | static void* threadMain(void* arg); |
| 34 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 35 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 36 | pthread_t pthread_; |
| 37 | STATE state_; |
| 38 | int policy_; |
| 39 | int priority_; |
| 40 | int stackSize_; |
| 41 | weak_ptr<PthreadThread> self_; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 42 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 43 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 44 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 45 | PthreadThread(int policy, int priority, int stackSize, shared_ptr<Runnable> runnable) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 46 | pthread_(0), |
| 47 | state_(uninitialized), |
| 48 | policy_(policy), |
| 49 | priority_(priority), |
| 50 | stackSize_(stackSize) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 51 | |
| 52 | this->Thread::runnable(runnable); |
| 53 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 54 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 55 | ~PthreadThread() {} |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 56 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 57 | void start() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 58 | if (state_ != uninitialized) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 62 | state_ = starting; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 63 | |
| 64 | pthread_attr_t thread_attr; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 65 | assert(pthread_attr_init(&thread_attr) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 66 | assert(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE) == 0); |
| 67 | |
| 68 | // Set thread stack size |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 69 | assert(pthread_attr_setstacksize(&thread_attr, MB * stackSize_) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 70 | |
| 71 | // Set thread policy |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 72 | assert(pthread_attr_setschedpolicy(&thread_attr, policy_) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 73 | |
| 74 | struct sched_param sched_param; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 75 | sched_param.sched_priority = priority_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 76 | |
| 77 | // Set thread priority |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 78 | assert(pthread_attr_setschedparam(&thread_attr, &sched_param) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 79 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 80 | // Create reference |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 81 | shared_ptr<PthreadThread>* selfRef = new shared_ptr<PthreadThread>(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 82 | *selfRef = self_.lock(); |
| 83 | assert(pthread_create(&pthread_, &thread_attr, threadMain, (void*)selfRef) == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void join() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 87 | if (state_ != stopped) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 88 | void* ignore; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 89 | pthread_join(pthread_, &ignore); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 93 | shared_ptr<Runnable> runnable() const { return Thread::runnable(); } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 94 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 95 | void runnable(shared_ptr<Runnable> value) { Thread::runnable(value); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 96 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 97 | void weakRef(shared_ptr<PthreadThread> self) { |
| 98 | assert(self.get() == this); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 99 | self_ = weak_ptr<PthreadThread>(self); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 100 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 103 | void* PthreadThread::threadMain(void* arg) { |
| 104 | // XXX need a lock here when testing thread state |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 105 | shared_ptr<PthreadThread> thread = *(shared_ptr<PthreadThread>*)arg; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 106 | delete reinterpret_cast<shared_ptr<PthreadThread>*>(arg); |
| 107 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 108 | if (thread == NULL) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 109 | return (void*)0; |
| 110 | } |
| 111 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 112 | if (thread->state_ != starting) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 113 | return (void*)0; |
| 114 | } |
| 115 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 116 | thread->state_ = starting; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 117 | thread->runnable()->run(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 118 | if (thread->state_ != stopping && thread->state_ != stopped) { |
| 119 | thread->state_ = stopping; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return (void*)0; |
| 123 | } |
| 124 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 125 | /** |
| 126 | * POSIX Thread factory implementation |
| 127 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 128 | class PosixThreadFactory::Impl { |
| 129 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 130 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 131 | POLICY policy_; |
| 132 | PRIORITY priority_; |
| 133 | int stackSize_; |
| 134 | bool detached_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 135 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Converts generic posix thread schedule policy enums into pthread |
| 138 | * API values. |
| 139 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 140 | static int toPthreadPolicy(POLICY policy) { |
| 141 | switch(policy) { |
| 142 | case OTHER: return SCHED_OTHER; break; |
| 143 | case FIFO: return SCHED_FIFO; break; |
| 144 | case ROUND_ROBIN: return SCHED_RR; break; |
| 145 | default: return SCHED_OTHER; break; |
| 146 | } |
| 147 | } |
| 148 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 149 | /** |
| 150 | * Converts relative thread priorities to absolute value based on posix |
| 151 | * thread scheduler policy |
| 152 | * |
| 153 | * The idea is simply to divide up the priority range for the given policy |
| 154 | * into the correpsonding relative priority level (lowest..highest) and |
| 155 | * then pro-rate accordingly. |
| 156 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 157 | static int toPthreadPriority(POLICY policy, PRIORITY priority) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 158 | int pthread_policy = toPthreadPolicy(policy); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 159 | int min_priority = sched_get_priority_min(pthread_policy); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 160 | int max_priority = sched_get_priority_max(pthread_policy); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 161 | int quanta = (HIGHEST - LOWEST) + 1; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 162 | float stepsperquanta = (max_priority - min_priority) / quanta; |
| 163 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 164 | if (priority <= HIGHEST) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 165 | return (int)(min_priority + stepsperquanta * priority); |
| 166 | } else { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 167 | // should never get here for priority increments. |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 168 | assert(false); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 169 | return (int)(min_priority + stepsperquanta * NORMAL); |
| 170 | } |
| 171 | } |
| 172 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 173 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 174 | |
| 175 | Impl(POLICY policy, PRIORITY priority, int stackSize, bool detached) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 176 | policy_(policy), |
| 177 | priority_(priority), |
| 178 | stackSize_(stackSize), |
| 179 | detached_(detached) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 180 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 181 | /** |
| 182 | * Creates a new POSIX thread to run the runnable object |
| 183 | * |
| 184 | * @param runnable A runnable object |
| 185 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 186 | shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 187 | shared_ptr<PthreadThread> result = shared_ptr<PthreadThread>(new PthreadThread(toPthreadPolicy(policy_), toPthreadPriority(policy_, priority_), stackSize_, runnable)); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 188 | result->weakRef(result); |
| 189 | runnable->thread(result); |
| 190 | return result; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 193 | int stackSize() const { return stackSize_; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 194 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 195 | void stackSize(int value) { stackSize_ = value; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 196 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 197 | PRIORITY priority() const { return priority_; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 198 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 199 | /** |
| 200 | * Sets priority. |
| 201 | * |
| 202 | * XXX |
| 203 | * Need to handle incremental priorities properly. |
| 204 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 205 | void priority(PRIORITY value) { priority_ = value; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | PosixThreadFactory::PosixThreadFactory(POLICY policy, PRIORITY priority, int stackSize, bool detached) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 209 | impl_(new PosixThreadFactory::Impl(policy, priority, stackSize, detached)) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 210 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 211 | shared_ptr<Thread> PosixThreadFactory::newThread(shared_ptr<Runnable> runnable) const { return impl_->newThread(runnable); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 212 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 213 | int PosixThreadFactory::stackSize() const { return impl_->stackSize(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 214 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 215 | void PosixThreadFactory::stackSize(int value) { impl_->stackSize(value); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 216 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 217 | PosixThreadFactory::PRIORITY PosixThreadFactory::priority() const { return impl_->priority(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 218 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 219 | void PosixThreadFactory::priority(PosixThreadFactory::PRIORITY value) { impl_->priority(value); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 220 | |
| 221 | }}} // facebook::thrift::concurrency |