Skip to content

Commit

Permalink
DispatchQueue improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 16, 2024
1 parent 5f76374 commit 74d7068
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
9 changes: 8 additions & 1 deletion Submodules/UIKit/include/DispatchQueue.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <Timer.h>
#include <queue>
#include <string>
#include <functional>
Expand All @@ -12,6 +13,11 @@

namespace NXKit {

struct DispatchQueueTask {
std::function<void()> func;
Timer delay;
};

class DispatchQueue {
public:
DispatchQueue(const std::string& tag);
Expand All @@ -22,12 +28,13 @@ class DispatchQueue {

std::string tag() const { return _tag; }
void async(const std::function<void()>& task);
void asyncAfter(double seconds, const std::function<void()>& task);

bool isActive() { return _task_loop_active; }
private:
static std::shared_ptr<DispatchQueue> _main;
static std::shared_ptr<DispatchQueue> _global;
std::queue<std::function<void()>> _queue;
std::queue<DispatchQueueTask> _queue;
std::string _tag;

pthread_t _task_loop_thread = nullptr;
Expand Down
1 change: 1 addition & 0 deletions Submodules/UIKit/include/UIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <DispatchQueue.h>
#include <UIBlurView.h>
#include <UIImageView.h>
#include <UILabel.h>
Expand Down
20 changes: 16 additions & 4 deletions Submodules/UIKit/lib/DispatchQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ DispatchQueue::~DispatchQueue() {

void DispatchQueue::async(const std::function<void()>& task) {
std::lock_guard<std::mutex> guard(_m_async_mutex);
_queue.push(task);
_queue.push({ task });
}

void DispatchQueue::asyncAfter(double seconds, const std::function<void()>& task) {
std::lock_guard<std::mutex> guard(_m_async_mutex);
_queue.push({ task, Timer(seconds * 1000) });
}

void DispatchQueue::performAll() {
Expand All @@ -51,16 +56,23 @@ void DispatchQueue::performAll() {
link->func();
}

std::queue<std::function<void()>> copy;
std::queue<DispatchQueueTask> copy;
{
std::lock_guard<std::mutex> guard(_m_async_mutex);
std::swap( _queue, copy );
}


Timer now;
while (!copy.empty()) {
auto task = copy.front();
copy.pop();
task();

// If task is delayed, put it back into queue
if (now - task.delay < 0) {
_queue.push(task);
} else {
task.func();
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions app/Screens/TestViewController/TestViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ void animateLabel(std::shared_ptr<UILabel> label) {
// });
}

void switchLabelText(std::shared_ptr<UILabel> label) {
label->setText("Text number 1");
label->sizeToFit();
DispatchQueue::main()->asyncAfter(2, [label]() {
label->setText("Text number 2\nHere is more text to test sizeToFit");
label->sizeToFit();
DispatchQueue::main()->asyncAfter(2, [label]() {
switchLabelText(label);
});
});
}

TestViewController::TestViewController() {

}
Expand Down Expand Up @@ -110,6 +122,8 @@ void TestViewController::loadView() {
label2->setFrame({ 100, 200, 0, 0 });
rootView->addSubview(label2);

switchLabelText(label2);

setView(rootView);
}

Expand Down

0 comments on commit 74d7068

Please sign in to comment.