2020-08-15 19:13:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2021-08-07 11:12:35 +00:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2020-08-15 19:13:19 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-15 19:13:19 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-07 11:12:35 +00:00
|
|
|
#include <AK/Singleton.h>
|
2021-07-11 19:02:15 +00:00
|
|
|
#include <Kernel/ProcessGroup.h>
|
2020-08-15 19:13:19 +00:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-07 19:34:11 +00:00
|
|
|
static Singleton<SpinLockProtectedValue<ProcessGroup::List>> s_process_groups;
|
2021-08-07 11:12:35 +00:00
|
|
|
|
|
|
|
SpinLockProtectedValue<ProcessGroup::List>& process_groups()
|
|
|
|
{
|
|
|
|
return *s_process_groups;
|
|
|
|
}
|
2020-08-15 19:13:19 +00:00
|
|
|
|
|
|
|
ProcessGroup::~ProcessGroup()
|
|
|
|
{
|
2021-08-07 11:12:35 +00:00
|
|
|
process_groups().with([&](auto& groups) {
|
|
|
|
groups.remove(*this);
|
|
|
|
});
|
2020-08-15 19:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 09:07:25 +00:00
|
|
|
RefPtr<ProcessGroup> ProcessGroup::create(ProcessGroupID pgid)
|
2020-08-15 19:13:19 +00:00
|
|
|
{
|
2021-06-20 08:21:16 +00:00
|
|
|
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
|
2021-08-07 11:12:35 +00:00
|
|
|
if (!process_group)
|
|
|
|
return {};
|
|
|
|
process_groups().with([&](auto& groups) {
|
|
|
|
groups.prepend(*process_group);
|
|
|
|
});
|
2020-08-15 19:13:19 +00:00
|
|
|
return process_group;
|
|
|
|
}
|
|
|
|
|
2021-05-18 09:07:25 +00:00
|
|
|
RefPtr<ProcessGroup> ProcessGroup::find_or_create(ProcessGroupID pgid)
|
2020-08-15 19:13:19 +00:00
|
|
|
{
|
2021-08-07 11:12:35 +00:00
|
|
|
return process_groups().with([&](auto& groups) -> RefPtr<ProcessGroup> {
|
|
|
|
for (auto& group : groups) {
|
|
|
|
if (group.pgid() == pgid)
|
|
|
|
return &group;
|
|
|
|
}
|
|
|
|
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
|
|
|
|
if (process_group)
|
|
|
|
groups.prepend(*process_group);
|
|
|
|
return process_group;
|
|
|
|
});
|
2020-08-15 19:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
RefPtr<ProcessGroup> ProcessGroup::from_pgid(ProcessGroupID pgid)
|
2020-08-15 19:13:19 +00:00
|
|
|
{
|
2021-08-07 11:12:35 +00:00
|
|
|
return process_groups().with([&](auto& groups) -> RefPtr<ProcessGroup> {
|
|
|
|
for (auto& group : groups) {
|
|
|
|
if (group.pgid() == pgid)
|
|
|
|
return &group;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
2020-08-15 19:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|