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 "CyteKit/UCPlatform.h"
24 #include "Menes/yieldToSelector.h"
26 @implementation NSObject (MenesYieldToSelector)
31 - (void) _yieldToContext:(NSMutableArray *)context {
32 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
34 SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
35 id object([[context objectAtIndex:1] nonretainedObjectValue]);
36 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
38 /* XXX: deal with exceptions */
39 id value([self performSelector:selector withObject:object]);
41 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
42 [context removeAllObjects];
43 if ([signature methodReturnLength] != 0 && value != nil)
44 [context addObject:value];
49 performSelectorOnMainThread:@selector(doNothing)
57 - (id) yieldToSelector:(SEL)selector withObject:(id)object {
58 volatile bool stopped(false);
60 NSMutableArray *context([NSMutableArray arrayWithObjects:
61 [NSValue valueWithPointer:selector],
62 [NSValue valueWithNonretainedObject:object],
63 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
66 NSThread *thread([[[NSThread alloc]
68 selector:@selector(_yieldToContext:)
74 NSRunLoop *loop([NSRunLoop currentRunLoop]);
75 NSDate *future([NSDate distantFuture]);
76 NSString *mode([loop currentMode] ?: NSDefaultRunLoopMode);
78 while (!stopped && [loop runMode:mode beforeDate:future]);
80 return [context count] == 0 ? nil : [context objectAtIndex:0];
83 - (id) yieldToSelector:(SEL)selector {
84 return [self yieldToSelector:selector withObject:nil];