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

#ifndef _WIN32
#   define SOCKET int
#endif

namespace IceInternal
{

class BasicStream;

class ThreadPool : public ::IceUtil::Shared, public IceUtil::Monitor<IceUtil::Mutex>
{
public:

    void _register(SOCKET, const EventHandlerPtr&);
    void unregister(SOCKET);
    void promoteFollower();
    void initiateShutdown(); // Signal-safe shutdown initiation.
    void waitUntilFinished();
    void joinWithAllThreads();
    
private:

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

    bool clearInterrupt();
    void setInterrupt(char);

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

    InstancePtr _instance;
    bool _destroyed;
    SOCKET _maxFd;
    SOCKET _minFd;
    SOCKET _lastFd;
    SOCKET _fdIntrRead;
    SOCKET _fdIntrWrite;
    fd_set _fdSet;
    std::list<std::pair<SOCKET, EventHandlerPtr> > _changes; // Event handler set for addition; null for removal.
    std::map<SOCKET, EventHandlerPtr> _handlerMap;
    int _handlers;
    int _timeout;
    ::IceUtil::Mutex _threadMutex;
    bool _multipleThreads;

    class EventHandlerThread : public ::IceUtil::Thread
    {
    public:
	
	EventHandlerThread(const ThreadPoolPtr&);
	virtual void run();

    private:

	ThreadPoolPtr _pool;
    };
    friend class EventHandlerThread;

    std::vector<IceUtil::ThreadControl> _threads; // Control for all threads, running or not.
    int _threadNum; // Number of running threads.
};

}

#endif