mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +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/NSObject.h>
|
||||||
#include <Foundation/NSDate.h>
|
#include <Foundation/NSDate.h>
|
||||||
|
#include <Foundation/NSProcessInfo.h>
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -53,8 +54,16 @@ typedef NSInteger NSQualityOfService;
|
||||||
|
|
||||||
@class NSString, NSTimer;
|
@class NSString, NSTimer;
|
||||||
|
|
||||||
DEFINE_BLOCK_TYPE(NSBackgroundActivityCompletionHandler, void, NSBackgroundActivityResult);
|
# ifndef __has_feature
|
||||||
DEFINE_BLOCK_TYPE(GSScheduledBlock, void, NSBackgroundActivityCompletionHandler);
|
# 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
|
@interface NSBackgroundActivityScheduler : NSObject
|
||||||
{
|
{
|
||||||
|
@ -64,7 +73,11 @@ DEFINE_BLOCK_TYPE(GSScheduledBlock, void, NSBackgroundActivityCompletionHandler)
|
||||||
NSTimeInterval _tolerance;
|
NSTimeInterval _tolerance;
|
||||||
BOOL _repeats;
|
BOOL _repeats;
|
||||||
BOOL _shouldDefer;
|
BOOL _shouldDefer;
|
||||||
NSTimer *_timer;
|
NSTimer *_timer;
|
||||||
|
NSActivityOptions _opts;
|
||||||
|
id _token;
|
||||||
|
NSString *_reason;
|
||||||
|
BLOCK_SCOPE GSScheduledBlock _block;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (instancetype) initWithIdentifier: (NSString *)identifier;
|
- (instancetype) initWithIdentifier: (NSString *)identifier;
|
||||||
|
@ -86,9 +99,9 @@ DEFINE_BLOCK_TYPE(GSScheduledBlock, void, NSBackgroundActivityCompletionHandler)
|
||||||
|
|
||||||
- (BOOL) shouldDefer;
|
- (BOOL) shouldDefer;
|
||||||
- (void) setShouldDefer: (BOOL)flag;
|
- (void) setShouldDefer: (BOOL)flag;
|
||||||
|
|
||||||
- (void) scheduleWithBlock: (GSScheduledBlock)block;
|
- (void) scheduleWithBlock: (GSScheduledBlock)block;
|
||||||
|
|
||||||
- (void) invalidate;
|
- (void) invalidate;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -23,6 +23,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <Foundation/NSBackgroundActivityScheduler.h>
|
#include <Foundation/NSBackgroundActivityScheduler.h>
|
||||||
|
#include <Foundation/NSString.h>
|
||||||
|
#include <Foundation/NSTimer.h>
|
||||||
|
|
||||||
@implementation NSBackgroundActivityScheduler
|
@implementation NSBackgroundActivityScheduler
|
||||||
|
|
||||||
|
@ -38,10 +40,21 @@
|
||||||
_tolerance = 0;
|
_tolerance = 0;
|
||||||
_shouldDefer = NO;
|
_shouldDefer = NO;
|
||||||
_timer = nil;
|
_timer = nil;
|
||||||
|
_opts = 0;
|
||||||
|
_token = nil;
|
||||||
|
_reason = [NSString stringWithFormat: @"Reason-%@", self];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void) dealloc
|
||||||
|
{
|
||||||
|
RELEASE(_identifier);
|
||||||
|
RELEASE(_token);
|
||||||
|
RELEASE(_reason);
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
- (NSString *) identifier
|
- (NSString *) identifier
|
||||||
{
|
{
|
||||||
return _identifier;
|
return _identifier;
|
||||||
|
@ -102,36 +115,61 @@
|
||||||
_shouldDefer = flag;
|
_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
|
- (void) scheduleWithBlock: (GSScheduledBlock)block
|
||||||
{
|
{
|
||||||
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
|
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
|
||||||
id token = nil;
|
|
||||||
NSActivityOptions opts = 0;
|
ASSIGN(_block, block);
|
||||||
|
switch(_qualityOfService)
|
||||||
switch(qualityOfService)
|
|
||||||
{
|
{
|
||||||
case NSQualityOfServiceUserInteractive:
|
case NSQualityOfServiceUserInteractive:
|
||||||
opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
_opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
||||||
break;
|
break;
|
||||||
case NSQualityOfServiceUserInitiated:
|
case NSQualityOfServiceUserInitiated:
|
||||||
opts = NSActivityUserInitiated;
|
_opts = NSActivityUserInitiated;
|
||||||
break;
|
break;
|
||||||
case NSQualityOfServiceUtility:
|
case NSQualityOfServiceUtility:
|
||||||
opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
_opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
|
||||||
break;
|
break;
|
||||||
case NSQualityOfServiceBackground:
|
case NSQualityOfServiceBackground:
|
||||||
opts = NSActivityBackground;
|
_opts = NSActivityBackground;
|
||||||
break;
|
break;
|
||||||
case NSQualityOfServiceDefault:
|
case NSQualityOfServiceDefault:
|
||||||
opts = NSActivityLatencyCritical;
|
_opts = NSActivityLatencyCritical;
|
||||||
break;
|
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
|
- (void) invalidate
|
||||||
{
|
{
|
||||||
|
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
|
||||||
|
[_timer invalidate];
|
||||||
|
[pinfo endActivity: _token];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -116,6 +116,7 @@ static Class NSDate_class;
|
||||||
userInfo: (id)info
|
userInfo: (id)info
|
||||||
repeats: (BOOL)f
|
repeats: (BOOL)f
|
||||||
{
|
{
|
||||||
|
_block = nil;
|
||||||
if (ti <= 0.0)
|
if (ti <= 0.0)
|
||||||
{
|
{
|
||||||
ti = 0.0001;
|
ti = 0.0001;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue