]> git.saurik.com Git - cydia.git/blob - Menes/yieldToSelector.mm
Cytore values need to be _packed (I got lucky :D).
[cydia.git] / Menes / yieldToSelector.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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/>.
19 **/
20 /* }}} */
21
22 #include "Menes/yieldToSelector.h"
23
24 @implementation NSObject (MenesYieldToSelector)
25
26 - (void) doNothing {
27 }
28
29 - (void) _yieldToContext:(NSMutableArray *)context {
30 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
31
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]));
35
36 /* XXX: deal with exceptions */
37 id value([self performSelector:selector withObject:object]);
38
39 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
40 [context removeAllObjects];
41 if ([signature methodReturnLength] != 0 && value != nil)
42 [context addObject:value];
43
44 stopped = true;
45
46 [self
47 performSelectorOnMainThread:@selector(doNothing)
48 withObject:nil
49 waitUntilDone:NO
50 ];
51
52 [pool release];
53 }
54
55 - (id) yieldToSelector:(SEL)selector withObject:(id)object {
56 volatile bool stopped(false);
57
58 NSMutableArray *context([NSMutableArray arrayWithObjects:
59 [NSValue valueWithPointer:selector],
60 [NSValue valueWithNonretainedObject:object],
61 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
62 nil]);
63
64 NSThread *thread([[[NSThread alloc]
65 initWithTarget:self
66 selector:@selector(_yieldToContext:)
67 object:context
68 ] autorelease]);
69
70 [thread start];
71
72 NSRunLoop *loop([NSRunLoop currentRunLoop]);
73 NSDate *future([NSDate distantFuture]);
74 NSString *mode([loop currentMode] ?: NSDefaultRunLoopMode);
75
76 while (!stopped && [loop runMode:mode beforeDate:future]);
77
78 return [context count] == 0 ? nil : [context objectAtIndex:0];
79 }
80
81 - (id) yieldToSelector:(SEL)selector {
82 return [self yieldToSelector:selector withObject:nil];
83 }
84
85 @end