2020-08-15 22:13:19 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-15 22:13:19 +03:00
|
|
|
*/
|
|
|
|
|
2021-07-11 22:02:15 +03:00
|
|
|
#include <Kernel/ProcessGroup.h>
|
2020-08-15 22:13:19 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
RecursiveSpinLock g_process_groups_lock;
|
2021-06-03 13:21:04 +03:00
|
|
|
ProcessGroup::List* g_process_groups;
|
2020-08-15 22:13:19 +03:00
|
|
|
|
|
|
|
ProcessGroup::~ProcessGroup()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(g_process_groups_lock);
|
2021-06-03 13:21:04 +03:00
|
|
|
g_process_groups->remove(*this);
|
2020-08-15 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2021-05-18 12:07:25 +03:00
|
|
|
RefPtr<ProcessGroup> ProcessGroup::create(ProcessGroupID pgid)
|
2020-08-15 22:13:19 +03:00
|
|
|
{
|
2021-06-20 11:21:16 +03:00
|
|
|
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
|
2021-05-18 12:07:25 +03:00
|
|
|
if (process_group) {
|
2020-08-15 22:13:19 +03:00
|
|
|
ScopedSpinLock lock(g_process_groups_lock);
|
2021-06-03 13:21:04 +03:00
|
|
|
g_process_groups->prepend(*process_group);
|
2020-08-15 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return process_group;
|
|
|
|
}
|
|
|
|
|
2021-05-18 12:07:25 +03:00
|
|
|
RefPtr<ProcessGroup> ProcessGroup::find_or_create(ProcessGroupID pgid)
|
2020-08-15 22:13:19 +03:00
|
|
|
{
|
2020-11-30 02:05:27 +03:00
|
|
|
ScopedSpinLock lock(g_process_groups_lock);
|
|
|
|
|
2021-05-21 12:27:20 +03:00
|
|
|
if (auto existing = from_pgid(pgid))
|
2020-11-30 02:05:27 +03:00
|
|
|
return existing.release_nonnull();
|
2020-08-15 22:13:19 +03:00
|
|
|
|
2021-05-21 12:27:20 +03:00
|
|
|
return create(pgid);
|
2020-08-15 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
RefPtr<ProcessGroup> ProcessGroup::from_pgid(ProcessGroupID pgid)
|
2020-08-15 22:13:19 +03:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(g_process_groups_lock);
|
|
|
|
|
|
|
|
for (auto& group : *g_process_groups) {
|
|
|
|
if (group.pgid() == pgid)
|
|
|
|
return &group;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|