@@ -67,11 +67,18 @@ /* * Nasty workaround for thread implementations which can't return a * value on join. */ +#ifdef OF_HAVE_BLOCKS + if (thread->block != nil) + thread->returnValue = [thread->block(thread->object) retain]; + else + thread->returnValue = [[thread main] retain]; +#else thread->returnValue = [[thread main] retain]; +#endif [thread handleTermination]; thread->running = OF_THREAD_WAITING_FOR_JOIN; @@ -86,10 +93,14 @@ return 0; } @implementation OFThread +#if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS) +@synthesize block; +#endif + + (void)initialize { if (self != [OFThread class]) return; @@ -104,10 +115,24 @@ + threadWithObject: (id)object { return [[[self alloc] initWithObject: object] autorelease]; } + +#ifdef OF_HAVE_BLOCKS ++ threadWithBlock: (of_thread_block_t)block +{ + return [[[self alloc] initWithBlock: block] autorelease]; +} + ++ threadWithBlock: (of_thread_block_t)block + object: (id)object +{ + return [[[self alloc] initWithBlock: block + object: object] autorelease]; +} +#endif + (void)setObject: (id)object forTLSKey: (OFTLSKey*)key { id oldObject = of_tlskey_get(key->key); @@ -241,10 +266,34 @@ @throw e; } return self; } + +#ifdef OF_HAVE_BLOCKS +- initWithBlock: (of_thread_block_t)block_ +{ + return [self initWithBlock: block_ + object: nil]; +} + +- initWithBlock: (of_thread_block_t)block_ + object: (id)object_ +{ + self = [super init]; + + @try { + block = [block_ retain]; + object = [object_ retain]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} +#endif - (id)main { @throw [OFNotImplementedException newWithClass: isa selector: _cmd];