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-21 23:37:17 +00:00
|
|
|
static Singleton<SpinlockProtected<ProcessGroup::List>> s_process_groups;
|
2021-08-07 11:12:35 +00:00
|
|
|
|
2021-08-21 23:37:17 +00:00
|
|
|
SpinlockProtected<ProcessGroup::List>& process_groups()
|
2021-08-07 11:12:35 +00:00
|
|
|
{
|
|
|
|
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-11-07 23:51:39 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ProcessGroup>> ProcessGroup::try_create(ProcessGroupID pgid)
|
2020-08-15 19:13:19 +00:00
|
|
|
{
|
2021-09-05 19:48:04 +00:00
|
|
|
auto process_group = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessGroup(pgid)));
|
2021-08-07 11:12:35 +00:00
|
|
|
process_groups().with([&](auto& groups) {
|
|
|
|
groups.prepend(*process_group);
|
|
|
|
});
|
2020-08-15 19:13:19 +00:00
|
|
|
return process_group;
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<NonnullRefPtr<ProcessGroup>> ProcessGroup::try_find_or_create(ProcessGroupID pgid)
|
2020-08-15 19:13:19 +00:00
|
|
|
{
|
2021-11-07 23:51:39 +00:00
|
|
|
return process_groups().with([&](auto& groups) -> ErrorOr<NonnullRefPtr<ProcessGroup>> {
|
2021-08-07 11:12:35 +00:00
|
|
|
for (auto& group : groups) {
|
|
|
|
if (group.pgid() == pgid)
|
2021-09-05 19:48:04 +00:00
|
|
|
return group;
|
2021-08-07 11:12:35 +00:00
|
|
|
}
|
2021-09-05 19:48:04 +00:00
|
|
|
auto process_group = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessGroup(pgid)));
|
|
|
|
groups.prepend(*process_group);
|
2021-08-07 11:12:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|