summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/ThreadPool.h
blob: 2c90624a865b14466262ba7834106d357dbf0353 (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
// **********************************************************************
//
// Copyright (c) 2002
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#ifndef ICE_THREAD_POOL_H
#define ICE_THREAD_POOL_H

#include <IceUtil/Shared.h>
#include <Ice/ThreadPoolF.h>
#include <Ice/InstanceF.h>
#include <Ice/EventHandlerF.h>
#include <list>

#ifndef WIN32
#   define SOCKET int
#endif

namespace IceInternal
{

class BasicStream;

class ThreadPool : public ::IceUtil::Shared, public JTCMonitorT<JTCMutex>
{
public:

    void _register(SOCKET, const EventHandlerPtr&);
    void unregister(SOCKET, bool);
    void promoteFollower();
    void initiateServerShutdown(); // Signal-safe shutdown initiation.
    void waitUntilServerFinished();
    void waitUntilFinished();
    void joinWithAllThreads();
    void setMaxConnections(int);
    int getMaxConnections();
    
private:

    ThreadPool(const InstancePtr&);
    virtual ~ThreadPool();
    void destroy();
    friend class Instance;

    bool clearInterrupt();
    void setInterrupt();

    void run();
    void read(const EventHandlerPtr&);

    InstancePtr _instance;
    bool _destroyed;
    SOCKET _maxFd;
    SOCKET _minFd;
    SOCKET _lastFd;
    SOCKET _fdIntrRead;
    SOCKET _fdIntrWrite;
    fd_set _fdSet;
    std::vector<std::pair<SOCKET, EventHandlerPtr> > _adds;
    std::vector<std::pair<SOCKET, bool> > _removes;
    std::map<SOCKET, EventHandlerPtr> _handlerMap;
    int _servers;
    int _timeout;
    JTCMutex _threadMutex;

    class EventHandlerThread : public JTCThread
    {
    public:
	
	EventHandlerThread(const ThreadPoolPtr& pool) : _pool(pool) { }
	virtual void run();

    private:

	ThreadPoolPtr _pool;
    };
    friend class EventHandlerThread;
    std::vector<JTCThreadHandle> _threads; // Handles for all threads, running or not.
    int _threadNum; // Number of running threads.
    int _maxConnections; // Maximum number of connections. If set to zero, the number of connections is not limited.
};

}

#endif