// // WordFileContainer.m // Duncan // // Created by Risa Mettaserikul on Thu Feb 27 2003. // Copyright (c) 2003 CSFSS. All rights reserved. // #import "WordFileContainer.h" @implementation WordFileContainer static NSLock *_wcLock; - (id)initWithName:(NSString *)newName file:(NSString*)file target:(id)newTarget { self = [self initWithName:newName]; isDirty = YES; isChanged = NO; isReady = NO; NSConditionLock *breaker = [[NSConditionLock alloc] initWithCondition:NO]; sourceFile = [file retain]; [self setTarget:newTarget]; [_dict setObject:@"Loading.." forKey:@"Counter"]; // NSLog(@"about to spawn thread %@ by, %d",string, [NSThread currentThread]); [NSThread detachNewThreadSelector: @selector(_threadLoader:) toTarget: self withObject: breaker]; // NSLog(@"main thread is locked %d",[NSThread currentThread]); [breaker lockWhenCondition:YES]; //This lock make sure that the new thread is inited. // NSLog(@"thread can lock now %d",[NSThread currentThread]); //try to register itself to an observer [breaker release]; return self; } - (void) _threadLoader:(NSConditionLock *)breaker { id conn2; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; conn2 = [self target]; NSLog(@"loader going to add, %d",[NSThread currentThread]); // conn2 = [NSConnection connectionWithReceivePort:[ports objectAtIndex:0] sendPort:[ports objectAtIndex:1]]; // [conn2 enableMultipleThreads]; [self lock]; [breaker unlockWithCondition:YES]; [conn2 registerThread]; // NSLog(@"...unlock main thread %d",[NSThread currentThread]); [self _addEntriesFromDictionary:[NSDictionary dictionaryWithContentsOfFile:sourceFile]]; [self unlock]; // NSLog(@"loader finish adding, now go sort, %d",[NSThread currentThread]); [self sortedIndexes]; // NSLog(@"loader done sorting, %d",[NSThread currentThread]); isReady = YES; [conn2 resignThread]; [pool release]; [NSThread exit]; } - (void) dealloc { NSLog(@"WordFilecontainer got dealloc"); if (sourceFile && isChanged) { NSLog(@"The dictionary is changed, update file content..."); // [dict writeToFile:sourceFile atomically:YES]; } [sourceFile release]; return [super dealloc]; } @end