blob: a56999c80879c51e447242fa56ddd246e5b53af0 [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_
2#define _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00003
4#include "Thread.h"
5
Marc Slemko6f038a72006-08-03 18:58:09 +00006#include <boost/shared_ptr.hpp>
7
Marc Slemko66949872006-07-15 01:52:39 +00008namespace facebook { namespace thrift { namespace concurrency {
9
Marc Slemko6f038a72006-08-03 18:58:09 +000010using namespace boost;
11
Mark Sleef5f2be42006-09-05 21:05:31 +000012/**
13 * A thread factory to create posix threads
14 *
15 * @author marc
16 * @version $Id:$
17 */
Marc Slemko66949872006-07-15 01:52:39 +000018class PosixThreadFactory : public ThreadFactory {
19
20 public:
21
Mark Sleef5f2be42006-09-05 21:05:31 +000022 /**
23 * POSIX Thread scheduler policies
24 */
Marc Slemko66949872006-07-15 01:52:39 +000025 enum POLICY {
26 OTHER,
27 FIFO,
28 ROUND_ROBIN
29 };
30
Mark Sleef5f2be42006-09-05 21:05:31 +000031 /**
32 * POSIX Thread scheduler relative priorities,
33 *
34 * Absolute priority is determined by scheduler policy and OS. This
35 * enumeration specifies relative priorities such that one can specify a
36 * priority withing a giving scheduler policy without knowing the absolute
37 * value of the priority.
38 */
Marc Slemko66949872006-07-15 01:52:39 +000039 enum PRIORITY {
40 LOWEST = 0,
41 LOWER = 1,
42 LOW = 2,
43 NORMAL = 3,
44 HIGH = 4,
45 HIGHER = 5,
46 HIGHEST = 6,
47 INCREMENT = 7,
48 DECREMENT = 8
49 };
50
51 PosixThreadFactory(POLICY policy=ROUND_ROBIN, PRIORITY priority=NORMAL, int stackSize=1, bool detached=false);
52
53 // From ThreadFactory;
Marc Slemko6f038a72006-08-03 18:58:09 +000054 shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const;
Marc Slemko66949872006-07-15 01:52:39 +000055
Mark Sleef5f2be42006-09-05 21:05:31 +000056 /**
57 * Sets stack size for created threads
58 *
59 * @param value size in megabytes
60 */
Marc Slemko66949872006-07-15 01:52:39 +000061 virtual void stackSize(int value);
62
Mark Sleef5f2be42006-09-05 21:05:31 +000063 /**
64 * Gets stack size for created threads
65 *
66 * @return int size in megabytes
67 */
Marc Slemko66949872006-07-15 01:52:39 +000068 virtual int stackSize() const;
69
Mark Sleef5f2be42006-09-05 21:05:31 +000070 /**
71 * Sets priority relative to current policy
72 */
Marc Slemko66949872006-07-15 01:52:39 +000073 virtual void priority(PRIORITY priority);
74
Mark Sleef5f2be42006-09-05 21:05:31 +000075 /**
76 * Gets priority relative to current policy
77 */
Marc Slemko66949872006-07-15 01:52:39 +000078 virtual PRIORITY priority() const;
79
80 private:
Marc Slemko66949872006-07-15 01:52:39 +000081 class Impl;
Marc Slemko6f038a72006-08-03 18:58:09 +000082 shared_ptr<Impl> _impl;
Marc Slemko66949872006-07-15 01:52:39 +000083};
84
85}}} // facebook::thrift::concurrency
86
Mark Sleef5f2be42006-09-05 21:05:31 +000087#endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_