blob: 4e50dffd6712f26e5bc4fa8e936e83ea22eb8385 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_
8#define _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00009
10#include "Thread.h"
11
Marc Slemko6f038a72006-08-03 18:58:09 +000012#include <boost/shared_ptr.hpp>
13
Marc Slemko66949872006-07-15 01:52:39 +000014namespace facebook { namespace thrift { namespace concurrency {
15
Marc Slemko6f038a72006-08-03 18:58:09 +000016using namespace boost;
17
Mark Sleef5f2be42006-09-05 21:05:31 +000018/**
19 * A thread factory to create posix threads
20 *
21 * @author marc
22 * @version $Id:$
23 */
Marc Slemko66949872006-07-15 01:52:39 +000024class PosixThreadFactory : public ThreadFactory {
25
26 public:
27
Mark Sleef5f2be42006-09-05 21:05:31 +000028 /**
29 * POSIX Thread scheduler policies
30 */
Marc Slemko66949872006-07-15 01:52:39 +000031 enum POLICY {
32 OTHER,
33 FIFO,
34 ROUND_ROBIN
35 };
36
Mark Sleef5f2be42006-09-05 21:05:31 +000037 /**
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 Slemko66949872006-07-15 01:52:39 +000045 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 Slemko6f038a72006-08-03 18:58:09 +000060 shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const;
Marc Slemko66949872006-07-15 01:52:39 +000061
Mark Sleef5f2be42006-09-05 21:05:31 +000062 /**
63 * Sets stack size for created threads
64 *
65 * @param value size in megabytes
66 */
Marc Slemko66949872006-07-15 01:52:39 +000067 virtual void stackSize(int value);
68
Mark Sleef5f2be42006-09-05 21:05:31 +000069 /**
70 * Gets stack size for created threads
71 *
72 * @return int size in megabytes
73 */
Marc Slemko66949872006-07-15 01:52:39 +000074 virtual int stackSize() const;
75
Mark Sleef5f2be42006-09-05 21:05:31 +000076 /**
77 * Sets priority relative to current policy
78 */
Marc Slemko66949872006-07-15 01:52:39 +000079 virtual void priority(PRIORITY priority);
80
Mark Sleef5f2be42006-09-05 21:05:31 +000081 /**
82 * Gets priority relative to current policy
83 */
Marc Slemko66949872006-07-15 01:52:39 +000084 virtual PRIORITY priority() const;
85
86 private:
Marc Slemko66949872006-07-15 01:52:39 +000087 class Impl;
Mark Slee2f6404d2006-10-10 01:37:40 +000088 shared_ptr<Impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +000089};
90
91}}} // facebook::thrift::concurrency
92
Mark Sleef5f2be42006-09-05 21:05:31 +000093#endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_