summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_event.cpp
blob: e52fafbc7c5ee768f352aaf49515ce06fe6420f7 (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
// 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_}, readable_event{kernel_}, writable_event{
                                                                                 kernel_} {}

KEvent::~KEvent() = default;

void KEvent::Initialize(std::string&& name_, KProcess* owner_) {
    // Increment reference count.
    // Because reference count is one on creation, this will result
    // in a reference count of two. Thus, when both readable and
    // writable events are closed this object will be destroyed.
    Open();

    // Create our sub events.
    KAutoObject::Create(std::addressof(readable_event));
    KAutoObject::Create(std::addressof(writable_event));

    // Initialize our sub sessions.
    readable_event.Initialize(this, name_ + ":Readable");
    writable_event.Initialize(this, name_ + ":Writable");

    // Set our owner process.
    owner = owner_;
    owner->Open();

    // Mark initialized.
    name = std::move(name_);
    initialized = true;
}

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

void KEvent::PostDestroy(uintptr_t arg) {
    // Release the event count resource the owner process holds.
    KProcess* owner = reinterpret_cast<KProcess*>(arg);
    owner->GetResourceLimit()->Release(LimitableResource::Events, 1);
    owner->Close();
}

} // namespace Kernel