-
Notifications
You must be signed in to change notification settings - Fork 12
MacOS: Add support for timers to the platform integrations #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
90798b0
MacOS: Add support for timers to the platform integrations
itzurabhi 739e506
remove unused forward declarations
itzurabhi 1df46c6
move to CFTimer API
itzurabhi 009f7f4
remove debug statements
itzurabhi 79058bb
addressing comments
itzurabhi 6a2bbd8
make the timer map private
itzurabhi 824a0f8
tune timer to fix CI test runs
itzurabhi bde5a92
add info statements for elapsed time
itzurabhi 0340078
increase test case timeout
itzurabhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| This file is part of KDUtils. | ||
|
|
||
| SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
| Author: Paul Lemire <paul.lemire@kdab.com> | ||
|
|
||
| SPDX-License-Identifier: MIT | ||
|
|
||
| Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <Foundation/Foundation.h> | ||
|
|
||
| #include <chrono> | ||
|
|
||
| #include <KDFoundation/platform/abstract_platform_timer.h> | ||
| #include <KDFoundation/file_descriptor_notifier.h> | ||
|
|
||
| namespace KDFoundation { | ||
|
|
||
| class Timer; | ||
|
|
||
| class KDFOUNDATION_API MacOSPlatformTimer : public AbstractPlatformTimer | ||
| { | ||
| public: | ||
| MacOSPlatformTimer(Timer *timer); | ||
itzurabhi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ~MacOSPlatformTimer() override; | ||
|
|
||
| private: | ||
| void arm(std::chrono::microseconds us); | ||
| void disarm(); | ||
| static void timerFired(void *context); | ||
| Timer *m_handler; | ||
itzurabhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dispatch_source_t highResTimer; | ||
| }; | ||
|
|
||
| } // namespace KDFoundation | ||
116 changes: 116 additions & 0 deletions
116
src/KDFoundation/platform/macos/macos_platform_timer.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| This file is part of KDUtils. | ||
|
|
||
| SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
| Author: Paul Lemire <paul.lemire@kdab.com> | ||
|
|
||
| SPDX-License-Identifier: MIT | ||
|
|
||
| Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
| */ | ||
|
|
||
| #include "macos_platform_timer.h" | ||
| #include "macos_platform_event_loop.h" | ||
|
|
||
| #include <Foundation/Foundation.h> | ||
| #include <algorithm> | ||
itzurabhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <cassert> | ||
| #include <chrono> | ||
| #include <type_traits> | ||
| #include <unistd.h> | ||
| #import <AppKit/AppKit.h> | ||
itzurabhi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #include "KDFoundation/core_application.h" | ||
| #include "KDFoundation/timer.h" | ||
| #include "KDFoundation/platform/macos/macos_platform_event_loop.h" | ||
|
|
||
| namespace KDFoundation { | ||
|
|
||
| class NSTimerWrapper | ||
| { | ||
| NSTimer *timer; | ||
| }; | ||
| } // namespace KDFoundation | ||
|
|
||
| using namespace KDFoundation; | ||
|
|
||
| inline MacOSPlatformEventLoop *eventLoop() | ||
| { | ||
| return static_cast<MacOSPlatformEventLoop *>(CoreApplication::instance()->eventLoop()); | ||
| } | ||
|
|
||
| MacOSPlatformTimer::MacOSPlatformTimer(Timer *timer) | ||
| : m_handler{ timer }, highResTimer{ nullptr } | ||
| { | ||
|
|
||
| @autoreleasepool { | ||
| // make sure there's a NSApp | ||
| [NSApplication sharedApplication]; | ||
| } | ||
|
|
||
| timer->running.valueChanged().connect([this, timer](bool running) { | ||
| if (running) { | ||
| arm(timer->interval.get()); | ||
| } else { | ||
| disarm(); | ||
| } | ||
| }); | ||
| timer->interval.valueChanged().connect([this, timer]() { | ||
| if (timer->running.get()) { | ||
| arm(timer->interval.get()); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| MacOSPlatformTimer::~MacOSPlatformTimer() | ||
| { | ||
| disarm(); | ||
| } | ||
|
|
||
| void MacOSPlatformTimer::timerFired(void *context) | ||
| { | ||
| @autoreleasepool { | ||
| MacOSPlatformEventLoop *ev = eventLoop(); | ||
| dispatch_source_t caller = (dispatch_source_t)context; | ||
| if (auto it = ev->timerMap.find(caller); it != ev->timerMap.end()) { | ||
| it->second->m_handler->timeout.emit(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void MacOSPlatformTimer::arm(std::chrono::microseconds us) | ||
| { | ||
| @autoreleasepool { | ||
|
|
||
| if (highResTimer) { | ||
| disarm(); | ||
| } | ||
| const auto interval = std::chrono::duration_cast<std::chrono::nanoseconds>(us).count(); | ||
| dispatch_queue_main_t mainQueue = dispatch_get_main_queue(); | ||
| highResTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainQueue); | ||
| dispatch_source_set_timer(highResTimer, | ||
| dispatch_time(DISPATCH_TIME_NOW, interval), | ||
| interval, | ||
| 0); | ||
| dispatch_source_set_event_handler_f(highResTimer, timerFired); | ||
| dispatch_set_context(highResTimer, highResTimer); | ||
| dispatch_resume(highResTimer); | ||
|
|
||
| if (highResTimer) { | ||
| void *key = reinterpret_cast<void *>(highResTimer); | ||
| eventLoop()->timerMap[key] = this; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void MacOSPlatformTimer::disarm() | ||
| { | ||
| @autoreleasepool { | ||
| if (highResTimer) { | ||
| void *key = reinterpret_cast<void *>(highResTimer); | ||
| eventLoop()->timerMap.erase(key); | ||
| dispatch_source_cancel(highResTimer); | ||
| highResTimer = nullptr; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.