summaryrefslogtreecommitdiff
path: root/Kernel/ScopedCritical.cpp
blob: c97478c6d54091b5b0b8b84a14f5ed0fa5f02498 (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
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/ScopedCritical.h>

#include <Kernel/Arch/Processor.h>

namespace Kernel {

ScopedCritical::ScopedCritical()
{
    enter();
}

ScopedCritical::~ScopedCritical()
{
    if (m_valid)
        leave();
}

ScopedCritical::ScopedCritical(ScopedCritical&& from)
    : m_valid(exchange(from.m_valid, false))
{
}

ScopedCritical& ScopedCritical::operator=(ScopedCritical&& from)
{
    if (&from != this) {
        m_valid = exchange(from.m_valid, false);
    }
    return *this;
}

void ScopedCritical::leave()
{
    VERIFY(m_valid);
    m_valid = false;
    Processor::leave_critical();
}

void ScopedCritical::enter()
{
    VERIFY(!m_valid);
    m_valid = true;
    Processor::enter_critical();
}

}