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