2020-08-15 22:13:19 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2021-08-07 14:12:35 +03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2020-08-15 22:13:19 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-15 22:13:19 +03:00
|
|
|
*/
|
|
|
|
|
2021-08-07 14:12:35 +03:00
|
|
|
#include <AK/Singleton.h>
|
2021-07-11 22:02:15 +03:00
|
|
|
#include <Kernel/ProcessGroup.h>
|
2020-08-15 22:13:19 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-22 02:37:17 +03:00
|
|
|
static Singleton<SpinlockProtected<ProcessGroup::List>> s_process_groups;
|
2021-08-07 14:12:35 +03:00
|
|
|
|
2021-08-22 02:37:17 +03:00
|
|
|
SpinlockProtected<ProcessGroup::List>& process_groups()
|
2021-08-07 14:12:35 +03:00
|
|
|
{
|
|
|
|
return *s_process_groups;
|
|
|
|
}
|
2020-08-15 22:13:19 +03:00
|
|
|
|
|
|
|
ProcessGroup::~ProcessGroup()
|
|
|
|
{
|
2021-08-07 14:12:35 +03:00
|
|
|
process_groups().with([&](auto& groups) {
|
|
|
|
groups.remove(*this);
|
|
|
|
});
|
2020-08-15 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<ProcessGroup>> ProcessGroup::try_create(ProcessGroupID pgid)
|
2020-08-15 22:13:19 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
auto process_group = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcessGroup(pgid)));
|
2021-08-07 14:12:35 +03:00
|
|
|
process_groups().with([&](auto& groups) {
|
|
|
|
groups.prepend(*process_group);
|
|
|
|
});
|
2020-08-15 22:13:19 +03:00
|
|
|
return process_group;
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<ProcessGroup>> ProcessGroup::try_find_or_create(ProcessGroupID pgid)
|
2020-08-15 22:13:19 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
return process_groups().with([&](auto& groups) -> ErrorOr<NonnullLockRefPtr<ProcessGroup>> {
|
2021-08-07 14:12:35 +03:00
|
|
|
for (auto& group : groups) {
|
|
|
|
if (group.pgid() == pgid)
|
2021-09-05 22:48:04 +03:00
|
|
|
return group;
|
2021-08-07 14:12:35 +03:00
|
|
|
}
|
2022-08-19 21:53:40 +03:00
|
|
|
auto process_group = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) ProcessGroup(pgid)));
|
2021-09-05 22:48:04 +03:00
|
|
|
groups.prepend(*process_group);
|
2021-08-07 14:12:35 +03:00
|
|
|
return process_group;
|
|
|
|
});
|
2020-08-15 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
LockRefPtr<ProcessGroup> ProcessGroup::from_pgid(ProcessGroupID pgid)
|
2020-08-15 22:13:19 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
return process_groups().with([&](auto& groups) -> LockRefPtr<ProcessGroup> {
|
2021-08-07 14:12:35 +03:00
|
|
|
for (auto& group : groups) {
|
|
|
|
if (group.pgid() == pgid)
|
|
|
|
return &group;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
2020-08-15 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|