root/lang/objective-cplusplus/i3/trunk/tmp/onmainthread.cpp @ 36474

Revision 36474, 6.2 kB (checked in by saturday06, 5 years ago)

asdf

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