1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
22 #include "Menes/yieldToSelector.h"
24 @implementation NSObject (MenesYieldToSelector)
29 - (void) _yieldToContext:(NSMutableArray *)context {
30 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
32 SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
33 id object([[context objectAtIndex:1] nonretainedObjectValue]);
34 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
36 /* XXX: deal with exceptions */
37 id value([self performSelector:selector withObject:object]);
39 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
40 [context removeAllObjects];
41 if ([signature methodReturnLength] != 0 && value != nil)
42 [context addObject:value];
47 performSelectorOnMainThread:@selector(doNothing)
55 - (id) yieldToSelector:(SEL)selector withObject:(id)object {
56 volatile bool stopped(false);
58 NSMutableArray *context([NSMutableArray arrayWithObjects:
59 [NSValue valueWithPointer:selector],
60 [NSValue valueWithNonretainedObject:object],
61 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
64 NSThread *thread([[[NSThread alloc]
66 selector:@selector(_yieldToContext:)
72 NSRunLoop *loop([NSRunLoop currentRunLoop]);
73 NSDate *future([NSDate distantFuture]);
74 NSString *mode([loop currentMode] ?: NSDefaultRunLoopMode);
76 while (!stopped && [loop runMode:mode beforeDate:future]);
78 return [context count] == 0 ? nil : [context objectAtIndex:0];
81 - (id) yieldToSelector:(SEL)selector {
82 return [self yieldToSelector:selector withObject:nil];