mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 00:11:26 +00:00
Start implementing scheduler
This commit is contained in:
parent
873e4edc47
commit
6ef18c3ec3
3 changed files with 67 additions and 15 deletions
|
@ -27,6 +27,7 @@
|
|||
|
||||
#include <Foundation/NSObject.h>
|
||||
#include <Foundation/NSDate.h>
|
||||
#include <Foundation/NSProcessInfo.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
|
@ -53,8 +54,16 @@ typedef NSInteger NSQualityOfService;
|
|||
|
||||
@class NSString, NSTimer;
|
||||
|
||||
DEFINE_BLOCK_TYPE(NSBackgroundActivityCompletionHandler, void, NSBackgroundActivityResult);
|
||||
DEFINE_BLOCK_TYPE(GSScheduledBlock, void, NSBackgroundActivityCompletionHandler);
|
||||
# ifndef __has_feature
|
||||
# define __has_feature(x) 0
|
||||
# endif
|
||||
|
||||
//# if __has_feature(blocks)
|
||||
//typedef void(^NSBackgroundActivityCompletionHandler)(NSBackgroundActivityResult result) GSScheduledBlock;
|
||||
//# else
|
||||
DEFINE_BLOCK_TYPE(NSBackgroundActivityCompletionHandler, void, NSBackgroundActivityResult);
|
||||
DEFINE_BLOCK_TYPE(GSScheduledBlock, void, NSBackgroundActivityCompletionHandler);
|
||||
//# endif
|
||||
|
||||
@interface NSBackgroundActivityScheduler : NSObject
|
||||
{
|
||||
|
@ -64,7 +73,11 @@ DEFINE_BLOCK_TYPE(GSScheduledBlock, void, NSBackgroundActivityCompletionHandler)
|
|||
NSTimeInterval _tolerance;
|
||||
BOOL _repeats;
|
||||
BOOL _shouldDefer;
|
||||
NSTimer *_timer;
|
||||
NSTimer *_timer;
|
||||
NSActivityOptions _opts;
|
||||
id _token;
|
||||
NSString *_reason;
|
||||
BLOCK_SCOPE GSScheduledBlock _block;
|
||||
}
|
||||
|
||||
- (instancetype) initWithIdentifier: (NSString *)identifier;
|
||||
|
@ -86,9 +99,9 @@ DEFINE_BLOCK_TYPE(GSScheduledBlock, void, NSBackgroundActivityCompletionHandler)
|
|||
|
||||
- (BOOL) shouldDefer;
|
||||
- (void) setShouldDefer: (BOOL)flag;
|
||||
|
||||
|
||||
- (void) scheduleWithBlock: (GSScheduledBlock)block;
|
||||
|
||||
|
||||
- (void) invalidate;
|
||||
|
||||
@end
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
*/
|
||||
|
||||
#include <Foundation/NSBackgroundActivityScheduler.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSTimer.h>
|
||||
|
||||
@implementation NSBackgroundActivityScheduler
|
||||
|
||||
|
@ -38,10 +40,21 @@
|
|||
_tolerance = 0;
|
||||
_shouldDefer = NO;
|
||||
_timer = nil;
|
||||
_opts = 0;
|
||||
_token = nil;
|
||||
_reason = [NSString stringWithFormat: @"Reason-%@", self];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_identifier);
|
||||
RELEASE(_token);
|
||||
RELEASE(_reason);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSString *) identifier
|
||||
{
|
||||
return _identifier;
|
||||
|
@ -102,36 +115,61 @@
|
|||
_shouldDefer = flag;
|
||||
}
|
||||
|
||||
- (void) _performActivity
|
||||
{
|
||||
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
|
||||
|
||||
# if __has_feature(blocks)
|
||||
[pinfo performActivityWithOptions: _opts
|
||||
reason: _reason
|
||||
usingBlock: ^{
|
||||
// CALL_BLOCK(_block);
|
||||
}];
|
||||
# else
|
||||
NSLog(@"No block support, so not running background activity....");
|
||||
# endif
|
||||
}
|
||||
|
||||
- (void) scheduleWithBlock: (GSScheduledBlock)block
|
||||
{
|
||||
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
|
||||
id token = nil;
|
||||
NSActivityOptions opts = 0;
|
||||
|
||||
switch(qualityOfService)
|
||||
|
||||
ASSIGN(_block, block);
|
||||
switch(_qualityOfService)
|
||||
{
|
||||
case NSQualityOfServiceUserInteractive:
|
||||
opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
||||
_opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
||||
break;
|
||||
case NSQualityOfServiceUserInitiated:
|
||||
opts = NSActivityUserInitiated;
|
||||
_opts = NSActivityUserInitiated;
|
||||
break;
|
||||
case NSQualityOfServiceUtility:
|
||||
opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
||||
_opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
||||
break;
|
||||
case NSQualityOfServiceBackground:
|
||||
opts = NSActivityBackground;
|
||||
_opts = NSActivityBackground;
|
||||
break;
|
||||
case NSQualityOfServiceDefault:
|
||||
opts = NSActivityLatencyCritical;
|
||||
_opts = NSActivityLatencyCritical;
|
||||
break;
|
||||
}
|
||||
|
||||
token = [pinfo beginActivityWithOptions:
|
||||
_token = [pinfo beginActivityWithOptions: _opts
|
||||
reason: _reason];
|
||||
|
||||
_timer = [NSTimer scheduledTimerWithTimeInterval: _interval
|
||||
target: self
|
||||
selector: @selector(_performActivity)
|
||||
userInfo: nil
|
||||
repeats: _repeats];
|
||||
|
||||
}
|
||||
|
||||
- (void) invalidate
|
||||
{
|
||||
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
|
||||
[_timer invalidate];
|
||||
[pinfo endActivity: _token];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -116,6 +116,7 @@ static Class NSDate_class;
|
|||
userInfo: (id)info
|
||||
repeats: (BOOL)f
|
||||
{
|
||||
_block = nil;
|
||||
if (ti <= 0.0)
|
||||
{
|
||||
ti = 0.0001;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue