summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/client/Bounds.cpp
blob: 1df21db941bbdf11b44aa059e2cf1add0d25e418 (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
#include "Bounds.h"

#include "qpid/log/Statement.h"

namespace qpid {
namespace client {

using sys::Monitor;

Bounds::Bounds(size_t maxSize) : max(maxSize), current(0) {}

bool Bounds::expand(size_t sizeRequired, bool block) 
{
    if (max) {
        Monitor::ScopedLock l(lock);
        current += sizeRequired;
        if (block) {
            while (current > max) {
                QPID_LOG(debug, "Waiting for bounds: " << *this); 
                lock.wait();
            }
            QPID_LOG(debug, "Bounds ok: " << *this);
        }
        return current <= max;
    } else {
        return true;
    }
}

void Bounds::reduce(size_t size)
{
    if (!max || size == 0) return;
    Monitor::ScopedLock l(lock);
    if (current == 0) return;
    bool needNotify = current > max;
    current -= std::min(size, current);
    if (needNotify && current < max) {
        //todo: notify one at a time, but ensure that all threads are
        //eventually notified
        lock.notifyAll();
    }
}

size_t Bounds::getCurrentSize()
{
    Monitor::ScopedLock l(lock);
    return current;
}

std::ostream& operator<<(std::ostream& out, const Bounds& bounds) {
    out << "current=" << bounds.current << ", max=" << bounds.max << " [" << &bounds << "]";
    return out;
}

}} // namespace qpid::client