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 //==============================================================================
31 //
32 // Author: Mike Day (mdday@us.ibm.com)
33 //
34 // Modified By: Markus Mueller
35 // Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
36 // Amit K Arora, IBM (amita@in.ibm.com) for PEP#101
37 // David Dillard, VERITAS Software Corp.
38 // (david.dillard@veritas.com)
39 // Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)
40 // Josephine Eskaline Joyce, IBM (jojustin@in.ibm.com) for Bug#2393
41 //
42 //%/////////////////////////////////////////////////////////////////////////////
43 mike 1.2
44 #ifndef Pegasus_ThreadPool_h
45 #define Pegasus_ThreadPool_h
46
47 #include <Pegasus/Common/Config.h>
48 #include <Pegasus/Common/Linkage.h>
49 #include <Pegasus/Common/Thread.h>
50
51 PEGASUS_NAMESPACE_BEGIN
52
53 class PEGASUS_COMMON_LINKAGE ThreadPool
54 {
55 public:
56
57 /**
58 Constructs a new ThreadPool object.
59 @param initialSize The number of threads that are initially added to
60 the thread pool.
61 @param key A name for this thread pool that can be used to determine
62 equality of two thread pool objects. Only the first 16 characters
63 of this value are used.
64 mike 1.2 @param minThreads The minimum number of threads that should be
65 contained in this thread pool at any given time.
66 @param maxThreads The maximum number of threads that should be
67 contained in this thread pool at any given time.
68 @param deallocateWait The minimum time that a thread should be idle
69 before it is removed from the pool and cleaned up.
70 */
71 ThreadPool(Sint16 initialSize,
72 const char *key,
73 Sint16 minThreads,
74 Sint16 maxThreads, struct timeval &deallocateWait);
75
76 /**
77 Destructs the ThreadPool object.
78 */
79 ~ThreadPool();
80
81 /**
82 Allocate and start a thread to do a unit of work.
83 @param parm A generic parameter to pass to the thread
84 @param work A pointer to the function that is to be executed by
85 mike 1.2 the thread
86 @param blocking A pointer to an optional semaphore which, if
87 specified, is signaled after the thread finishes
88 executing the work function
89 @return PEGASUS_THREAD_OK if the thread is started successfully,
90 PEGASUS_THREAD_INSUFFICIENT_RESOURCES if the
91 resources necessary to start the thread are not currently
92 available. PEGASUS_THREAD_SETUP_FAILURE if the thread
93 could not be setup properly. PEGASUS_THREAD_UNAVAILABLE
94 if this service is shutting down and no more threads can
95 be allocated.
96 @exception IPCException
97 */
98 ThreadStatus allocate_and_awaken(void *parm,
99 ThreadReturnType(PEGASUS_THREAD_CDECL *
100 work) (void *),
101 Semaphore * blocking = 0);
102
103 /**
104 Cleans up idle threads if they have been running longer than the
105 deallocate_wait configuration and more than the configured
106 mike 1.2 minimum number of threads is running.
107 @return The number of threads that were cleaned up.
108 @exception IPCException
109 */
110 Uint32 cleanupIdleThreads();
111
112 void get_key(Sint8 * buf, int bufsize);
113
114 inline void setMinThreads(Sint16 min)
115 {
116 _minThreads = min;
117 }
118
119 inline Sint16 getMinThreads() const
120 {
121 return _minThreads;
122 }
123
124 inline void setMaxThreads(Sint16 max)
125 {
126 _maxThreads = max;
127 mike 1.2 }
128
129 inline Sint16 getMaxThreads() const
130 {
131 return _maxThreads;
132 }
133
134 inline Uint32 runningCount()
135 {
136 return _runningThreads.size();
137 }
138
139 inline Uint32 idleCount()
140 {
141 return _idleThreads.size();
142 }
143
144 private:
145
146 ThreadPool(); // Unimplemented
147 ThreadPool(const ThreadPool &); // Unimplemented
148 mike 1.2 ThreadPool & operator=(const ThreadPool &); // Unimplemented
149
150 static ThreadReturnType PEGASUS_THREAD_CDECL _loop(void *);
151
152 static Boolean _timeIntervalExpired(struct timeval *start,
153 struct timeval *interval);
154
155 static void _deleteSemaphore(void *p);
156
157 void _cleanupThread(Thread * thread);
158 Thread *_initializeThread();
159 void _addToIdleThreadsQueue(Thread * th);
160
161 Sint16 _maxThreads;
162 Sint16 _minThreads;
163 AtomicInt _currentThreads;
164 struct timeval _deallocateWait;
165 char _key[17];
166 List < Thread, Mutex > _idleThreads;
167 List < Thread, Mutex > _runningThreads;
168 AtomicInt _dying;
169 mike 1.2 };
170
171 PEGASUS_NAMESPACE_END
172
173 #endif // Pegasus_ThreadPool_h
|