summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/ThreadPool.h
blob: 01365b4c9ff2a530beee8e7b7410f54d178ab03b (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
// **********************************************************************
//
// 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 <map>

namespace IceInternal
{

class Stream;

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

    void _register(int, const EventHandlerPtr&);
    void unregister(int);
    void promoteFollower();
    void waitUntilServerFinished();
    void waitUntilFinished();
    void joinWithAllThreads();
    
private:

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

    void clearInterrupt();
    void setInterrupt();

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

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

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

    private:

	ThreadPoolPtr _pool;
    };
    friend class EventHandlerThread;
    std::vector<JTCThreadHandle> _threads;
};

}

#endif