aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/latch.h
blob: a34d42c7907cb1636d402eb58e523e64dcbe6394 (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
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
// 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 {

/**
 * A latch acts like a blocking queue where the maximum capacity is a
 * single element. It enables directional exchange of data where reads
 * and writes are alternating.
 **/
template <typename T>
class Latch {
private:
    std::mutex              _lock;
    std::condition_variable _cond;
    char                    _space[sizeof(T)];
    bool                    _has_value;

    void *as_void() { return &_space[0]; }
    T *as_value() { return (T*)as_void(); }
public:
    Latch() : _lock(), _cond(), _space(), _has_value(false) {}
    ~Latch() {
        if (_has_value) {
            as_value()->~T();
        }
    }
    bool has_value() {
        std::lock_guard guard(_lock);
        return _has_value;
    }
    T read() {
        std::unique_lock guard(_lock);
        while (!_has_value) {
            _cond.wait(guard);
        }
        T value = std::move(*as_value());
        as_value()->~T();
        _has_value = false;
        _cond.notify_all();
        return value;
    }
    void write(T value) {
        std::unique_lock guard(_lock);
        while (_has_value) {
            _cond.wait(guard);
        }
        new (as_void()) T(std::move(value));
        _has_value = true;
        _cond.notify_all();
    }
};

} // namespace vespalib