blob: 2baa0011a2183832646a4f837be9c3dc6170d9c1 [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
T Jake Lucianib5e62212009-01-31 22:36:20 +000014namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000015
Mark Sleef5f2be42006-09-05 21:05:31 +000016/**
Marc Slemkoa6479032007-06-05 22:20:14 +000017 * A thread factory to create posix threads
Mark Sleef5f2be42006-09-05 21:05:31 +000018 *
Mark Sleef5f2be42006-09-05 21:05:31 +000019 * @version $Id:$
20 */
Marc Slemko66949872006-07-15 01:52:39 +000021class PosixThreadFactory : public ThreadFactory {
22
23 public:
24
Mark Sleef5f2be42006-09-05 21:05:31 +000025 /**
26 * POSIX Thread scheduler policies
27 */
Marc Slemko66949872006-07-15 01:52:39 +000028 enum POLICY {
29 OTHER,
30 FIFO,
31 ROUND_ROBIN
32 };
33
Mark Sleef5f2be42006-09-05 21:05:31 +000034 /**
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 Slemko66949872006-07-15 01:52:39 +000042 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 Slemkoa6479032007-06-05 22:20:14 +000054 /**
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 Slemko67606e52007-06-04 21:01:19 +000057 * 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 Slemkoa6479032007-06-05 22:20:14 +000061 * 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 Slemko67606e52007-06-04 21:01:19 +000064 *
Mark Slee17496a02007-08-02 06:37:40 +000065 * By default threads are not joinable.
Marc Slemko67606e52007-06-04 21:01:19 +000066 */
67
Aditya Agarwal80cdca72007-08-02 06:26:11 +000068 PosixThreadFactory(POLICY policy=ROUND_ROBIN, PRIORITY priority=NORMAL, int stackSize=1, bool detached=true);
Marc Slemko66949872006-07-15 01:52:39 +000069
70 // From ThreadFactory;
Mark Slee5ea15f92007-03-05 22:55:59 +000071 boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const;
Marc Slemko66949872006-07-15 01:52:39 +000072
Marc Slemko3a3b53b2007-05-22 23:59:54 +000073 // From ThreadFactory;
Marc Slemkoa6479032007-06-05 22:20:14 +000074 Thread::id_t getCurrentThreadId() const;
Marc Slemko66949872006-07-15 01:52:39 +000075
Mark Sleef5f2be42006-09-05 21:05:31 +000076 /**
77 * Gets stack size for created threads
78 *
79 * @return int size in megabytes
80 */
Marc Slemkoa6479032007-06-05 22:20:14 +000081 virtual int getStackSize() const;
Marc Slemko66949872006-07-15 01:52:39 +000082
Mark Sleef5f2be42006-09-05 21:05:31 +000083 /**
Marc Slemkoa6479032007-06-05 22:20:14 +000084 * Sets stack size for created threads
85 *
86 * @param value size in megabytes
Mark Sleef5f2be42006-09-05 21:05:31 +000087 */
Marc Slemkoa6479032007-06-05 22:20:14 +000088 virtual void setStackSize(int value);
Marc Slemko66949872006-07-15 01:52:39 +000089
Mark Sleef5f2be42006-09-05 21:05:31 +000090 /**
91 * Gets priority relative to current policy
92 */
Marc Slemkoa6479032007-06-05 22:20:14 +000093 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 Slemko66949872006-07-15 01:52:39 +0000110 private:
Marc Slemko66949872006-07-15 01:52:39 +0000111 class Impl;
Mark Slee5ea15f92007-03-05 22:55:59 +0000112 boost::shared_ptr<Impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +0000113};
114
T Jake Lucianib5e62212009-01-31 22:36:20 +0000115}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000116
Mark Sleef5f2be42006-09-05 21:05:31 +0000117#endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_