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