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

#include "qpid/log/Statement.h"
#include "qpid/sys/Waitable.h"

namespace qpid {
namespace client {

using sys::Waitable;

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

bool Bounds::expand(size_t sizeRequired, bool block) {
    if (!max) return true;
    Waitable::ScopedLock l(lock);
    current += sizeRequired;
    if (block) {
        Waitable::ScopedWait w(lock);
        while (current > max) 
            lock.wait();
    }
    return current <= max;
}

void Bounds::reduce(size_t size) {
    if (!max || size == 0) return;
    Waitable::ScopedLock l(lock);
    if (current == 0) return;
    current -= std::min(size, current);
    if (current < max && lock.hasWaiters()) {
        assert(lock.hasWaiters() == 1);
        lock.notify();
    }
}

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

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

void Bounds::setException(const sys::ExceptionHolder& e) {
    Waitable::ScopedLock l(lock);    
    lock.setException(e);
    lock.waitWaiters();         // Wait for waiting threads to exit.
}

}} // namespace qpid::client