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