Skip to content
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

Investigation: timers synchronization #33

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DetoxSync/DetoxSync/Spies/NSTimer+DTXSpy.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void _DTXCFTimerTrampoline(CFRunLoopTimerRef timer, void *info)
// NSLog(@"❤️ %p", timer);

id<DTXTimerProxy> tp = [DTXTimerSyncResource existingTimerProxyWithTimer:NS(timer)];
[tp fire:(__bridge NSTimer*)timer];
[tp fire];
}

static CFRunLoopTimerRef (*__orig_CFRunLoopTimerCreate)(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, CFRunLoopTimerCallBack callout, CFRunLoopTimerContext *context);
Expand Down
2 changes: 1 addition & 1 deletion DetoxSync/DetoxSync/SyncResources/DTXTimerSyncResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN

//NSTimer
@property (nonatomic, weak) NSTimer* timer;
- (void)fire:(NSTimer*)timer;
- (void)fire;
@property (nonatomic) CFRunLoopRef runLoop;

//CADisplayLink
Expand Down
49 changes: 13 additions & 36 deletions DetoxSync/DetoxSync/Utils/_DTXTimerTrampoline.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,47 +112,24 @@ - (void)setDisplayLink:(CADisplayLink*)displayLink
#endif
}

- (void)fire:(id)timer
- (void)fire
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following changes are a bit more involved.

Firstly, there's no need for the timer parameter - we already have a reference to that as an ivar.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand why the timer argument is redundant.

{
//This is to ensure the timer is still valid after fire.
CFRunLoopRef runloop = CFRunLoopGetCurrent();
CFRunLoopMode mode = CFRunLoopCopyCurrentMode(runloop);
CFRunLoopPerformBlock(runloop, mode, ^{
if(CFRunLoopTimerIsValid((__bridge CFRunLoopTimerRef)timer) == NO)
{
[self untrack];

CFRelease(mode);

return;
}

CFRunLoopPerformBlock(runloop, mode, ^{
if(CFRunLoopTimerIsValid((__bridge CFRunLoopTimerRef)timer) == NO)
{
[self untrack];

CFRelease(mode);

return;
}

CFRelease(mode);
});
});
Comment on lines -117 to -142
Copy link
Contributor Author

@ball-hayden ball-hayden Apr 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand right, the purpose of these are to see if the timer is still valid after it has fired.

If we ask if the timer is valid inline here, CFRunLoopTimerIsValid always evaluates to true, I assume because we're still within the fire function.

Here I'm proposing a different method.

We track the invalidation and removal of timers (in NSTimer+DTXSpy.m), which means we know when a repeating timer is stopping. We also know that a timer that doesn't repeat is finished once we have called its callback (or selector).

Therefore, we can simply call untrack if we are not a recurring timer after we have called its callback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, this code checks whether the timer needs to be untracked (for example, as you mentioned - in case of a non-repeating timer) immediately after finishing the fire.

Therefore, we can simply call untrack if we are not a recurring timer after we have called its callback.

Generally, this sounds right, but I'm not sure if recurring / non-recurring is our only flow. What about calling to CFRunLoopTimerInvalidate on the 3rd fire or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling CFRunLoopTimerInvalidate will result in an explicit call to untrack from the spy:

https://github.com/PlayerData/DetoxSync/blob/f5cd8cbde60311bb8e41df7c773850cd73c8f84b/DetoxSync/DetoxSync/Spies/NSTimer%2BDTXSpy.m#L108


if(_callback)
{
CFRunLoopTimerContext ctx;
CFRunLoopTimerGetContext((__bridge CFRunLoopTimerRef)timer, &ctx);
_callback((__bridge CFRunLoopTimerRef)timer, ctx.info);
return;
CFRunLoopTimerGetContext((__bridge CFRunLoopTimerRef)_timer, &ctx);
_callback((__bridge CFRunLoopTimerRef)_timer, ctx.info);
}

if(_target && _sel) {
IMP impl = [_target methodForSelector:_sel];
void (*func)(id, SEL, NSTimer*) = (void *)impl;
func(_target, _sel, _timer);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a small tidy-up recommended by https://stackoverflow.com/a/20058585

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if(!_repeats) {
[self untrack];
}
Comment on lines +141 to 143
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this will have the same result as the previous check.


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[_target performSelector:_sel withObject:timer];
#pragma clang diagnostic pop
Comment on lines -152 to -155
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you removed this? I guess it was there for a reason

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is replaced by ln 131-138 - gaining a reference to impl explicitly is recommended.

I think my previous comment was lost - sorry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't, you're right #33 (comment), forgot about this for a moment 😅

}

- (void)track
Expand Down