(file) Return to ThreadPool.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  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                            @exception IPCException
 89                         */
 90 kumpf          1.3     ThreadStatus allocate_and_awaken(
 91                            void* parm,
 92                            ThreadReturnType(PEGASUS_THREAD_CDECL* work) (void*),
 93                            Semaphore* blocking = 0);
 94 mike           1.2 
 95                        /**
 96                            Cleans up idle threads if they have been running longer than the
 97                            deallocate_wait configuration and more than the configured
 98                            minimum number of threads is running.
 99                            @return The number of threads that were cleaned up.
100                            @exception IPCException
101                         */
102                        Uint32 cleanupIdleThreads();
103                    
104 kumpf          1.3     void get_key(Sint8* buf, int bufsize);
105 mike           1.2 
106                        inline void setMinThreads(Sint16 min)
107                        {
108                            _minThreads = min;
109                        }
110                    
111                        inline Sint16 getMinThreads() const
112                        {
113                            return _minThreads;
114                        }
115                    
116                        inline void setMaxThreads(Sint16 max)
117                        {
118                            _maxThreads = max;
119                        }
120                    
121                        inline Sint16 getMaxThreads() const
122                        {
123                            return _maxThreads;
124                        }
125                    
126 mike           1.2     inline Uint32 runningCount()
127                        {
128 kamal.locahana 1.4         return (Uint32) _runningThreads.size();
129 mike           1.2     }
130                    
131                        inline Uint32 idleCount()
132                        {
133 kamal.locahana 1.4         return (Uint32) _idleThreads.size();
134 mike           1.2     }
135                    
136                    private:
137                    
138                        ThreadPool();               // Unimplemented
139                        ThreadPool(const ThreadPool &);     // Unimplemented
140                        ThreadPool & operator=(const ThreadPool &); // Unimplemented
141                    
142                        static ThreadReturnType PEGASUS_THREAD_CDECL _loop(void *);
143                    
144 kumpf          1.3     static Boolean _timeIntervalExpired(
145                            struct timeval* start,
146                            struct timeval* interval);
147                    
148                        static void _deleteSemaphore(void* p);
149                    
150                        void _cleanupThread(Thread* thread);
151                        Thread* _initializeThread();
152                        void _addToIdleThreadsQueue(Thread* th);
153 mike           1.2 
154                        Sint16 _maxThreads;
155                        Sint16 _minThreads;
156                        AtomicInt _currentThreads;
157                        struct timeval _deallocateWait;
158                        char _key[17];
159 kumpf          1.3     List<Thread, Mutex> _idleThreads;
160                        List<Thread, Mutex> _runningThreads;
161 mike           1.2     AtomicInt _dying;
162                    };
163                    
164                    PEGASUS_NAMESPACE_END
165                    
166                    #endif // Pegasus_ThreadPool_h

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2