summaryrefslogtreecommitdiff
path: root/cpp/src/IceStorm/SubscriberPool.h
blob: 4e1354602f410ca6372275056b2e3f57de4caa71 (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
// **********************************************************************
//
// Copyright (c) 2003-2008 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 SUBSCRIBER_POOL_H
#define SUBSCRIBER_POOL_H

#include <IceStorm/Subscriber.h>

#include <IceUtil/Mutex.h>
#include <IceUtil/Monitor.h>
#include <IceUtil/Time.h>
#include <IceUtil/Thread.h>
#include <Ice/Identity.h>
#include <list>
#include <set>

namespace IceStorm
{

//
// Forward declarations.
//
class TraceLevels;
typedef IceUtil::Handle<TraceLevels> TraceLevelsPtr;

class Instance;
typedef IceUtil::Handle<Instance> InstancePtr;

class SubscriberPool;
typedef IceUtil::Handle<SubscriberPool> SubscriberPoolPtr;

class SubscriberPoolMonitor : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>
{
public:

    SubscriberPoolMonitor(const SubscriberPoolPtr&, const IceUtil::Time&);
    ~SubscriberPoolMonitor();

    virtual void run();

    void startMonitor();
    void stopMonitor();
    void destroy();

private:

    const SubscriberPoolPtr _manager;
    const IceUtil::Time _timeout;
    bool _needCheck;
    bool _destroyed;
};

typedef IceUtil::Handle<SubscriberPoolMonitor> SubscriberPoolMonitorPtr;

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

    SubscriberPool(const InstancePtr&);
    ~SubscriberPool();

    void flush(std::list<SubscriberPtr>&);
    void flush(const SubscriberPtr&);
    void add(const SubscriberPtr&);
    void remove(const SubscriberPtr&);
    void destroy();

    //
    // For use by the subscriber worker.
    //
    void dequeue(SubscriberPtr&, bool, const IceUtil::Time&, bool&);
    //
    // For use by the monitor.
    //
    void check();

private:

    bool invariants();
    
    const TraceLevelsPtr _traceLevels;
    const unsigned int _sizeMax;
    const unsigned int _sizeWarn;
    const unsigned int _size;
    const IceUtil::Time _timeout;
    const IceUtil::Time _stallCheck;
    SubscriberPoolMonitorPtr _subscriberPoolMonitor;

    std::list<SubscriberPtr> _pending;
    std::list<SubscriberPtr> _subscribers;
    bool _destroyed;
    std::list<IceUtil::ThreadPtr> _workers;

    int _reap;
    unsigned int _inUse;

    IceUtil::Time _lastStallCheck;
    IceUtil::Time _lastDequeue;
};

} // End namespace IceStorm

#endif