/******************************************************************************************************

	BXBackgroundThread.h
	
 ******************************************************************************************************

	Author:			Peter Baumgartner		Tel:  (+4989) 649-43875
					Rosskopfstr. 20			Mail: peter@baumgartner.com
					82031 Grünwald			
					Germany					
	
					Copyright © 2006 by Peter Baumgartner - All Rights Reserved

 ******************************************************************************************************/

/******************************************************************************************************
	READ ME
 ******************************************************************************************************/

	#pragma mark READ ME

	// BXBackgroundThread is a generic class that lets you perform arbitrary selectors on
	// a background thread. Leopard supplies this feature out of the box, but if you need
	// to ship for Tiger, this is a way to do it. Use it like this:
	// 
	//   1) Create a BXBackgroundThread object and retain it somewhere.
	//   2) Start the thread.
	//   3) Send messages with the performSelector:withTarget: methods. These messages
	//      are handled asynchronously in the background thread. If you need to return  
	//		results, you can do that via performSelectorOnMainThread.
	//	 4) Stop the thread.
	//	 5) Release the BXBackgroundThread object.
	// 
	// Based on the technique described in "Notification Programming Topics" at 
	// file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Conceptual/Notifications/index.html
	// an "Communicating with Mach Ports" at
	// file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Conceptual/Multithreading/index.html

	

/******************************************************************************************************
	CONSTANTS
 ******************************************************************************************************/

	#pragma mark CONSTANTS
	
	enum BXBackgroundThreadFlushMode
	{
		kFlushSupress,
		kFlushImmediately,
		kFlushDeferred
	};
	
	
	
	#pragma mark
	
/******************************************************************************************************
	BXBackgroundThread
 ******************************************************************************************************/


	@interface BXBackgroundThread : NSObject
	{
		NSThread* _backgroundThread;
		NSMachPort* _messagePort;
		NSMutableArray* _messageQueue;
		NSLock* _messageQueueLock;
		BOOL _exitRunLoop;
		id _delegate;
		NSString* _name;
		BOOL _runningOnLeopard;
	}
	
	+ (BXBackgroundThread*) backgroundThread;
	+ (BXBackgroundThread*) backgroundThreadWithName:(NSString*)inName;
//	- (id) init;
	- (id) initWithName:(NSString*)inName;
	- (void) dealloc;
	
	 - (void) setDelegate:(id)inDelegate;
	 - (id) delegate;
	 
	 - (void) setName:(NSString*)inName;
	 - (NSString*) name;
	 
	- (void) start:(BOOL)inSync;
	- (void) stop:(BOOL)inSync;
	
	- (void) performSelector:(SEL)inSelector withTarget:(id)inTarget;
	- (void) performSelector:(SEL)inSelector withTarget:(id)inTarget withObject:(id)inObject;
	- (void) performSelector:(SEL)inSelector withTarget:(id)inTarget withObject:(id)inObject1 withObject:(id)inObject2;
	- (void) performSelector:(SEL)inSelector withTarget:(id)inTarget withObject:(id)inObject1 withObject:(id)inObject2 flush:(NSInteger)inFlushMode;

	- (void) flush;
	- (void) cancel;

	@end



/******************************************************************************************************
	BXBackgroundThreadException
 ******************************************************************************************************/

	@interface NSObject (BXBackgroundThreadException)

	- (void) handleBackgroundThreadException:(NSException*)inException;

	@end



/******************************************************************************************************/
