Last active
July 6, 2018 01:23
-
-
Save felix-schwarz/9fa9055b6ade900f1f21 to your computer and use it in GitHub Desktop.
This class makes it easy to spawn background threads, use their runloop and submit blocks to it from anywhere. Developed for scheduling NSStreams in the background. Keep in mind runloops aren't thread-safe, so when you use a thread's runloop, be sure to wrap that part of your code inside a block and pass it to -[ISRunLoopThread dispatchBlockToRu…
This file contains 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
// | |
// ISRunLoopThread.h | |
// | |
// Created by Felix Schwarz on 13.09.14. | |
// Copyright (c) 2014 IOSPIRIT GmbH. All rights reserved. | |
// | |
/* | |
# | |
# Copyright (c) 2014 Felix Schwarz (@felix_schwarz), IOSPIRIT GmbH (@iospirit) | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# | |
*/ | |
#import <Foundation/Foundation.h> | |
@interface ISRunLoopThread : NSObject | |
{ | |
NSThread *thread; | |
NSRunLoop *runLoop; | |
NSString *name; | |
dispatch_semaphore_t _syncSemaphore; | |
} | |
@property(retain) NSString *name; | |
@property(readonly) NSThread *thread; | |
@property(readonly) NSRunLoop *runLoop; | |
+ (instancetype)mainRunLoopThread; | |
+ (instancetype)currentRunLoopThread; | |
+ (instancetype)runLoopThreadNamed:(NSString *)runLoopThreadName; | |
- (void)dispatchBlockToRunLoopAsync:(dispatch_block_t)block; | |
@end |
This file contains 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
// | |
// ISRunLoopThread.m | |
// | |
// Created by Felix Schwarz on 13.09.14. | |
// Copyright (c) 2014 IOSPIRIT GmbH. All rights reserved. | |
// | |
#import "ISRunLoopThread.h" | |
#import <pthread/pthread.h> | |
static NSString *kISRunLoopThreadNameMain = @"__main__"; | |
@implementation ISRunLoopThread | |
@synthesize name; | |
@synthesize thread; | |
@synthesize runLoop; | |
+ (NSMutableDictionary *)_runLoopThreadsByNameDict | |
{ | |
static dispatch_once_t onceToken; | |
static NSMutableDictionary *sISRunLoopThreadsByName; | |
dispatch_once(&onceToken, ^{ | |
sISRunLoopThreadsByName = [[NSMutableDictionary alloc] init]; | |
}); | |
return (sISRunLoopThreadsByName); | |
} | |
+ (instancetype)mainRunLoopThread | |
{ | |
return ([self runLoopThreadNamed:kISRunLoopThreadNameMain]); | |
} | |
+ (instancetype)currentRunLoopThread | |
{ | |
ISRunLoopThread *currentRunLoopThread = nil; | |
if ([NSThread isMainThread]) | |
{ | |
return ([self mainRunLoopThread]); | |
} | |
else | |
{ | |
@synchronized(self) | |
{ | |
NSMutableDictionary *threadsByNameDict = [self _runLoopThreadsByNameDict]; | |
NSThread *currentThread = [NSThread currentThread]; | |
for (NSString *name in threadsByNameDict) | |
{ | |
ISRunLoopThread *runLoopThread; | |
runLoopThread = [threadsByNameDict objectForKey:name]; | |
if (runLoopThread.thread == currentThread) | |
{ | |
return (runLoopThread); | |
} | |
} | |
if ((currentRunLoopThread = [[ISRunLoopThread alloc] initWithName:[NSString stringWithFormat:@"%p",currentThread] thread:currentThread runLoop:[NSRunLoop currentRunLoop]]) != nil) | |
{ | |
[threadsByNameDict setObject:currentRunLoopThread forKey:currentRunLoopThread.name]; | |
[currentRunLoopThread release]; | |
} | |
} | |
} | |
return (currentRunLoopThread); | |
} | |
+ (instancetype)runLoopThreadNamed:(NSString *)runLoopThreadName | |
{ | |
ISRunLoopThread *runLoopThread = nil; | |
if (runLoopThreadName==nil) { return(nil); } | |
@synchronized(self) | |
{ | |
runLoopThread = [[self _runLoopThreadsByNameDict] objectForKey:runLoopThreadName]; | |
if (runLoopThread == nil) | |
{ | |
if ([runLoopThreadName isEqual:kISRunLoopThreadNameMain]) | |
{ | |
runLoopThread = [[self alloc] initWithMainThread]; | |
} | |
else | |
{ | |
runLoopThread = [[self alloc] initWithName:runLoopThreadName]; | |
} | |
if (runLoopThread != nil) | |
{ | |
[[self _runLoopThreadsByNameDict] setObject:runLoopThread forKey:runLoopThreadName]; | |
[runLoopThread autorelease]; | |
} | |
} | |
} | |
return (runLoopThread); | |
} | |
- (instancetype)init | |
{ | |
if ((self = [super init]) != nil) | |
{ | |
_syncSemaphore = dispatch_semaphore_create(0); | |
} | |
return (self); | |
} | |
- (instancetype)initWithMainThread | |
{ | |
if ((self = [self init]) != nil) | |
{ | |
self.name = kISRunLoopThreadNameMain; | |
runLoop = [NSRunLoop mainRunLoop]; | |
thread = [[NSThread mainThread] retain]; | |
} | |
return (self); | |
} | |
- (instancetype)initWithName:(NSString *)aName | |
{ | |
if ((self = [self init]) != nil) | |
{ | |
self.name = aName; | |
runLoop = nil; | |
thread = [[NSThread alloc] initWithTarget:self selector:@selector(_threadMain:) object:nil]; | |
[thread start]; | |
// Wait for thread's runloop to become available | |
dispatch_semaphore_wait(_syncSemaphore,DISPATCH_TIME_FOREVER); | |
} | |
return (self); | |
} | |
- (instancetype)initWithName:(NSString *)aName thread:(NSThread *)aThread runLoop:(NSRunLoop *)aRunLoop | |
{ | |
if ((self = [self init]) != nil) | |
{ | |
self.name = aName; | |
thread = [aThread retain]; | |
runLoop = aRunLoop; | |
} | |
return (self); | |
} | |
- (void)dealloc | |
{ | |
[name release]; | |
name = nil; | |
[thread release]; | |
thread = nil; | |
[_syncSemaphore release]; | |
_syncSemaphore = nil; | |
[super dealloc]; | |
} | |
- (void)_threadMain:(id)sender | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
// Set thread name | |
NSString *runLoopThreadName = [NSString stringWithFormat:@"ISRunLoopThread:%@",name]; | |
pthread_setname_np([runLoopThreadName UTF8String]); | |
[[NSThread currentThread] setName:runLoopThreadName]; | |
// Get runloop | |
runLoop = [NSRunLoop currentRunLoop]; | |
// Signal init | |
dispatch_semaphore_signal(_syncSemaphore); | |
// Run runloop | |
do | |
{ | |
NSDate *runUntil; | |
if ((runUntil = [[NSDate alloc] initWithTimeIntervalSinceNow:5.0]) != nil) | |
{ | |
[[NSRunLoop currentRunLoop] runUntilDate:runUntil]; | |
[runUntil release]; | |
} | |
}while(![thread isCancelled]); | |
// Clean up thread | |
runLoop = nil; | |
[pool drain]; | |
} | |
- (void)dispatchBlockToRunLoopAsync:(dispatch_block_t)block | |
{ | |
if (block != nil) | |
{ | |
dispatch_block_t jobBlock; | |
if ((jobBlock = [block copy]) != nil) | |
{ | |
[self performSelector:@selector(_executeBlock:) onThread:thread withObject:jobBlock waitUntilDone:NO]; | |
[jobBlock release]; | |
} | |
} | |
} | |
- (void)_executeBlock:(dispatch_block_t)block | |
{ | |
if (block != nil) | |
{ | |
block(); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment