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