mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 22:48:11 +00:00
Kernel: Port process groups to SpinLockProtectedValue
This commit is contained in:
parent
6762378f89
commit
d6667e4cb8
3 changed files with 35 additions and 26 deletions
|
@ -76,7 +76,6 @@ UNMAP_AFTER_INIT void Process::initialize()
|
||||||
g_modules = new HashMap<String, OwnPtr<Module>>;
|
g_modules = new HashMap<String, OwnPtr<Module>>;
|
||||||
|
|
||||||
next_pid.store(0, AK::MemoryOrder::memory_order_release);
|
next_pid.store(0, AK::MemoryOrder::memory_order_release);
|
||||||
g_process_groups = new ProcessGroup::List();
|
|
||||||
|
|
||||||
hostname().with_exclusive([&](auto& name) {
|
hostname().with_exclusive([&](auto& name) {
|
||||||
name = "courage";
|
name = "courage";
|
||||||
|
|
|
@ -1,52 +1,63 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, the SerenityOS developers.
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Singleton.h>
|
||||||
#include <Kernel/ProcessGroup.h>
|
#include <Kernel/ProcessGroup.h>
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
RecursiveSpinLock g_process_groups_lock;
|
static AK::Singleton<SpinLockProtectedValue<ProcessGroup::List>> s_process_groups;
|
||||||
ProcessGroup::List* g_process_groups;
|
|
||||||
|
SpinLockProtectedValue<ProcessGroup::List>& process_groups()
|
||||||
|
{
|
||||||
|
return *s_process_groups;
|
||||||
|
}
|
||||||
|
|
||||||
ProcessGroup::~ProcessGroup()
|
ProcessGroup::~ProcessGroup()
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(g_process_groups_lock);
|
process_groups().with([&](auto& groups) {
|
||||||
g_process_groups->remove(*this);
|
groups.remove(*this);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<ProcessGroup> ProcessGroup::create(ProcessGroupID pgid)
|
RefPtr<ProcessGroup> ProcessGroup::create(ProcessGroupID pgid)
|
||||||
{
|
{
|
||||||
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
|
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
|
||||||
if (process_group) {
|
if (!process_group)
|
||||||
ScopedSpinLock lock(g_process_groups_lock);
|
return {};
|
||||||
g_process_groups->prepend(*process_group);
|
process_groups().with([&](auto& groups) {
|
||||||
}
|
groups.prepend(*process_group);
|
||||||
|
});
|
||||||
return process_group;
|
return process_group;
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<ProcessGroup> ProcessGroup::find_or_create(ProcessGroupID pgid)
|
RefPtr<ProcessGroup> ProcessGroup::find_or_create(ProcessGroupID pgid)
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(g_process_groups_lock);
|
return process_groups().with([&](auto& groups) -> RefPtr<ProcessGroup> {
|
||||||
|
for (auto& group : groups) {
|
||||||
if (auto existing = from_pgid(pgid))
|
if (group.pgid() == pgid)
|
||||||
return existing.release_nonnull();
|
return &group;
|
||||||
|
}
|
||||||
return create(pgid);
|
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
|
||||||
|
if (process_group)
|
||||||
|
groups.prepend(*process_group);
|
||||||
|
return process_group;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<ProcessGroup> ProcessGroup::from_pgid(ProcessGroupID pgid)
|
RefPtr<ProcessGroup> ProcessGroup::from_pgid(ProcessGroupID pgid)
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(g_process_groups_lock);
|
return process_groups().with([&](auto& groups) -> RefPtr<ProcessGroup> {
|
||||||
|
for (auto& group : groups) {
|
||||||
for (auto& group : *g_process_groups) {
|
if (group.pgid() == pgid)
|
||||||
if (group.pgid() == pgid)
|
return &group;
|
||||||
return &group;
|
}
|
||||||
}
|
return nullptr;
|
||||||
return nullptr;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include <AK/IntrusiveList.h>
|
#include <AK/IntrusiveList.h>
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
#include <AK/Weakable.h>
|
#include <AK/Weakable.h>
|
||||||
#include <Kernel/Locking/SpinLock.h>
|
#include <Kernel/Locking/SpinLockProtectedValue.h>
|
||||||
#include <Kernel/UnixTypes.h>
|
#include <Kernel/UnixTypes.h>
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
@ -43,7 +43,6 @@ public:
|
||||||
using List = IntrusiveList<ProcessGroup, RawPtr<ProcessGroup>, &ProcessGroup::m_list_node>;
|
using List = IntrusiveList<ProcessGroup, RawPtr<ProcessGroup>, &ProcessGroup::m_list_node>;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ProcessGroup::List* g_process_groups;
|
SpinLockProtectedValue<ProcessGroup::List>& process_groups();
|
||||||
extern RecursiveSpinLock g_process_groups_lock;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue