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 | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace concurrency { |
| 15 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 16 | using namespace boost; |
| 17 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 18 | /** |
| 19 | * A thread factory to create posix threads |
| 20 | * |
| 21 | * @author marc |
| 22 | * @version $Id:$ |
| 23 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 24 | class PosixThreadFactory : public ThreadFactory { |
| 25 | |
| 26 | public: |
| 27 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 28 | /** |
| 29 | * POSIX Thread scheduler policies |
| 30 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | enum POLICY { |
| 32 | OTHER, |
| 33 | FIFO, |
| 34 | ROUND_ROBIN |
| 35 | }; |
| 36 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 37 | /** |
| 38 | * POSIX Thread scheduler relative priorities, |
| 39 | * |
| 40 | * Absolute priority is determined by scheduler policy and OS. This |
| 41 | * enumeration specifies relative priorities such that one can specify a |
| 42 | * priority withing a giving scheduler policy without knowing the absolute |
| 43 | * value of the priority. |
| 44 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 45 | enum PRIORITY { |
| 46 | LOWEST = 0, |
| 47 | LOWER = 1, |
| 48 | LOW = 2, |
| 49 | NORMAL = 3, |
| 50 | HIGH = 4, |
| 51 | HIGHER = 5, |
| 52 | HIGHEST = 6, |
| 53 | INCREMENT = 7, |
| 54 | DECREMENT = 8 |
| 55 | }; |
| 56 | |
| 57 | PosixThreadFactory(POLICY policy=ROUND_ROBIN, PRIORITY priority=NORMAL, int stackSize=1, bool detached=false); |
| 58 | |
| 59 | // From ThreadFactory; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 60 | shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 61 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Sets stack size for created threads |
| 64 | * |
| 65 | * @param value size in megabytes |
| 66 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 67 | virtual void stackSize(int value); |
| 68 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 69 | /** |
| 70 | * Gets stack size for created threads |
| 71 | * |
| 72 | * @return int size in megabytes |
| 73 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 74 | virtual int stackSize() const; |
| 75 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Sets priority relative to current policy |
| 78 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 79 | virtual void priority(PRIORITY priority); |
| 80 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 81 | /** |
| 82 | * Gets priority relative to current policy |
| 83 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 84 | virtual PRIORITY priority() const; |
| 85 | |
| 86 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 87 | class Impl; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 88 | shared_ptr<Impl> impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | }}} // facebook::thrift::concurrency |
| 92 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 93 | #endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ |