Gavin McDonald | 0b75e1a | 2010-10-28 02:12:01 +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 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ |
| 21 | #define _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ 1 |
| 22 | |
| 23 | #include "Thread.h" |
| 24 | |
| 25 | #include <boost/shared_ptr.hpp> |
| 26 | |
| 27 | namespace apache { namespace thrift { namespace concurrency { |
| 28 | |
| 29 | /** |
| 30 | * A thread factory to create posix threads |
| 31 | * |
| 32 | * @version $Id:$ |
| 33 | */ |
| 34 | class PosixThreadFactory : public ThreadFactory { |
| 35 | |
| 36 | public: |
| 37 | |
| 38 | /** |
| 39 | * POSIX Thread scheduler policies |
| 40 | */ |
| 41 | enum POLICY { |
| 42 | OTHER, |
| 43 | FIFO, |
| 44 | ROUND_ROBIN |
| 45 | }; |
| 46 | |
| 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 | */ |
| 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 | |
| 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 |
| 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 |
| 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. |
| 77 | * |
| 78 | * By default threads are not joinable. |
| 79 | */ |
| 80 | |
| 81 | PosixThreadFactory(POLICY policy=ROUND_ROBIN, PRIORITY priority=NORMAL, int stackSize=1, bool detached=true); |
| 82 | |
| 83 | // From ThreadFactory; |
| 84 | boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const; |
| 85 | |
| 86 | // From ThreadFactory; |
| 87 | Thread::id_t getCurrentThreadId() const; |
| 88 | |
| 89 | /** |
| 90 | * Gets stack size for created threads |
| 91 | * |
| 92 | * @return int size in megabytes |
| 93 | */ |
| 94 | virtual int getStackSize() const; |
| 95 | |
| 96 | /** |
| 97 | * Sets stack size for created threads |
| 98 | * |
| 99 | * @param value size in megabytes |
| 100 | */ |
| 101 | virtual void setStackSize(int value); |
| 102 | |
| 103 | /** |
| 104 | * Gets priority relative to current policy |
| 105 | */ |
| 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 | |
| 123 | private: |
| 124 | class Impl; |
| 125 | boost::shared_ptr<Impl> impl_; |
| 126 | }; |
| 127 | |
| 128 | }}} // apache::thrift::concurrency |
| 129 | |
| 130 | #endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ |