| 1 | // -*- coding: utf-8-dos -*-
|
|---|
| 2 |
|
|---|
| 3 | #import <Cocoa/Cocoa.h>
|
|---|
| 4 |
|
|---|
| 5 | #define NUM_THREADS 20
|
|---|
| 6 |
|
|---|
| 7 | // [NSThread sleepUntilDate:[[NSDate date] addTimeInterval:0.5]];
|
|---|
| 8 | // [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
|
|---|
| 9 |
|
|---|
| 10 | @interface MainTask : NSObject {
|
|---|
| 11 | @public
|
|---|
| 12 | NSConnection* connections[NUM_THREADS];
|
|---|
| 13 | }
|
|---|
| 14 | - (void)start;
|
|---|
| 15 | - (void)say:(NSString*)message;
|
|---|
| 16 | - (void)addNewThread:(NSNumber*)taskNumber;
|
|---|
| 17 | @end
|
|---|
| 18 |
|
|---|
| 19 | @interface SubTask : NSObject {
|
|---|
| 20 | @public
|
|---|
| 21 | NSString* ownerThreadName;
|
|---|
| 22 | NSNumber* taskNumber;
|
|---|
| 23 | }
|
|---|
| 24 | - (oneway void)handleProxy;
|
|---|
| 25 | - (oneway void)handleProxy2:(int)sender;
|
|---|
| 26 | - (void)startThread:(NSArray*)data;
|
|---|
| 27 | @end
|
|---|
| 28 |
|
|---|
| 29 | SubTask* proxies[NUM_THREADS];
|
|---|
| 30 |
|
|---|
| 31 | @implementation MainTask
|
|---|
| 32 | - (void)say:(NSString*)message {
|
|---|
| 33 | NSLog(@"say [%s] %s", [[[NSThread currentThread] name] UTF8String], [message UTF8String]);
|
|---|
| 34 | }
|
|---|
| 35 | - (void)addNewThread:(NSNumber*)taskNumber {
|
|---|
| 36 | int task_num = [taskNumber intValue];
|
|---|
| 37 | NSLog(@"addNewThread [%s] %d", [[[NSThread currentThread] name] UTF8String], task_num);
|
|---|
| 38 | proxies[task_num] = (SubTask*)[connections[task_num] rootProxy];
|
|---|
| 39 | NSLog(@"addNewThread [%s] %d rootProxy", [[[NSThread currentThread] name] UTF8String], task_num);
|
|---|
| 40 | [proxies[task_num] handleProxy];
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | - (void)start {
|
|---|
| 44 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|---|
| 45 | NSLog(@"start [%s]", [[[NSThread currentThread] name] UTF8String]);
|
|---|
| 46 |
|
|---|
| 47 | NS_DURING
|
|---|
| 48 | {
|
|---|
| 49 | int task_num = 0;
|
|---|
| 50 | for (task_num = 0; task_num < NUM_THREADS; task_num++) {
|
|---|
| 51 | NSNumber* taskNumber = [[NSNumber numberWithInt:task_num] retain];
|
|---|
| 52 | proxies[task_num] = nil;
|
|---|
| 53 | NSPort* port1 = [[NSPort port] retain];
|
|---|
| 54 | NSPort* port2 = [[NSPort port] retain];
|
|---|
| 55 | if (port1 == nil) {
|
|---|
| 56 | NSLog(@"port1 == nil");
|
|---|
| 57 | return;
|
|---|
| 58 | }
|
|---|
| 59 | if (port2 == nil) {
|
|---|
| 60 | NSLog(@"port2 == nil");
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 | connections[task_num] =
|
|---|
| 64 | [[NSConnection alloc] initWithReceivePort:port1
|
|---|
| 65 | sendPort:port2];
|
|---|
| 66 | [connections[task_num] retain];
|
|---|
| 67 | NSArray* data = [[NSArray arrayWithObjects:taskNumber, port1, port2, nil] retain];
|
|---|
| 68 | SubTask* task = [[[SubTask alloc] init] retain];
|
|---|
| 69 | [NSThread detachNewThreadSelector:@selector(startThread:)
|
|---|
| 70 | toTarget:task
|
|---|
| 71 | withObject:data];
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | NS_HANDLER
|
|---|
| 75 | {
|
|---|
| 76 | NSLog(@"Objective-C exception [%s] name=[%s] reason=[%s]\n",
|
|---|
| 77 | [[[NSThread currentThread] name] UTF8String],
|
|---|
| 78 | [[localException name] UTF8String],
|
|---|
| 79 | [[localException reason] UTF8String]);
|
|---|
| 80 | }
|
|---|
| 81 | NS_ENDHANDLER
|
|---|
| 82 |
|
|---|
| 83 | [pool release];
|
|---|
| 84 | }
|
|---|
| 85 | @end
|
|---|
| 86 |
|
|---|
| 87 | MainTask* mainTask = nil;
|
|---|
| 88 |
|
|---|
| 89 | @implementation SubTask
|
|---|
| 90 | - (oneway void)handleProxy2:(int)sender {
|
|---|
| 91 | NSLog(@"handleProxy2 [%s] sender=%d", [[[NSThread currentThread] name] UTF8String], sender);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | - (oneway void)handleProxy {
|
|---|
| 95 | NSLog(@"handleProxy [%s] %s", [[[NSThread currentThread] name] UTF8String], [ownerThreadName UTF8String]);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | - (void)startThread:(NSArray*)data {
|
|---|
| 99 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|---|
| 100 |
|
|---|
| 101 | NS_DURING
|
|---|
| 102 | {
|
|---|
| 103 | taskNumber = [data objectAtIndex:0];
|
|---|
| 104 | NSPort* port1 = [data objectAtIndex:1];
|
|---|
| 105 | NSPort* port2 = [data objectAtIndex:2];
|
|---|
| 106 |
|
|---|
| 107 | ownerThreadName = [@"SubTask" stringByAppendingString:[taskNumber description]];
|
|---|
| 108 | [ownerThreadName retain];
|
|---|
| 109 |
|
|---|
| 110 | NSThread* currentThread = [NSThread currentThread];
|
|---|
| 111 | NSString* name = [currentThread name];
|
|---|
| 112 | if (name != nil) {
|
|---|
| 113 | NSLog(@"warning [[NSThread currentThread] name] is already set \"%s\"", name);
|
|---|
| 114 | }
|
|---|
| 115 | [currentThread setName:ownerThreadName];
|
|---|
| 116 |
|
|---|
| 117 | NSLog(@"ready [%s] %s", [[[NSThread currentThread] name] UTF8String], [ownerThreadName UTF8String]);
|
|---|
| 118 |
|
|---|
| 119 | //@synchronized (mainTask) {
|
|---|
| 120 | // [mainTask performSelectorOnMainThread:@selector(say:) withObject:ownerThreadName waitUntilDone:NO];
|
|---|
| 121 | //}
|
|---|
| 122 |
|
|---|
| 123 | NSConnection *serverConnection =
|
|---|
| 124 | [NSConnection connectionWithReceivePort:port2
|
|---|
| 125 | sendPort:port1];
|
|---|
| 126 | [serverConnection retain];
|
|---|
| 127 | [serverConnection setRootObject:self];
|
|---|
| 128 |
|
|---|
| 129 | [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
|
|---|
| 130 |
|
|---|
| 131 | @synchronized (mainTask) {
|
|---|
| 132 | [mainTask performSelectorOnMainThread:@selector(addNewThread:) withObject:taskNumber waitUntilDone:NO];
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];
|
|---|
| 136 |
|
|---|
| 137 | // invoke proxy
|
|---|
| 138 | NSLog(@"execute handleProxy2 [%s]", [[[NSThread currentThread] name] UTF8String]);
|
|---|
| 139 | int n = [taskNumber intValue];
|
|---|
| 140 | SubTask* p = proxies[(n + 1) % NUM_THREADS];
|
|---|
| 141 | @synchronized (p) {
|
|---|
| 142 | if (p == nil) {
|
|---|
| 143 | NSLog(@"p == nil");
|
|---|
| 144 | return;
|
|---|
| 145 | }
|
|---|
| 146 | [p handleProxy2:n];
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | [[NSRunLoop currentRunLoop] run];
|
|---|
| 150 | }
|
|---|
| 151 | NS_HANDLER
|
|---|
| 152 | {
|
|---|
| 153 | NSLog(@"Objective-C exception [%s] name=[%s] reason=[%s]\n",
|
|---|
| 154 | [[[NSThread currentThread] name] UTF8String],
|
|---|
| 155 | [[localException name] UTF8String],
|
|---|
| 156 | [[localException reason] UTF8String]);
|
|---|
| 157 | }
|
|---|
| 158 | NS_ENDHANDLER
|
|---|
| 159 |
|
|---|
| 160 | [pool release];
|
|---|
| 161 | }
|
|---|
| 162 | @end
|
|---|
| 163 |
|
|---|
| 164 | int main() {
|
|---|
| 165 | fputs("main()\n", stderr);
|
|---|
| 166 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|---|
| 167 |
|
|---|
| 168 | NS_DURING
|
|---|
| 169 | {
|
|---|
| 170 | [NSApplication sharedApplication];
|
|---|
| 171 |
|
|---|
| 172 | [[NSThread currentThread] setName:@"main() thread"];
|
|---|
| 173 |
|
|---|
| 174 | mainTask = [[MainTask alloc] init];
|
|---|
| 175 | [mainTask performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
|
|---|
| 176 |
|
|---|
| 177 | [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
|
|---|
| 178 | }
|
|---|
| 179 | NS_HANDLER
|
|---|
| 180 | {
|
|---|
| 181 | NSLog(@"Objective-C exception [%s] name=[%s] reason=[%s]\n",
|
|---|
| 182 | [[[NSThread currentThread] name] UTF8String],
|
|---|
| 183 | [[localException name] UTF8String],
|
|---|
| 184 | [[localException reason] UTF8String]);
|
|---|
| 185 | }
|
|---|
| 186 | NS_ENDHANDLER
|
|---|
| 187 |
|
|---|
| 188 | [pool release];
|
|---|
| 189 | fputs("main() return\n", stderr);
|
|---|
| 190 | return 0;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|