blob: 894d5f5c92f97abb5d04f148e41bf24613945774 [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 Slemkoa6479032007-06-05 22:20:14 +000014namespace facebook { 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 *
19 * @author marc
20 * @version $Id:$
21 */
Marc Slemko66949872006-07-15 01:52:39 +000022class PosixThreadFactory : public ThreadFactory {
23
24 public:
25
Mark Sleef5f2be42006-09-05 21:05:31 +000026 /**
27 * POSIX Thread scheduler policies
28 */
Marc Slemko66949872006-07-15 01:52:39 +000029 enum POLICY {
30 OTHER,
31 FIFO,
32 ROUND_ROBIN
33 };
34
Mark Sleef5f2be42006-09-05 21:05:31 +000035 /**
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 Slemko66949872006-07-15 01:52:39 +000043 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 Slemkoa6479032007-06-05 22:20:14 +000055 /**
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 Slemko67606e52007-06-04 21:01:19 +000058 * 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 Slemkoa6479032007-06-05 22:20:14 +000062 * 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 Slemko67606e52007-06-04 21:01:19 +000065 *
66 * Joinable threads will detach themselves iff they were not explicitly joined and
67 * there are no remaining strong references to the thread. This guarantees that
Marc Slemkoa6479032007-06-05 22:20:14 +000068 * joinnable threads don't leak resources even when the application neglects to
Marc Slemko67606e52007-06-04 21:01:19 +000069 * call join explicitly.
70 *
71 * By default threads are joinable.
72 */
73
Marc Slemko66949872006-07-15 01:52:39 +000074 PosixThreadFactory(POLICY policy=ROUND_ROBIN, PRIORITY priority=NORMAL, int stackSize=1, bool detached=false);
75
76 // From ThreadFactory;
Mark Slee5ea15f92007-03-05 22:55:59 +000077 boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const;
Marc Slemko66949872006-07-15 01:52:39 +000078
Marc Slemko3a3b53b2007-05-22 23:59:54 +000079 // From ThreadFactory;
Marc Slemkoa6479032007-06-05 22:20:14 +000080 Thread::id_t getCurrentThreadId() const;
Marc Slemko66949872006-07-15 01:52:39 +000081
Mark Sleef5f2be42006-09-05 21:05:31 +000082 /**
83 * Gets stack size for created threads
84 *
85 * @return int size in megabytes
86 */
Marc Slemkoa6479032007-06-05 22:20:14 +000087 virtual int getStackSize() const;
Marc Slemko66949872006-07-15 01:52:39 +000088
Mark Sleef5f2be42006-09-05 21:05:31 +000089 /**
Marc Slemkoa6479032007-06-05 22:20:14 +000090 * Sets stack size for created threads
91 *
92 * @param value size in megabytes
Mark Sleef5f2be42006-09-05 21:05:31 +000093 */
Marc Slemkoa6479032007-06-05 22:20:14 +000094 virtual void setStackSize(int value);
Marc Slemko66949872006-07-15 01:52:39 +000095
Mark Sleef5f2be42006-09-05 21:05:31 +000096 /**
97 * Gets priority relative to current policy
98 */
Marc Slemkoa6479032007-06-05 22:20:14 +000099 virtual PRIORITY getPriority() const;
100
101 /**
102 * Sets priority relative to current policy
103 */
104 virtual void setPriority(PRIORITY priority);
105
106 /**
107 * Sets detached mode of threads
108 */
109 virtual void setDetached(bool detached);
110
111 /**
112 * Gets current detached mode
113 */
114 virtual bool isDetached() const;
115
Marc Slemko66949872006-07-15 01:52:39 +0000116 private:
Marc Slemko66949872006-07-15 01:52:39 +0000117 class Impl;
Mark Slee5ea15f92007-03-05 22:55:59 +0000118 boost::shared_ptr<Impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +0000119};
120
121}}} // facebook::thrift::concurrency
122
Mark Sleef5f2be42006-09-05 21:05:31 +0000123#endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_