mirror of
https://github.com/RGBCube/serenity
synced 2025-05-15 17:04:59 +00:00
Kernel: Avoid allocating under spinlock in ProcessGroup::find_or_create
Avoid allocating while holding the g_process_groups_lock spinlock, it's a pattern that has a negative effect on performance and scalability, especially given that it is a global lock, reachable by all processes.
This commit is contained in:
parent
bb91bed576
commit
e95eb7a51d
2 changed files with 17 additions and 2 deletions
|
@ -30,18 +30,31 @@ RefPtr<ProcessGroup> ProcessGroup::create(ProcessGroupID pgid)
|
||||||
|
|
||||||
RefPtr<ProcessGroup> ProcessGroup::find_or_create(ProcessGroupID pgid)
|
RefPtr<ProcessGroup> ProcessGroup::find_or_create(ProcessGroupID pgid)
|
||||||
{
|
{
|
||||||
|
// Avoid allocating under spinlock, this compromises by wasting the
|
||||||
|
// allocation when we race with another process to see if they have
|
||||||
|
// already created a process group.
|
||||||
|
auto process_group = adopt_ref_if_nonnull(new ProcessGroup(pgid));
|
||||||
ScopedSpinLock lock(g_process_groups_lock);
|
ScopedSpinLock lock(g_process_groups_lock);
|
||||||
|
|
||||||
if (auto existing = from_pgid(pgid))
|
if (auto existing = from_pgid_nolock(pgid))
|
||||||
return existing.release_nonnull();
|
return existing.release_nonnull();
|
||||||
|
|
||||||
return create(pgid);
|
if (!process_group)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
g_process_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);
|
ScopedSpinLock lock(g_process_groups_lock);
|
||||||
|
return from_pgid_nolock(pgid);
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPtr<ProcessGroup> ProcessGroup::from_pgid_nolock(ProcessGroupID pgid)
|
||||||
|
{
|
||||||
|
VERIFY(g_process_groups_lock.own_lock());
|
||||||
for (auto& group : *g_process_groups) {
|
for (auto& group : *g_process_groups) {
|
||||||
if (group.pgid() == pgid)
|
if (group.pgid() == pgid)
|
||||||
return &group;
|
return &group;
|
||||||
|
|
|
@ -40,6 +40,8 @@ private:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static RefPtr<ProcessGroup> from_pgid_nolock(ProcessGroupID);
|
||||||
|
|
||||||
ProcessGroup* m_prev { nullptr };
|
ProcessGroup* m_prev { nullptr };
|
||||||
ProcessGroup* m_next { nullptr };
|
ProcessGroup* m_next { nullptr };
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue