blob: d6d83a3a13611fbe6e6901bbf8d6df2dbebfa3fd [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_
21#define _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +000022
23#include "Thread.h"
24
Marc Slemko6f038a72006-08-03 18:58:09 +000025#include <boost/shared_ptr.hpp>
26
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000028
Mark Sleef5f2be42006-09-05 21:05:31 +000029/**
Marc Slemkoa6479032007-06-05 22:20:14 +000030 * A thread factory to create posix threads
Mark Sleef5f2be42006-09-05 21:05:31 +000031 *
Mark Sleef5f2be42006-09-05 21:05:31 +000032 * @version $Id:$
33 */
Marc Slemko66949872006-07-15 01:52:39 +000034class PosixThreadFactory : public ThreadFactory {
35
36 public:
37
Mark Sleef5f2be42006-09-05 21:05:31 +000038 /**
39 * POSIX Thread scheduler policies
40 */
Marc Slemko66949872006-07-15 01:52:39 +000041 enum POLICY {
42 OTHER,
43 FIFO,
44 ROUND_ROBIN
45 };
46
Mark Sleef5f2be42006-09-05 21:05:31 +000047 /**
48 * POSIX Thread scheduler relative priorities,
49 *
50 * Absolute priority is determined by scheduler policy and OS. This
51 * enumeration specifies relative priorities such that one can specify a
52 * priority withing a giving scheduler policy without knowing the absolute
53 * value of the priority.
54 */
Marc Slemko66949872006-07-15 01:52:39 +000055 enum PRIORITY {
56 LOWEST = 0,
57 LOWER = 1,
58 LOW = 2,
59 NORMAL = 3,
60 HIGH = 4,
61 HIGHER = 5,
62 HIGHEST = 6,
63 INCREMENT = 7,
64 DECREMENT = 8
65 };
66
Marc Slemkoa6479032007-06-05 22:20:14 +000067 /**
68 * Posix thread (pthread) factory. All threads created by a factory are reference-counted
69 * via boost::shared_ptr and boost::weak_ptr. The factory guarantees that threads and
Marc Slemko67606e52007-06-04 21:01:19 +000070 * the Runnable tasks they host will be properly cleaned up once the last strong reference
71 * to both is given up.
72 *
73 * Threads are created with the specified policy, priority, stack-size and detachable-mode
Marc Slemkoa6479032007-06-05 22:20:14 +000074 * detached means the thread is free-running and will release all system resources the
75 * when it completes. A detachable thread is not joinable. The join method
76 * of a detachable thread will return immediately with no error.
Marc Slemko67606e52007-06-04 21:01:19 +000077 *
Mark Slee17496a02007-08-02 06:37:40 +000078 * By default threads are not joinable.
Marc Slemko67606e52007-06-04 21:01:19 +000079 */
80
Aditya Agarwal80cdca72007-08-02 06:26:11 +000081 PosixThreadFactory(POLICY policy=ROUND_ROBIN, PRIORITY priority=NORMAL, int stackSize=1, bool detached=true);
Marc Slemko66949872006-07-15 01:52:39 +000082
83 // From ThreadFactory;
Mark Slee5ea15f92007-03-05 22:55:59 +000084 boost::shared_ptr<Thread> newThread(boost::shared_ptr<Runnable> runnable) const;
Marc Slemko66949872006-07-15 01:52:39 +000085
Marc Slemko3a3b53b2007-05-22 23:59:54 +000086 // From ThreadFactory;
Marc Slemkoa6479032007-06-05 22:20:14 +000087 Thread::id_t getCurrentThreadId() const;
Marc Slemko66949872006-07-15 01:52:39 +000088
Mark Sleef5f2be42006-09-05 21:05:31 +000089 /**
90 * Gets stack size for created threads
91 *
92 * @return int size in megabytes
93 */
Marc Slemkoa6479032007-06-05 22:20:14 +000094 virtual int getStackSize() const;
Marc Slemko66949872006-07-15 01:52:39 +000095
Mark Sleef5f2be42006-09-05 21:05:31 +000096 /**
Marc Slemkoa6479032007-06-05 22:20:14 +000097 * Sets stack size for created threads
98 *
99 * @param value size in megabytes
Mark Sleef5f2be42006-09-05 21:05:31 +0000100 */
Marc Slemkoa6479032007-06-05 22:20:14 +0000101 virtual void setStackSize(int value);
Marc Slemko66949872006-07-15 01:52:39 +0000102
Mark Sleef5f2be42006-09-05 21:05:31 +0000103 /**
104 * Gets priority relative to current policy
105 */
Marc Slemkoa6479032007-06-05 22:20:14 +0000106 virtual PRIORITY getPriority() const;
107
108 /**
109 * Sets priority relative to current policy
110 */
111 virtual void setPriority(PRIORITY priority);
112
113 /**
114 * Sets detached mode of threads
115 */
116 virtual void setDetached(bool detached);
117
118 /**
119 * Gets current detached mode
120 */
121 virtual bool isDetached() const;
122
Marc Slemko66949872006-07-15 01:52:39 +0000123 private:
Marc Slemko66949872006-07-15 01:52:39 +0000124 class Impl;
Mark Slee5ea15f92007-03-05 22:55:59 +0000125 boost::shared_ptr<Impl> impl_;
Marc Slemko66949872006-07-15 01:52:39 +0000126};
127
T Jake Lucianib5e62212009-01-31 22:36:20 +0000128}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000129
Mark Sleef5f2be42006-09-05 21:05:31 +0000130#endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_