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 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ |
| 21 | #define _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | |
| 23 | #include "Thread.h" |
| 24 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 25 | #include <boost/shared_ptr.hpp> |
| 26 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 27 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 28 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 29 | /** |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 30 | * A thread factory to create posix threads |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 32 | * @version $Id:$ |
| 33 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 34 | class PosixThreadFactory : public ThreadFactory { |
| 35 | |
| 36 | public: |
| 37 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 38 | /** |
| 39 | * POSIX Thread scheduler policies |
| 40 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 41 | enum POLICY { |
| 42 | OTHER, |
| 43 | FIFO, |
| 44 | ROUND_ROBIN |
| 45 | }; |
| 46 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 47 | /** |
| 48 | * POSIX Thread scheduler relative priorities, |
| 49 | * |
| 50 | * Absolute priority is determined by scheduler policy and OS. This |
| 51 | * enumeration specifies relative priorities such that one can specify a |
| 52 | * priority withing a giving scheduler policy without knowing the absolute |
| 53 | * value of the priority. |
| 54 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 55 | enum PRIORITY { |
| 56 | LOWEST = 0, |
| 57 | LOWER = 1, |
| 58 | LOW = 2, |
| 59 | NORMAL = 3, |
| 60 | HIGH = 4, |
| 61 | HIGHER = 5, |
| 62 | HIGHEST = 6, |
| 63 | INCREMENT = 7, |
| 64 | DECREMENT = 8 |
| 65 | }; |
| 66 | |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 67 | /** |
| 68 | * Posix thread (pthread) factory. All threads created by a factory are reference-counted |
| 69 | * via boost::shared_ptr and boost::weak_ptr. The factory guarantees that threads and |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 70 | * the Runnable tasks they host will be properly cleaned up once the last strong reference |
| 71 | * to both is given up. |
| 72 | * |
| 73 | * Threads are created with the specified policy, priority, stack-size and detachable-mode |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 74 | * detached means the thread is free-running and will release all system resources the |
| 75 | * when it completes. A detachable thread is not joinable. The join method |
| 76 | * of a detachable thread will return immediately with no error. |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 77 | * |
Mark Slee | 17496a0 | 2007-08-02 06:37:40 +0000 | [diff] [blame] | 78 | * By default threads are not joinable. |
Marc Slemko | 67606e5 | 2007-06-04 21:01:19 +0000 | [diff] [blame] | 79 | */ |
| 80 | |
Aditya Agarwal | 80cdca7 | 2007-08-02 06:26:11 +0000 | [diff] [blame] | 81 | PosixThreadFactory(POLICY policy=ROUND_ROBIN, PRIORITY priority=NORMAL, int stackSize=1, bool detached=true); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 82 | |
| 83 | // From ThreadFactory; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 84 | boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 85 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 86 | // From ThreadFactory; |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 87 | Thread::id_t getCurrentThreadId() const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 88 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Gets stack size for created threads |
| 91 | * |
| 92 | * @return int size in megabytes |
| 93 | */ |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 94 | virtual int getStackSize() const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 95 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 96 | /** |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 97 | * Sets stack size for created threads |
| 98 | * |
| 99 | * @param value size in megabytes |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 100 | */ |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 101 | virtual void setStackSize(int value); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 102 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 103 | /** |
| 104 | * Gets priority relative to current policy |
| 105 | */ |
Marc Slemko | a647903 | 2007-06-05 22:20:14 +0000 | [diff] [blame] | 106 | virtual PRIORITY getPriority() const; |
| 107 | |
| 108 | /** |
| 109 | * Sets priority relative to current policy |
| 110 | */ |
| 111 | virtual void setPriority(PRIORITY priority); |
| 112 | |
| 113 | /** |
| 114 | * Sets detached mode of threads |
| 115 | */ |
| 116 | virtual void setDetached(bool detached); |
| 117 | |
| 118 | /** |
| 119 | * Gets current detached mode |
| 120 | */ |
| 121 | virtual bool isDetached() const; |
| 122 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 123 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 124 | class Impl; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 125 | boost::shared_ptr<Impl> impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 128 | }}} // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 129 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 130 | #endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ |