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