summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/blockwritermutex.hpp
blob: d238fed18bf8dcb94cd570a30541bf0e1b6b0a53 (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
59
60
61
62
63
64
65
66
67
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vector>

#include <boost/noncopyable.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>


namespace vespalib {


/* Blocks a writer from being called while readers are active and vice versa.
 * This is only intended to be used by a single writer.
 */
class BlockWriterMutex : boost::noncopyable {
    typedef boost::mutex ReadersMutex;

    ReadersMutex _readersMutex;
    int _readers;

    boost::condition_variable _noReaders;

    void lockImpl(int sign) {
        boost::unique_lock<ReadersMutex> readersLock(_readersMutex);
        while ((sign*_readers) > 0)
            _noReaders.wait(readersLock);
        _readers -= sign;
    }

    void unlockImpl(int sign) {
        ReadersMutex::scoped_lock readersLock(_readersMutex);
        _readers += sign;
        if (_readers == 0)
            _noReaders.notify_all();
    }

public:
    typedef boost::shared_lock<BlockWriterMutex> ReaderLock;
    typedef boost::unique_lock<BlockWriterMutex> WriterLock;

    BlockWriterMutex()
        :_readers(0)
    {}

    void lock() {
        lockImpl(1);
    }

    void unlock() {
        unlockImpl(1);
    }

    void lock_shared() {
        lockImpl(-1);
    }

    void unlock_shared() {
        unlockImpl(-1);
    }
};

} //namespace vespalib