summaryrefslogtreecommitdiff
path: root/cpp/src/IceStorm/QueuedProxy.cpp
blob: 473e9b1f351872c377e4d38639ade91dd758476e (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) 2003-2005 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.
//
// **********************************************************************

#include <IceStorm/QueuedProxy.h>

using namespace std;

IceStorm::QueuedProxy::QueuedProxy() :
    _busy(false)
{
}

void
IceStorm::QueuedProxy::publish(const EventPtr& event)
{
    IceUtil::Mutex::Lock sync(_mutex);

    if(_exception.get())
    {
        _exception->ice_throw();
    }

    _events.push_back(event);

    //
    // If another thread is busy delivering events, then we
    // have nothing left to do.
    //
    if(_busy)
    {
        return;
    }

    _busy = true;

    try
    {
        while(!_events.empty())
        {
            //
            // Get the current set of events, but release the lock before
            // attempting to deliver the events. This allows other threads
            // to add events in case we block (such as during connection
            // establishment).
            //
            vector<EventPtr> v;
            v.swap(_events);
            sync.release();

            //
            // Deliver the events without holding the lock.
            //
            for(vector<EventPtr>::iterator p = v.begin(); p != v.end(); ++p)
            {
                deliver(*p);
            }

            //
            // Reacquire the lock before we check the queue again.
            //
            sync.acquire();
        }

        _busy = false;
    }
    catch(const Ice::LocalException& ex)
    {
        assert(!sync.acquired());
        sync.acquire();
        _busy = false;
        _exception.reset(dynamic_cast<Ice::LocalException*>(ex.ice_clone()));
        throw;
    }
}