// ********************************************************************** // // Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. // // ********************************************************************** #ifndef ICE_THREAD_POOL_H #define ICE_THREAD_POOL_H #include #include #include #include #include #include #include #include #include #include #include #include namespace IceInternal { class BasicStream; class ThreadPool : public IceUtil::Shared, public IceUtil::Monitor { public: ThreadPool(const InstancePtr&, const std::string&, int); virtual ~ThreadPool(); void destroy(); void incFdsInUse(); void decFdsInUse(); void _register(SOCKET, const EventHandlerPtr&); void unregister(SOCKET); void execute(const ThreadPoolWorkItemPtr&); void promoteFollower(); void joinWithAllThreads(); std::string prefix() const; private: bool run(); // Returns true if a follower should be promoted. bool read(const EventHandlerPtr&); InstancePtr _instance; bool _destroyed; const std::string _prefix; Selector _selector; std::list > _changes; // Event handler set for addition; null for removal. std::list _workItems; std::map _handlerMap; class EventHandlerThread : public IceUtil::Thread { public: EventHandlerThread(const ThreadPoolPtr&); virtual void run(); private: ThreadPoolPtr _pool; }; friend class EventHandlerThread; const int _size; // Number of threads that are pre-created. const int _sizeMax; // Maximum number of threads. const int _sizeWarn; // If _inUse reaches _sizeWarn, a "low on threads" warning will be printed. const size_t _stackSize; std::vector _threads; // All threads, running or not. int _running; // Number of running threads. int _inUse; // Number of threads that are currently in use. double _load; // Current load in number of threads. bool _promote; const bool _warnUdp; }; } #endif