ftp.nice.ch/pub/next/developer/objc/threads/ThreadedApp.1.0.s.tar.gz#/ThreadedApp-1.0/ThreadedApp.h

This is ThreadedApp.h in view mode; [Download] [Up]

/*
 *  ThreadedApp:   adds support for a multi-threaded application.
 *
 *  (c) 1997 Chris Roehrig <croehrig@House.ORG>
 *
 *  This subclass of Application contains the functionality of the OpenStep
 *  NSThread object, but in a place where (arguably) it makes more sense,
 *  is cleaner, and is easier to use.
 *
 *  It also gives objects a "callback" mechanism so they can request
 *  the main App thread to invoke their methods.   This makes for a
 *  very clean synchronization mechanism with the AppKit as well as between
 *  threads and in most cases should obviate the need to use any other 
 *  synchronization mechanisms (locks, conditions, etc).
 *
 *  Only the main thread can use the AppKit, so all other threads must
 *  message the main thread to do any functions (drawing, etc) that involve
 *  the AppKit.   This service is provided by a callback mechanism via the
 *  main event loop.
 *  A subsidiary thread calls NXApp's callback:perform: method to cause
 *  it to invoke a method under the main thread of execution.  The request
 *  appears to the App as a special event which is handled through the main
 *  windowserver event loop.
 *
 *  The callback:perform: methods are non-blocking: they don't wait for the 
 *  invoked method to return.
 *  
 *  The callback:perform:wait: methods block until the main thread
 *  has executed the method and returned the result.
 *
 *  There are two possible priority levels for the way the method is
 *  invoked by the main thread (corresponding to the priority of the
 *  event that appears in the event queue):
 *   1   The base priority (NX_BASETHRESHOLD), which postpones the 
 *       callback if the App is in a modal loop (attention panel, mouse
 *       down, etc).   This can be used, for instance, if the child
 *       thread wants to bring up its own interactive panel, but doesn't
 *	 want to interrupt anything important that the user might be doing.
 *   2   The fast priority (NX_MODALRESPTHRESHOLD+1) which means the
 *       event gets processed, even if the user is in a modal loop 
 *       (mouse dragging, etc).   Use this for drawing in the background
 *       and other activities which don't disturb the user's actions.
 *       Fast priority callbacks CANNOT get events or use locks (CJRLock).
 *
 *  The ThreadedApp object uses the appDidInit: delegate method, so if your
 *  application's delegate implements an appDidInit: method, be sure and
 *  call [NXApp appDidInit:] from it.
 *
 */     
#import <appkit/Application.h>
#import <mach/cthreads.h>


@interface ThreadedApp : Application
{
}


- (void)detachNewThreadSelector:	(SEL)aSelector 
			toTarget:	(id)aTarget 
			withObject:	(id)anArgument;
/*
 *  Start the method in a new thread of execution.  The method is called
 *  as in [aTarget perform:aSelector with:anArgument].   Any thread can
 *  invoke this method.
 */


- (void)exitCurrentThread;
/*
 *  Exits the caller's thread of execution.
 */


- (void)sleepCurrentThread:(int)msec;
/*
 *  Performs a context switch and puts the current thread to sleep for
 *  msec milliseconds.   Use this as an alternative to usleep
 *  (which is not thread-safe).
 */


/*
 * The following callback methods cause a method to be executed under the
 * App's main thread of execution.  They behave similarly to Object's 
 * perform:with: method except a target is also specified.   
 * Any int-sized argument can be used and typecast to an id.
 *
 *  If a callback method is invoked from the main thread of execution,
 *  the perform: method is directly used and no messages are sent.
 *
 * Typical usage is:
 *     [NXApp callback:self perform:@selector(update:) with:self];
 */

- (id)callbackAndWaitTarget:target perform:(SEL)aSel;
- (id)callbackAndWaitTarget:target perform:(SEL)aSel with:anObject;
- (void)callbackTarget:target perform:(SEL)aSel;
- (void)callbackTarget:target perform:(SEL)aSel with:anObject;

- (id)safeCallbackAndWaitTarget:target perform:(SEL)aSel;
- (id)safeCallbackAndWaitTarget:(id)target perform:(SEL)aSel with:(id)anObject;
- (void)safeCallbackTarget:target perform:(SEL)aSel;
- (void)safeCallbackTarget:(id)target perform:(SEL)aSel with:(id)anObject;

- (id)slowCallbackAndWaitTarget:target perform:(SEL)aSel;
- (id)slowCallbackAndWaitTarget:(id)target perform:(SEL)aSel with:(id)anObject;
- (void)slowCallbackTarget:target perform:(SEL)aSel;
- (void)slowCallbackTarget:(id)target perform:(SEL)aSel with:(id)anObject;


@end


// Private methods in a category for CJRLock...
@interface ThreadedApp(CJRLock) 
- (void)lock: (mutex_t)mutex;
- (void)lock: (mutex_t)mutex whenCondition: (condition_t)cond
		withData:(volatile int *)data is:(int)value;
- (void)threadDidUnlock:sender;
@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.