1 /*
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  */
19 
20 #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_
21 #define _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_ 1
22 
23 #include <thrift/concurrency/Thread.h>
24 
25 #include <thrift/stdcxx.h>
26 
27 namespace apache {
28 namespace thrift {
29 namespace concurrency {
30 
31 /**
32  * A thread factory to create posix threads
33  *
34  * @version $Id:$
35  */
36 class PosixThreadFactory : public ThreadFactory {
37 
38 public:
39   /**
40    * POSIX Thread scheduler policies
41    */
42   enum POLICY { OTHER, FIFO, ROUND_ROBIN };
43 
44   /**
45    * POSIX Thread scheduler relative priorities,
46    *
47    * Absolute priority is determined by scheduler policy and OS. This
48    * enumeration specifies relative priorities such that one can specify a
49    * priority within a giving scheduler policy without knowing the absolute
50    * value of the priority.
51    */
52   enum PRIORITY {
53     LOWEST = 0,
54     LOWER = 1,
55     LOW = 2,
56     NORMAL = 3,
57     HIGH = 4,
58     HIGHER = 5,
59     HIGHEST = 6,
60     INCREMENT = 7,
61     DECREMENT = 8
62   };
63 
64   /**
65    * Posix thread (pthread) factory.  All threads created by a factory are reference-counted
66    * via stdcxx::shared_ptr.  The factory guarantees that threads and the Runnable tasks
67    * they host will be properly cleaned up once the last strong reference to both is
68    * given up.
69    *
70    * Threads are created with the specified policy, priority, stack-size and detachable-mode
71    * detached means the thread is free-running and will release all system resources the
72    * when it completes.  A detachable thread is not joinable.  The join method
73    * of a detachable thread will return immediately with no error.
74    *
75    * By default threads are not joinable.
76    */
77   PosixThreadFactory(POLICY policy = ROUND_ROBIN,
78                      PRIORITY priority = NORMAL,
79                      int stackSize = 1,
80                      bool detached = true);
81 
82   /**
83    * Provide a constructor compatible with the other factories
84    * The default policy is POLICY::ROUND_ROBIN.
85    * The default priority is PRIORITY::NORMAL.
86    * The default stackSize is 1.
87    */
88   PosixThreadFactory(bool detached);
89 
90   // From ThreadFactory;
91   stdcxx::shared_ptr<Thread> newThread(stdcxx::shared_ptr<Runnable> runnable) const;
92 
93   // From ThreadFactory;
94   Thread::id_t getCurrentThreadId() const;
95 
96   /**
97    * Gets stack size for newly created threads
98    *
99    * @return int size in megabytes
100    */
101   virtual int getStackSize() const;
102 
103   /**
104    * Sets stack size for newly created threads
105    *
106    * @param value size in megabytes
107    */
108   virtual void setStackSize(int value);
109 
110   /**
111    * Gets priority relative to current policy
112    */
113   virtual PRIORITY getPriority() const;
114 
115   /**
116    * Sets priority relative to current policy
117    */
118   virtual void setPriority(PRIORITY priority);
119 
120 private:
121   POLICY policy_;
122   PRIORITY priority_;
123   int stackSize_;
124 };
125 }
126 }
127 } // apache::thrift::concurrency
128 
129 #endif // #ifndef _THRIFT_CONCURRENCY_POSIXTHREADFACTORY_H_
130