]>
Commit | Line | Data |
---|---|---|
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 "CyteKit/UCPlatform.h" | |
23 | ||
24 | #include "Menes/yieldToSelector.h" | |
25 | ||
26 | @implementation NSObject (MenesYieldToSelector) | |
27 | ||
28 | - (void) doNothing { | |
29 | } | |
30 | ||
31 | - (void) _yieldToContext:(NSMutableArray *)context { | |
32 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
33 | ||
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])); | |
37 | ||
38 | /* XXX: deal with exceptions */ | |
39 | id value([self performSelector:selector withObject:object]); | |
40 | ||
41 | NSMethodSignature *signature([self methodSignatureForSelector:selector]); | |
42 | [context removeAllObjects]; | |
43 | if ([signature methodReturnLength] != 0 && value != nil) | |
44 | [context addObject:value]; | |
45 | ||
46 | stopped = true; | |
47 | ||
48 | [self | |
49 | performSelectorOnMainThread:@selector(doNothing) | |
50 | withObject:nil | |
51 | waitUntilDone:NO | |
52 | ]; | |
53 | ||
54 | [pool release]; | |
55 | } | |
56 | ||
57 | - (id) yieldToSelector:(SEL)selector withObject:(id)object { | |
58 | volatile bool stopped(false); | |
59 | ||
60 | NSMutableArray *context([NSMutableArray arrayWithObjects: | |
61 | [NSValue valueWithPointer:selector], | |
62 | [NSValue valueWithNonretainedObject:object], | |
63 | [NSValue valueWithPointer:const_cast<bool *>(&stopped)], | |
64 | nil]); | |
65 | ||
66 | NSThread *thread([[[NSThread alloc] | |
67 | initWithTarget:self | |
68 | selector:@selector(_yieldToContext:) | |
69 | object:context | |
70 | ] autorelease]); | |
71 | ||
72 | [thread start]; | |
73 | ||
74 | NSRunLoop *loop([NSRunLoop currentRunLoop]); | |
75 | NSDate *future([NSDate distantFuture]); | |
76 | NSString *mode([loop currentMode] ?: NSDefaultRunLoopMode); | |
77 | ||
78 | while (!stopped && [loop runMode:mode beforeDate:future]); | |
79 | ||
80 | return [context count] == 0 ? nil : [context objectAtIndex:0]; | |
81 | } | |
82 | ||
83 | - (id) yieldToSelector:(SEL)selector { | |
84 | return [self yieldToSelector:selector withObject:nil]; | |
85 | } | |
86 | ||
87 | @end |