blob: 84ee595ceb4aa339b2660978319f0cb4cb8150d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
// **********************************************************************
//
// 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 <IceUtil/Shared.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/Monitor.h>
#include <IceUtil/Thread.h>
#include <Ice/ThreadPoolF.h>
#include <Ice/InstanceF.h>
#include <Ice/LoggerF.h>
#include <Ice/PropertiesF.h>
#include <Ice/EventHandlerF.h>
#include <list>
#ifdef _WIN32
# include <winsock2.h>
#else
# define SOCKET int
# ifdef __linux
# include <sys/epoll.h>
# else
# include <sys/poll.h>
# endif
#endif
namespace IceInternal
{
class BasicStream;
class ThreadPool : public IceUtil::Shared, public IceUtil::Monitor<IceUtil::Mutex>
{
public:
ThreadPool(const InstancePtr&, const std::string&, int);
virtual ~ThreadPool();
void destroy();
void _register(SOCKET, const EventHandlerPtr&);
void unregister(SOCKET);
void promoteFollower();
void joinWithAllThreads();
std::string prefix() const;
private:
void clearInterrupt();
void setInterrupt();
bool run(); // Returns true if a follower should be promoted.
void read(const EventHandlerPtr&);
InstancePtr _instance;
bool _destroyed;
const std::string _prefix;
SOCKET _maxFd;
SOCKET _minFd;
SOCKET _lastFd;
SOCKET _fdIntrRead;
SOCKET _fdIntrWrite;
#if defined(_WIN32)
fd_set _fdSet;
#elif defined(__linux)
int _epollFd;
std::vector<struct epoll_event> _events;
#else
std::vector<struct pollfd> _pollFdSet;
#endif
std::list<std::pair<SOCKET, EventHandlerPtr> > _changes; // Event handler set for addition; null for removal.
std::map<SOCKET, EventHandlerPtr> _handlerMap;
int _timeout;
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<IceUtil::ThreadPtr> _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
|