Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 7 | #include "PosixThreadFactory.h" |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 8 | #include "Exception.h" |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 9 | |
David Reiss | af29695 | 2008-06-10 22:54:40 +0000 | [diff] [blame] | 10 | #if GOOGLE_PERFTOOLS_REGISTER_THREAD |
| 11 | # include <google/profiler.h> |
| 12 | #endif |
| 13 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | #include <pthread.h> |
| 16 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 17 | #include <iostream> |
| 18 | |
| 19 | #include <boost/weak_ptr.hpp> |
| 20 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 21 | namespace facebook { namespace thrift { namespace concurrency { |
| 22 | |
David Reiss | d4a269c | 2007-08-23 02:37:19 +0000 | [diff] [blame] | 23 | using boost::shared_ptr; |
| 24 | using boost::weak_ptr; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 25 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 26 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 27 | * The POSIX thread class. |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 28 | * |
| 29 | * @author marc |
| 30 | * @version $Id:$ |
| 31 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 32 | class PthreadThread: public Thread { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 33 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 34 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 35 | enum STATE { |
| 36 | uninitialized, |
| 37 | starting, |
| 38 | started, |
| 39 | stopping, |
| 40 | stopped |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static const int MB = 1024 * 1024; |
| 44 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 45 | static void* threadMain(void* arg); |
| 46 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 47 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 48 | pthread_t pthread_; |
| 49 | STATE state_; |
| 50 | int policy_; |
| 51 | int priority_; |
| 52 | int stackSize_; |
| 53 | weak_ptr<PthreadThread> self_; |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 54 | bool detached_; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 55 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 56 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 57 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 58 | PthreadThread(int policy, int priority, int stackSize, bool detached, shared_ptr<Runnable> runnable) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 59 | pthread_(0), |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 60 | state_(uninitialized), |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 61 | policy_(policy), |
| 62 | priority_(priority), |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 63 | stackSize_(stackSize), |
| 64 | detached_(detached) { |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 65 | |
| 66 | this->Thread::runnable(runnable); |
| 67 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 68 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 69 | ~PthreadThread() { |
| 70 | /* Nothing references this thread, if is is not detached, do a join |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 71 | now, otherwise the thread-id and, possibly, other resources will |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 72 | be leaked. */ |
| 73 | if(!detached_) { |
| 74 | try { |
| 75 | join(); |
| 76 | } catch(...) { |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 77 | // We're really hosed. |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 81 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 82 | void start() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 83 | if (state_ != uninitialized) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 87 | pthread_attr_t thread_attr; |
Mark Slee | 2782d6d | 2007-05-23 04:55:30 +0000 | [diff] [blame] | 88 | if (pthread_attr_init(&thread_attr) != 0) { |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 89 | throw SystemResourceException("pthread_attr_init failed"); |
| 90 | } |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 91 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 92 | if(pthread_attr_setdetachstate(&thread_attr, |
| 93 | detached_ ? |
| 94 | PTHREAD_CREATE_DETACHED : |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 95 | PTHREAD_CREATE_JOINABLE) != 0) { |
| 96 | throw SystemResourceException("pthread_attr_setdetachstate failed"); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 97 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 98 | |
| 99 | // Set thread stack size |
Mark Slee | 2782d6d | 2007-05-23 04:55:30 +0000 | [diff] [blame] | 100 | if (pthread_attr_setstacksize(&thread_attr, MB * stackSize_) != 0) { |
| 101 | throw SystemResourceException("pthread_attr_setstacksize failed"); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 102 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 103 | |
| 104 | // Set thread policy |
Mark Slee | 2782d6d | 2007-05-23 04:55:30 +0000 | [diff] [blame] | 105 | if (pthread_attr_setschedpolicy(&thread_attr, policy_) != 0) { |
| 106 | throw SystemResourceException("pthread_attr_setschedpolicy failed"); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 107 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 108 | |
| 109 | struct sched_param sched_param; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 110 | sched_param.sched_priority = priority_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 111 | |
| 112 | // Set thread priority |
Mark Slee | 2782d6d | 2007-05-23 04:55:30 +0000 | [diff] [blame] | 113 | if (pthread_attr_setschedparam(&thread_attr, &sched_param) != 0) { |
| 114 | throw SystemResourceException("pthread_attr_setschedparam failed"); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 115 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 116 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 117 | // Create reference |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 118 | shared_ptr<PthreadThread>* selfRef = new shared_ptr<PthreadThread>(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 119 | *selfRef = self_.lock(); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 120 | |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 121 | state_ = starting; |
| 122 | |
Mark Slee | 2782d6d | 2007-05-23 04:55:30 +0000 | [diff] [blame] | 123 | if (pthread_create(&pthread_, &thread_attr, threadMain, (void*)selfRef) != 0) { |
| 124 | throw SystemResourceException("pthread_create failed"); |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 125 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void join() { |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 129 | if (!detached_ && state_ != uninitialized) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 130 | void* ignore; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 131 | /* XXX |
| 132 | If join fails it is most likely due to the fact |
| 133 | that the last reference was the thread itself and cannot |
| 134 | join. This results in leaked threads and will eventually |
| 135 | cause the process to run out of thread resources. |
| 136 | We're beyond the point of throwing an exception. Not clear how |
| 137 | best to handle this. */ |
| 138 | detached_ = pthread_join(pthread_, &ignore) == 0; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 142 | id_t getId() { |
Mark Slee | 9843915 | 2007-08-21 02:39:40 +0000 | [diff] [blame] | 143 | // TODO(dreiss): Stop using C-style casts. |
| 144 | return (id_t)pthread_; |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 147 | shared_ptr<Runnable> runnable() const { return Thread::runnable(); } |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 148 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 149 | void runnable(shared_ptr<Runnable> value) { Thread::runnable(value); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 150 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 151 | void weakRef(shared_ptr<PthreadThread> self) { |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 152 | assert(self.get() == this); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 153 | self_ = weak_ptr<PthreadThread>(self); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 154 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 157 | void* PthreadThread::threadMain(void* arg) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 158 | shared_ptr<PthreadThread> thread = *(shared_ptr<PthreadThread>*)arg; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 159 | delete reinterpret_cast<shared_ptr<PthreadThread>*>(arg); |
| 160 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 161 | if (thread == NULL) { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 162 | return (void*)0; |
| 163 | } |
| 164 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 165 | if (thread->state_ != starting) { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 166 | return (void*)0; |
| 167 | } |
| 168 | |
David Reiss | af29695 | 2008-06-10 22:54:40 +0000 | [diff] [blame] | 169 | #if GOOGLE_PERFTOOLS_REGISTER_THREAD |
| 170 | ProfilerRegisterThread(); |
| 171 | #endif |
| 172 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 173 | thread->state_ = starting; |
Marc Slemko | fe5ba12e | 2006-07-20 21:16:27 +0000 | [diff] [blame] | 174 | thread->runnable()->run(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 175 | if (thread->state_ != stopping && thread->state_ != stopped) { |
| 176 | thread->state_ = stopping; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 177 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 178 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 179 | return (void*)0; |
| 180 | } |
| 181 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 182 | /** |
| 183 | * POSIX Thread factory implementation |
| 184 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 185 | class PosixThreadFactory::Impl { |
| 186 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 187 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 188 | POLICY policy_; |
| 189 | PRIORITY priority_; |
| 190 | int stackSize_; |
| 191 | bool detached_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 192 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 193 | /** |
| 194 | * Converts generic posix thread schedule policy enums into pthread |
| 195 | * API values. |
| 196 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 197 | static int toPthreadPolicy(POLICY policy) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 198 | switch (policy) { |
| 199 | case OTHER: |
| 200 | return SCHED_OTHER; |
| 201 | case FIFO: |
| 202 | return SCHED_FIFO; |
| 203 | case ROUND_ROBIN: |
| 204 | return SCHED_RR; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 205 | } |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 206 | return SCHED_OTHER; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 209 | /** |
| 210 | * Converts relative thread priorities to absolute value based on posix |
| 211 | * thread scheduler policy |
| 212 | * |
| 213 | * The idea is simply to divide up the priority range for the given policy |
| 214 | * into the correpsonding relative priority level (lowest..highest) and |
| 215 | * then pro-rate accordingly. |
| 216 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 217 | static int toPthreadPriority(POLICY policy, PRIORITY priority) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 218 | int pthread_policy = toPthreadPolicy(policy); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 219 | int min_priority = sched_get_priority_min(pthread_policy); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 220 | int max_priority = sched_get_priority_max(pthread_policy); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 221 | int quanta = (HIGHEST - LOWEST) + 1; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 222 | float stepsperquanta = (max_priority - min_priority) / quanta; |
| 223 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 224 | if (priority <= HIGHEST) { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 225 | return (int)(min_priority + stepsperquanta * priority); |
| 226 | } else { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 227 | // should never get here for priority increments. |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 228 | assert(false); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 229 | return (int)(min_priority + stepsperquanta * NORMAL); |
| 230 | } |
| 231 | } |
| 232 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 233 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 234 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 235 | Impl(POLICY policy, PRIORITY priority, int stackSize, bool detached) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 236 | policy_(policy), |
| 237 | priority_(priority), |
| 238 | stackSize_(stackSize), |
| 239 | detached_(detached) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 240 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 241 | /** |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 242 | * Creates a new POSIX thread to run the runnable object |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 243 | * |
| 244 | * @param runnable A runnable object |
| 245 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 246 | shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const { |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 247 | shared_ptr<PthreadThread> result = shared_ptr<PthreadThread>(new PthreadThread(toPthreadPolicy(policy_), toPthreadPriority(policy_, priority_), stackSize_, detached_, runnable)); |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 248 | result->weakRef(result); |
| 249 | runnable->thread(result); |
| 250 | return result; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 253 | int getStackSize() const { return stackSize_; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 254 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 255 | void setStackSize(int value) { stackSize_ = value; } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 256 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 257 | PRIORITY getPriority() const { return priority_; } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 258 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 259 | /** |
| 260 | * Sets priority. |
| 261 | * |
| 262 | * XXX |
| 263 | * Need to handle incremental priorities properly. |
| 264 | */ |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 265 | void setPriority(PRIORITY value) { priority_ = value; } |
| 266 | |
| 267 | bool isDetached() const { return detached_; } |
| 268 | |
| 269 | void setDetached(bool value) { detached_ = value; } |
| 270 | |
Mark Slee | 9843915 | 2007-08-21 02:39:40 +0000 | [diff] [blame] | 271 | Thread::id_t getCurrentThreadId() const { |
| 272 | // TODO(dreiss): Stop using C-style casts. |
| 273 | return (id_t)pthread_self(); |
| 274 | } |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 275 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 278 | PosixThreadFactory::PosixThreadFactory(POLICY policy, PRIORITY priority, int stackSize, bool detached) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 279 | impl_(new PosixThreadFactory::Impl(policy, priority, stackSize, detached)) {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 280 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 281 | 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] | 282 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 283 | int PosixThreadFactory::getStackSize() const { return impl_->getStackSize(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 284 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 285 | void PosixThreadFactory::setStackSize(int value) { impl_->setStackSize(value); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 286 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 287 | PosixThreadFactory::PRIORITY PosixThreadFactory::getPriority() const { return impl_->getPriority(); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 288 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 289 | void PosixThreadFactory::setPriority(PosixThreadFactory::PRIORITY value) { impl_->setPriority(value); } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 290 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 291 | bool PosixThreadFactory::isDetached() const { return impl_->isDetached(); } |
| 292 | |
| 293 | void PosixThreadFactory::setDetached(bool value) { impl_->setDetached(value); } |
| 294 | |
| 295 | Thread::id_t PosixThreadFactory::getCurrentThreadId() const { return impl_->getCurrentThreadId(); } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 296 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 297 | }}} // facebook::thrift::concurrency |