mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
a6a439243f
This step would ideally not have been necessary (increases amount of refactoring and templates necessary, which in turn increases build times), but it gives us a couple of nice properties: - SpinlockProtected inside Singleton (a very common combination) can now obtain any lock rank just via the template parameter. It was not previously possible to do this with SingletonInstanceCreator magic. - SpinlockProtected's lock rank is now mandatory; this is the majority of cases and allows us to see where we're still missing proper ranks. - The type already informs us what lock rank a lock has, which aids code readability and (possibly, if gdb cooperates) lock mismatch debugging. - The rank of a lock can no longer be dynamic, which is not something we wanted in the first place (or made use of). Locks randomly changing their rank sounds like a disaster waiting to happen. - In some places, we might be able to statically check that locks are taken in the right order (with the right lock rank checking implementation) as rank information is fully statically known. This refactoring even more exposes the fact that Mutex has no lock rank capabilites, which is not fixed here.
69 lines
1.6 KiB
C++
69 lines
1.6 KiB
C++
/*
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Error.h>
|
|
#include <AK/IntrusiveList.h>
|
|
#include <Kernel/Forward.h>
|
|
#include <Kernel/Locking/SpinlockProtected.h>
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
namespace Kernel {
|
|
|
|
extern WorkQueue* g_io_work;
|
|
extern WorkQueue* g_ata_work;
|
|
|
|
class WorkQueue {
|
|
AK_MAKE_NONCOPYABLE(WorkQueue);
|
|
AK_MAKE_NONMOVABLE(WorkQueue);
|
|
|
|
public:
|
|
static void initialize();
|
|
|
|
ErrorOr<void> try_queue(void (*function)(void*), void* data = nullptr, void (*free_data)(void*) = nullptr)
|
|
{
|
|
auto item = new (nothrow) WorkItem; // TODO: use a pool
|
|
if (!item)
|
|
return Error::from_errno(ENOMEM);
|
|
item->function = [function, data, free_data] {
|
|
function(data);
|
|
if (free_data)
|
|
free_data(data);
|
|
};
|
|
do_queue(*item);
|
|
return {};
|
|
}
|
|
|
|
template<typename Function>
|
|
ErrorOr<void> try_queue(Function function)
|
|
{
|
|
auto item = new (nothrow) WorkItem; // TODO: use a pool
|
|
if (!item)
|
|
return Error::from_errno(ENOMEM);
|
|
item->function = Function(function);
|
|
do_queue(*item);
|
|
return {};
|
|
}
|
|
|
|
private:
|
|
explicit WorkQueue(StringView);
|
|
|
|
struct WorkItem {
|
|
public:
|
|
IntrusiveListNode<WorkItem> m_node;
|
|
Function<void()> function;
|
|
};
|
|
|
|
void do_queue(WorkItem&);
|
|
|
|
LockRefPtr<Thread> m_thread;
|
|
WaitQueue m_wait_queue;
|
|
SpinlockProtected<IntrusiveList<&WorkItem::m_node>, LockRank::None> m_items {};
|
|
};
|
|
|
|
}
|