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