summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_event.cpp
blob: d92b491f8b6d73722cc00930aab716d2273eecda (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
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/k_resource_limit.h"

namespace Kernel {

KEvent::KEvent(KernelCore& kernel)
    : KAutoObjectWithSlabHeapAndContainer{kernel}, m_readable_event{kernel} {}

KEvent::~KEvent() = default;

void KEvent::Initialize(KProcess* owner) {
    // Create our readable event.
    KAutoObject::Create(std::addressof(m_readable_event));

    // Initialize our readable event.
    m_readable_event.Initialize(this);

    // Set our owner process.
    // HACK: this should never be nullptr, but service threads don't have a
    // proper parent process yet.
    if (owner != nullptr) {
        m_owner = owner;
        m_owner->Open();
    }

    // Mark initialized.
    m_initialized = true;
}

void KEvent::Finalize() {
    KAutoObjectWithSlabHeapAndContainer<KEvent, KAutoObjectWithList>::Finalize();
}

Result KEvent::Signal() {
    KScopedSchedulerLock sl{m_kernel};

    R_SUCCEED_IF(m_readable_event_destroyed);

    return m_readable_event.Signal();
}

Result KEvent::Clear() {
    KScopedSchedulerLock sl{m_kernel};

    R_SUCCEED_IF(m_readable_event_destroyed);

    return m_readable_event.Clear();
}

void KEvent::PostDestroy(uintptr_t arg) {
    // Release the event count resource the owner process holds.
    KProcess* owner = reinterpret_cast<KProcess*>(arg);

    if (owner != nullptr) {
        owner->GetResourceLimit()->Release(LimitableResource::EventCountMax, 1);
        owner->Close();
    }
}

} // namespace Kernel