2020-11-29 11:57:47 +00:00
|
|
|
#import "GSTimeoutSource.h"
|
|
|
|
|
|
|
|
@implementation GSTimeoutSource
|
|
|
|
|
|
|
|
- (instancetype) initWithQueue: (dispatch_queue_t)queue
|
|
|
|
milliseconds: (NSInteger)milliseconds
|
|
|
|
handler: (dispatch_block_t)handler
|
|
|
|
{
|
|
|
|
if (nil != (self = [super init]))
|
|
|
|
{
|
|
|
|
_queue = queue;
|
2023-01-13 11:48:22 +00:00
|
|
|
_handler = Block_copy(handler);
|
2020-11-29 11:57:47 +00:00
|
|
|
_milliseconds = milliseconds;
|
|
|
|
_rawSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
|
|
|
|
|
|
|
|
uint64_t delay = MAX(1, milliseconds - 1);
|
|
|
|
|
|
|
|
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_MSEC);
|
|
|
|
|
|
|
|
dispatch_source_set_timer(_rawSource, start, delay * NSEC_PER_MSEC, _milliseconds == 1 ? 1 * NSEC_PER_USEC : 1 * NSEC_PER_MSEC);
|
|
|
|
dispatch_source_set_event_handler(_rawSource, _handler);
|
|
|
|
dispatch_resume(_rawSource);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2023-01-13 11:48:22 +00:00
|
|
|
[self cancel];
|
|
|
|
Block_release(_handler);
|
2020-11-29 11:57:47 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2023-01-13 11:48:22 +00:00
|
|
|
- (void) cancel
|
|
|
|
{
|
|
|
|
if (_rawSource)
|
|
|
|
{
|
|
|
|
dispatch_source_cancel(_rawSource);
|
|
|
|
dispatch_release(_rawSource);
|
|
|
|
_rawSource = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 11:57:47 +00:00
|
|
|
- (NSInteger) milliseconds
|
|
|
|
{
|
|
|
|
return _milliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (dispatch_queue_t) queue
|
|
|
|
{
|
|
|
|
return _queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (dispatch_block_t) handler
|
|
|
|
{
|
|
|
|
return _handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|