aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/interlock.h
blob: 59254efe166ca5bd44df1e819e9a1f867a642485 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <mutex>

namespace search::attribute {

class InterlockGuard;

/**
 * Class used to serialize getting enum change exclusive lock.  This
 * eliminates the need for defining a locking order when getting enum
 * change shared locks.  Scenario avoided is:
 *
 * Threads T1, T2: Grouping queries
 * Threads T3, T4: Attribute writer threads
 *
 * Thread T1 gets shared lock on A1
 * Thread T2 gets shared lock on A2
 * Theead T3 tries to get exclusive lock on A1
 * Theead T4 tries to get exclusive lock on A2
 * Thread T1 tries to get shared lock on A2
 * Thread T2 tries to get shared lock on A1
 *
 * With the interlock properly used, thread T3 will hold the
 * interlock, preventing thread T4 from registering intent to get
 * write lock on A2, thus thread T1 can get a shared lock on A2 and complete.
 */
class Interlock {
    std::mutex _mutex;
    friend class InterlockGuard;
public:
    Interlock() noexcept
        : _mutex()
    {
    }

    virtual ~Interlock() { }
};

/**
 * Class used to serialize getting enum change exclusive lock.  The guard
 * is passed to EnumModifier constructor to signal that interlock is held.
 */
class InterlockGuard
{
    std::lock_guard<std::mutex> _guard;
public:
    InterlockGuard(Interlock &interlock)
        : _guard(interlock._mutex)
    {
    }

    ~InterlockGuard() { }
};


}