|
@@ -25,9 +25,11 @@ public:
|
|
void queue(void (*function)(void*), void* data = nullptr, void (*free_data)(void*) = nullptr)
|
|
void queue(void (*function)(void*), void* data = nullptr, void (*free_data)(void*) = nullptr)
|
|
{
|
|
{
|
|
auto* item = new WorkItem; // TODO: use a pool
|
|
auto* item = new WorkItem; // TODO: use a pool
|
|
- item->function = function;
|
|
|
|
- item->data = data;
|
|
|
|
- item->free_data = free_data;
|
|
|
|
|
|
+ item->function = [function, data, free_data] {
|
|
|
|
+ function(data);
|
|
|
|
+ if (free_data)
|
|
|
|
+ free_data(data);
|
|
|
|
+ };
|
|
do_queue(item);
|
|
do_queue(item);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -35,31 +37,14 @@ public:
|
|
void queue(Function function)
|
|
void queue(Function function)
|
|
{
|
|
{
|
|
auto* item = new WorkItem; // TODO: use a pool
|
|
auto* item = new WorkItem; // TODO: use a pool
|
|
- item->function = [](void* f) {
|
|
|
|
- (*reinterpret_cast<Function*>(f))();
|
|
|
|
- };
|
|
|
|
- if constexpr (sizeof(Function) <= sizeof(item->inline_data)) {
|
|
|
|
- item->data = new (item->inline_data) Function(move(function));
|
|
|
|
- item->free_data = [](void* f) {
|
|
|
|
- reinterpret_cast<Function*>(f)->~Function();
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
- item->data = new Function(move(function));
|
|
|
|
- item->free_data = [](void* f) {
|
|
|
|
- delete reinterpret_cast<Function*>(f);
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
|
|
+ item->function = Function(function);
|
|
do_queue(item);
|
|
do_queue(item);
|
|
}
|
|
}
|
|
|
|
|
|
private:
|
|
private:
|
|
struct WorkItem {
|
|
struct WorkItem {
|
|
IntrusiveListNode<WorkItem> m_node;
|
|
IntrusiveListNode<WorkItem> m_node;
|
|
- void (*function)(void*);
|
|
|
|
- void* data;
|
|
|
|
- void (*free_data)(void*);
|
|
|
|
- u8 inline_data[4 * sizeof(void*)];
|
|
|
|
|
|
+ Function<void()> function;
|
|
};
|
|
};
|
|
|
|
|
|
void do_queue(WorkItem*);
|
|
void do_queue(WorkItem*);
|