aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/monitored_refcount.h
blob: 460afec3ba3d9b26372b528012683a694a8e9d32 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <mutex>
#include <condition_variable>

namespace vespalib {

class RetainGuard;
/*
 * Class containing a reference count that can be waited on to become zero.
 * Typically ancestor or member of a class that has to be careful of when
 * portions object can be properly torn down before destruction itself.
 */
class MonitoredRefCount
{
    std::mutex              _lock;
    std::condition_variable _cv;
    uint32_t                _refCount;
    void retain() noexcept;
    void release() noexcept;
    friend RetainGuard;
public:
    MonitoredRefCount();
    virtual ~MonitoredRefCount();
    void waitForZeroRefCount();
    bool has_zero_ref_count();
};

}