| 1 | #include <mil/PrecompiledHeaders.h>
|
|---|
| 2 | #include <mil/Synchronize.h>
|
|---|
| 3 |
|
|---|
| 4 | const int NUM_THREADS = 3;
|
|---|
| 5 | mil::Mutex mutex;
|
|---|
| 6 | NSPort* ports1[NUM_THREADS][NUM_THREADS];
|
|---|
| 7 | NSPort* ports2[NUM_THREADS][NUM_THREADS];
|
|---|
| 8 |
|
|---|
| 9 | @interface WorkerObj : NSObject {
|
|---|
| 10 | @public
|
|---|
| 11 | int thread_id;
|
|---|
| 12 | }
|
|---|
| 13 | - (oneway void)someInt:(int)foo;
|
|---|
| 14 | @end
|
|---|
| 15 |
|
|---|
| 16 | @implementation WorkerObj
|
|---|
| 17 | - (oneway void)someInt:(int)foo
|
|---|
| 18 | {
|
|---|
| 19 | NSLog(@"someInt in thread %d, %d", [NSThread currentThread], foo);
|
|---|
| 20 | }
|
|---|
| 21 | - (void)handleNotification3:(NSNotification*)notification
|
|---|
| 22 | {
|
|---|
| 23 | NSLog(@"notification3: %d", thread_id);
|
|---|
| 24 |
|
|---|
| 25 | synchronized (mutex) {
|
|---|
| 26 | for (int i = 0; i < NUM_THREADS; i++) {
|
|---|
| 27 | ports1[thread_id][i] = [NSPort port];
|
|---|
| 28 | ports2[thread_id][i] = [NSPort port];
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | - (void)handleNotification2:(NSNotification*)notification
|
|---|
| 34 | {
|
|---|
| 35 | NSLog(@"notification2: %d", thread_id);
|
|---|
| 36 |
|
|---|
| 37 | for (int i = 0; i < NUM_THREADS; i++) {
|
|---|
| 38 | if (i == thread_id) {
|
|---|
| 39 | continue;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | NSPort* port1 = nil;
|
|---|
| 43 | NSPort* port2 = nil;
|
|---|
| 44 | synchronized (mutex) {
|
|---|
| 45 | port1 = ports1[i][thread_id];
|
|---|
| 46 | port2 = ports2[i][thread_id];
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if (port1 == nil || port2 == nil) {
|
|---|
| 50 | continue;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | NSConnection *serverConnection =
|
|---|
| 54 | [NSConnection connectionWithReceivePort:port2 sendPort:port1];
|
|---|
| 55 | [serverConnection setRootObject:self];
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | - (void)handleNotification1:(NSNotification*)notification
|
|---|
| 61 | {
|
|---|
| 62 | NSLog(@"notification1: %d", thread_id);
|
|---|
| 63 |
|
|---|
| 64 | for (int i = 0; i < NUM_THREADS; i++) {
|
|---|
| 65 | if (i == thread_id) {
|
|---|
| 66 | continue;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | NSPort* port1 = nil;
|
|---|
| 70 | NSPort* port2 = nil;
|
|---|
| 71 | synchronized (mutex) {
|
|---|
| 72 | port1 = ports1[thread_id][i];
|
|---|
| 73 | port2 = ports2[thread_id][i];
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | NSConnection *connection =
|
|---|
| 77 | [[NSConnection alloc] initWithReceivePort:port1 sendPort:port2];
|
|---|
| 78 |
|
|---|
| 79 | WorkerObj* mainThreadProxy = (WorkerObj*)[connection rootProxy];
|
|---|
| 80 | [mainThreadProxy someInt: 10 * thread_id];
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | }
|
|---|
| 84 | @end
|
|---|
| 85 |
|
|---|
| 86 | void* thread_routine(void* foo) {
|
|---|
| 87 | int thread_id = (int)foo;
|
|---|
| 88 | sleep(1);
|
|---|
| 89 |
|
|---|
| 90 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|---|
| 91 |
|
|---|
| 92 | NSLog(@"thread %d", thread_id);
|
|---|
| 93 |
|
|---|
| 94 | WorkerObj* obj = [[WorkerObj alloc] init];
|
|---|
| 95 | obj->thread_id = thread_id;
|
|---|
| 96 |
|
|---|
| 97 | NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
|
|---|
| 98 | [center addObserver:obj selector:@selector(handleNotification1:) name:@"TestNotification1" object:nil];
|
|---|
| 99 | [center addObserver:obj selector:@selector(handleNotification2:) name:@"TestNotification2" object:nil];
|
|---|
| 100 | [center addObserver:obj selector:@selector(handleNotification3:) name:@"TestNotification3" object:nil];
|
|---|
| 101 |
|
|---|
| 102 | [[NSRunLoop currentRunLoop] run];
|
|---|
| 103 | [pool release];
|
|---|
| 104 | return NULL;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | @interface WorkerThread : NSObject
|
|---|
| 108 | {
|
|---|
| 109 | @public
|
|---|
| 110 | void* (*routine)(void*);
|
|---|
| 111 | void* arg;
|
|---|
| 112 | }
|
|---|
| 113 | - (void)startThread;
|
|---|
| 114 | @end
|
|---|
| 115 |
|
|---|
| 116 | @implementation WorkerThread
|
|---|
| 117 | - (void)startThread
|
|---|
| 118 | {
|
|---|
| 119 | routine(arg);
|
|---|
| 120 | }
|
|---|
| 121 | @end
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | int main2() {
|
|---|
| 125 |
|
|---|
| 126 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|---|
| 127 | [NSApplication sharedApplication];
|
|---|
| 128 |
|
|---|
| 129 | for (int i = 0; i < NUM_THREADS; i++) {
|
|---|
| 130 | for (int j = 0; i < NUM_THREADS; i++) {
|
|---|
| 131 | ports1[i][j] = nil;
|
|---|
| 132 | ports2[i][j] = nil;
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | for (int i = 0; i < NUM_THREADS; i++) {
|
|---|
| 137 | WorkerThread *thread = [[WorkerThread alloc] init];
|
|---|
| 138 | thread->routine = thread_routine;
|
|---|
| 139 | thread->arg = (void*)i;
|
|---|
| 140 | [NSThread detachNewThreadSelector:@selector(startThread) toTarget:thread withObject:nil];
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | sleep(2);
|
|---|
| 144 |
|
|---|
| 145 | NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
|
|---|
| 146 | NSNotification* notification3 = [NSNotification notificationWithName:@"TestNotification3" object:nil];
|
|---|
| 147 | [center postNotification:notification3];
|
|---|
| 148 | sleep(2);
|
|---|
| 149 |
|
|---|
| 150 | NSNotification* notification2 = [NSNotification notificationWithName:@"TestNotification2" object:nil];
|
|---|
| 151 | [center postNotification:notification2];
|
|---|
| 152 | sleep(2);
|
|---|
| 153 |
|
|---|
| 154 | NSNotification* notification1 = [NSNotification notificationWithName:@"TestNotification1" object:nil];
|
|---|
| 155 | [center postNotification:notification1];
|
|---|
| 156 |
|
|---|
| 157 | [[NSRunLoop currentRunLoop] run];
|
|---|
| 158 |
|
|---|
| 159 | [pool release];
|
|---|
| 160 | return 0;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | int main()
|
|---|
| 164 | {
|
|---|
| 165 | int result = 0;
|
|---|
| 166 |
|
|---|
| 167 | NS_DURING
|
|---|
| 168 | {
|
|---|
| 169 | result = main2();
|
|---|
| 170 | }
|
|---|
| 171 | NS_HANDLER
|
|---|
| 172 | {
|
|---|
| 173 | fprintf(stderr, "objective-c exception [%s]\n", [[localException name] cString]);
|
|---|
| 174 | }
|
|---|
| 175 | NS_ENDHANDLER
|
|---|
| 176 |
|
|---|
| 177 | return 1;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|