1 mike 1.2 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 mike 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
|
30 kamal.locahana 1.4 //=============================================================================
|
31 mike 1.2 //
|
32 kamal.locahana 1.4 //%////////////////////////////////////////////////////////////////////////////
|
33 mike 1.2
34 #ifndef Pegasus_ThreadPool_h
35 #define Pegasus_ThreadPool_h
36
37 #include <Pegasus/Common/Config.h>
38 #include <Pegasus/Common/Linkage.h>
39 #include <Pegasus/Common/Thread.h>
40
|
41 kumpf 1.3 PEGASUS_NAMESPACE_BEGIN
|
42 mike 1.2
43 class PEGASUS_COMMON_LINKAGE ThreadPool
44 {
45 public:
46
47 /**
48 Constructs a new ThreadPool object.
49 @param initialSize The number of threads that are initially added to
50 the thread pool.
51 @param key A name for this thread pool that can be used to determine
52 equality of two thread pool objects. Only the first 16 characters
53 of this value are used.
54 @param minThreads The minimum number of threads that should be
55 contained in this thread pool at any given time.
56 @param maxThreads The maximum number of threads that should be
57 contained in this thread pool at any given time.
58 @param deallocateWait The minimum time that a thread should be idle
59 before it is removed from the pool and cleaned up.
60 */
|
61 kumpf 1.3 ThreadPool(
62 Sint16 initialSize,
63 const char* key,
64 Sint16 minThreads,
65 Sint16 maxThreads,
66 struct timeval& deallocateWait);
|
67 mike 1.2
68 /**
69 Destructs the ThreadPool object.
70 */
71 ~ThreadPool();
72
73 /**
74 Allocate and start a thread to do a unit of work.
75 @param parm A generic parameter to pass to the thread
76 @param work A pointer to the function that is to be executed by
77 the thread
78 @param blocking A pointer to an optional semaphore which, if
79 specified, is signaled after the thread finishes
80 executing the work function
|
81 kumpf 1.3 @return PEGASUS_THREAD_OK if the thread is started successfully,
82 PEGASUS_THREAD_INSUFFICIENT_RESOURCES if the
|
83 mike 1.2 resources necessary to start the thread are not currently
84 available. PEGASUS_THREAD_SETUP_FAILURE if the thread
85 could not be setup properly. PEGASUS_THREAD_UNAVAILABLE
86 if this service is shutting down and no more threads can
87 be allocated.
88 */
|
89 kumpf 1.3 ThreadStatus allocate_and_awaken(
90 void* parm,
91 ThreadReturnType(PEGASUS_THREAD_CDECL* work) (void*),
92 Semaphore* blocking = 0);
|
93 mike 1.2
94 /**
95 Cleans up idle threads if they have been running longer than the
96 deallocate_wait configuration and more than the configured
97 minimum number of threads is running.
98 @return The number of threads that were cleaned up.
99 */
100 Uint32 cleanupIdleThreads();
101
|
102 kumpf 1.3 void get_key(Sint8* buf, int bufsize);
|
103 mike 1.2
104 inline void setMinThreads(Sint16 min)
105 {
106 _minThreads = min;
107 }
108
109 inline Sint16 getMinThreads() const
110 {
111 return _minThreads;
112 }
113
114 inline void setMaxThreads(Sint16 max)
115 {
116 _maxThreads = max;
117 }
118
119 inline Sint16 getMaxThreads() const
120 {
121 return _maxThreads;
122 }
123
124 mike 1.2 inline Uint32 runningCount()
125 {
|
126 kamal.locahana 1.4 return (Uint32) _runningThreads.size();
|
127 mike 1.2 }
128
129 inline Uint32 idleCount()
130 {
|
131 kamal.locahana 1.4 return (Uint32) _idleThreads.size();
|
132 mike 1.2 }
133
134 private:
135
136 ThreadPool(); // Unimplemented
137 ThreadPool(const ThreadPool &); // Unimplemented
138 ThreadPool & operator=(const ThreadPool &); // Unimplemented
139
140 static ThreadReturnType PEGASUS_THREAD_CDECL _loop(void *);
141
|
142 kumpf 1.3 static Boolean _timeIntervalExpired(
143 struct timeval* start,
144 struct timeval* interval);
145
146 static void _deleteSemaphore(void* p);
147
148 void _cleanupThread(Thread* thread);
149 Thread* _initializeThread();
150 void _addToIdleThreadsQueue(Thread* th);
|
151 mike 1.2
152 Sint16 _maxThreads;
153 Sint16 _minThreads;
154 AtomicInt _currentThreads;
155 struct timeval _deallocateWait;
156 char _key[17];
|
157 kumpf 1.3 List<Thread, Mutex> _idleThreads;
158 List<Thread, Mutex> _runningThreads;
|
159 mike 1.2 AtomicInt _dying;
160 };
161
162 PEGASUS_NAMESPACE_END
163
164 #endif // Pegasus_ThreadPool_h
|