blob: f86b7c1473a1e60582e7c88e7f8287f7a5e022c6 (
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
52
53
54
55
56
57
58
59
60
|
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Singleton.h>
#include <Kernel/ProcessGroup.h>
namespace Kernel {
static Singleton<SpinlockProtected<ProcessGroup::List, LockRank::None>> s_process_groups;
SpinlockProtected<ProcessGroup::List, LockRank::None>& process_groups()
{
return *s_process_groups;
}
ProcessGroup::~ProcessGroup()
{
process_groups().with([&](auto& groups) {
groups.remove(*this);
});
}
ErrorOr<NonnullLockRefPtr<ProcessGroup>> ProcessGroup::try_create(ProcessGroupID pgid)
{
auto process_group = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcessGroup(pgid)));
process_groups().with([&](auto& groups) {
groups.prepend(*process_group);
});
return process_group;
}
ErrorOr<NonnullLockRefPtr<ProcessGroup>> ProcessGroup::try_find_or_create(ProcessGroupID pgid)
{
return process_groups().with([&](auto& groups) -> ErrorOr<NonnullLockRefPtr<ProcessGroup>> {
for (auto& group : groups) {
if (group.pgid() == pgid)
return group;
}
auto process_group = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcessGroup(pgid)));
groups.prepend(*process_group);
return process_group;
});
}
LockRefPtr<ProcessGroup> ProcessGroup::from_pgid(ProcessGroupID pgid)
{
return process_groups().with([&](auto& groups) -> LockRefPtr<ProcessGroup> {
for (auto& group : groups) {
if (group.pgid() == pgid)
return &group;
}
return nullptr;
});
}
}
|