Skip to content

Commit d6667e4

Browse files
committed
Kernel: Port process groups to SpinLockProtectedValue
1 parent 6762378 commit d6667e4

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

Kernel/Process.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ UNMAP_AFTER_INIT void Process::initialize()
7676
g_modules = new HashMap<String, OwnPtr<Module>>;
7777

7878
next_pid.store(0, AK::MemoryOrder::memory_order_release);
79-
g_process_groups = new ProcessGroup::List();
8079

8180
hostname().with_exclusive([&](auto& name) {
8281
name = "courage";

Kernel/ProcessGroup.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,63 @@
11
/*
22
* Copyright (c) 2020, the SerenityOS developers.
3+
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
34
*
45
* SPDX-License-Identifier: BSD-2-Clause
56
*/
67

8+
#include <AK/Singleton.h>
79
#include <Kernel/ProcessGroup.h>
810

911
namespace Kernel {
1012

11-
RecursiveSpinLock g_process_groups_lock;
12-
ProcessGroup::List* g_process_groups;
13+
static AK::Singleton<SpinLockProtectedValue<ProcessGroup::List>> s_process_groups;
14+
15+
SpinLockProtectedValue<ProcessGroup::List>& process_groups()
16+
{
17+
return *s_process_groups;
18+
}
1319

1420
ProcessGroup::~ProcessGroup()
1521
{
16-
ScopedSpinLock lock(g_process_groups_lock);
17-
g_process_groups->remove(*this);
22+
process_groups().with([&](auto& groups) {
23+
groups.remove(*this);
24+
});
1825
}
1926

2027
RefPtr<ProcessGroup> ProcessGroup::create(ProcessGroupID pgid)
2128
{
2229
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
23-
if (process_group) {
24-
ScopedSpinLock lock(g_process_groups_lock);
25-
g_process_groups->prepend(*process_group);
26-
}
27-
30+
if (!process_group)
31+
return {};
32+
process_groups().with([&](auto& groups) {
33+
groups.prepend(*process_group);
34+
});
2835
return process_group;
2936
}
3037

3138
RefPtr<ProcessGroup> ProcessGroup::find_or_create(ProcessGroupID pgid)
3239
{
33-
ScopedSpinLock lock(g_process_groups_lock);
34-
35-
if (auto existing = from_pgid(pgid))
36-
return existing.release_nonnull();
37-
38-
return create(pgid);
40+
return process_groups().with([&](auto& groups) -> RefPtr<ProcessGroup> {
41+
for (auto& group : groups) {
42+
if (group.pgid() == pgid)
43+
return &group;
44+
}
45+
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
46+
if (process_group)
47+
groups.prepend(*process_group);
48+
return process_group;
49+
});
3950
}
4051

4152
RefPtr<ProcessGroup> ProcessGroup::from_pgid(ProcessGroupID pgid)
4253
{
43-
ScopedSpinLock lock(g_process_groups_lock);
44-
45-
for (auto& group : *g_process_groups) {
46-
if (group.pgid() == pgid)
47-
return &group;
48-
}
49-
return nullptr;
54+
return process_groups().with([&](auto& groups) -> RefPtr<ProcessGroup> {
55+
for (auto& group : groups) {
56+
if (group.pgid() == pgid)
57+
return &group;
58+
}
59+
return nullptr;
60+
});
5061
}
5162

5263
}

Kernel/ProcessGroup.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <AK/IntrusiveList.h>
1010
#include <AK/RefCounted.h>
1111
#include <AK/Weakable.h>
12-
#include <Kernel/Locking/SpinLock.h>
12+
#include <Kernel/Locking/SpinLockProtectedValue.h>
1313
#include <Kernel/UnixTypes.h>
1414

1515
namespace Kernel {
@@ -43,7 +43,6 @@ class ProcessGroup
4343
using List = IntrusiveList<ProcessGroup, RawPtr<ProcessGroup>, &ProcessGroup::m_list_node>;
4444
};
4545

46-
extern ProcessGroup::List* g_process_groups;
47-
extern RecursiveSpinLock g_process_groups_lock;
46+
SpinLockProtectedValue<ProcessGroup::List>& process_groups();
4847

4948
}

0 commit comments

Comments
 (0)