1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2008-2011 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 "yieldToSelector.h"
24 @implementation NSObject (CydgetYieldToSelector)
26 - (void) cydget$doNothing {
29 - (void) cydget$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(cydget$doNothing)
55 - (id) cydget$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(cydget$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) cydget$yieldToSelector:(SEL)selector {
82 return [self cydget$yieldToSelector:selector withObject:nil];