2001-12-17 14:31:42 +00:00
|
|
|
|
/** Control of executable units within a shared virtual memory space
|
2000-04-19 12:29:17 +00:00
|
|
|
|
Copyright (C) 1996-2000 Free Software Foundation, Inc.
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
Original Author: Scott Christley <scottc@net-community.com>
|
|
|
|
|
Rewritten by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
|
|
|
|
Created: 1996
|
1999-05-10 06:45:36 +00:00
|
|
|
|
Rewritten by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
|
to add optimisations features for faster thread access.
|
2000-11-12 07:41:24 +00:00
|
|
|
|
Modified by: Nicola Pero <n.pero@mi.flashnet.it>
|
2005-02-22 11:22:44 +00:00
|
|
|
|
to add GNUstep extensions allowing to interact with threads created
|
2000-11-12 07:41:24 +00:00
|
|
|
|
by external libraries/code (eg, a Java Virtual Machine).
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
This file is part of the GNUstep Objective-C Library.
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1996-02-13 15:40:05 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
1996-02-13 15:40:05 +00:00
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1996-02-13 15:40:05 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2006-06-04 06:42:10 +00:00
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSThread class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
2005-02-22 11:22:44 +00:00
|
|
|
|
*/
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "config.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/preface.h"
|
2002-05-02 21:22:06 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
1999-06-24 19:30:29 +00:00
|
|
|
|
#include <unistd.h>
|
2002-02-20 06:42:05 +00:00
|
|
|
|
#endif
|
2003-07-17 09:00:31 +00:00
|
|
|
|
#ifdef HAVE_NANOSLEEP
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#endif
|
2007-12-27 07:02:27 +00:00
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_PTHREAD_H
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#endif
|
2004-02-17 12:55:02 +00:00
|
|
|
|
#ifdef NeXT_RUNTIME
|
|
|
|
|
#include "thr-mach.h"
|
|
|
|
|
#endif
|
2005-02-23 16:05:09 +00:00
|
|
|
|
#include <errno.h>
|
2008-09-18 08:22:53 +00:00
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __POSIX_SOURCE
|
|
|
|
|
#define NBLK_OPT O_NONBLOCK
|
|
|
|
|
#else
|
|
|
|
|
#define NBLK_OPT FNDELAY
|
|
|
|
|
#endif
|
2005-02-23 16:05:09 +00:00
|
|
|
|
|
2007-12-27 07:02:27 +00:00
|
|
|
|
#include "Foundation/NSDebug.h"
|
2003-09-30 18:19:03 +00:00
|
|
|
|
#include "Foundation/NSException.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "Foundation/NSThread.h"
|
|
|
|
|
#include "Foundation/NSLock.h"
|
|
|
|
|
#include "Foundation/NSString.h"
|
2006-10-31 07:05:46 +00:00
|
|
|
|
#include "Foundation/NSNotification.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "Foundation/NSNotificationQueue.h"
|
|
|
|
|
#include "Foundation/NSRunLoop.h"
|
|
|
|
|
#include "Foundation/NSConnection.h"
|
|
|
|
|
#include "Foundation/NSInvocation.h"
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
2006-10-09 14:00:01 +00:00
|
|
|
|
#include "GSPrivate.h"
|
2006-03-04 06:47:56 +00:00
|
|
|
|
#include "GSRunLoopCtxt.h"
|
2002-11-03 18:56:46 +00:00
|
|
|
|
|
2006-03-08 11:28:59 +00:00
|
|
|
|
@interface NSAutoreleasePool (NSThread)
|
|
|
|
|
+ (void) _endThread: (NSThread*)thread;
|
|
|
|
|
@end
|
|
|
|
|
|
2004-09-27 08:59:04 +00:00
|
|
|
|
typedef struct { @defs(NSThread) } NSThread_ivars;
|
|
|
|
|
|
2001-03-19 12:20:21 +00:00
|
|
|
|
static Class threadClass = Nil;
|
2002-08-07 13:29:31 +00:00
|
|
|
|
static NSNotificationCenter *nc = nil;
|
2001-03-19 12:20:21 +00:00
|
|
|
|
|
2005-07-08 11:48:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* This class performs a dual function ...
|
|
|
|
|
* <p>
|
|
|
|
|
* As a class, it is responsible for handling incoming events from
|
|
|
|
|
* the main runloop on a special inputFd. This consumes any bytes
|
|
|
|
|
* written to wake the main runloop.<br />
|
|
|
|
|
* During initialisation, the default runloop is set up to watch
|
|
|
|
|
* for data arriving on inputFd.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* As instances, each instance retains perform receiver and argument
|
|
|
|
|
* values as long as they are needed, and handles locking to support
|
2008-02-08 13:35:50 +00:00
|
|
|
|
* methods which want to block until an action has been performed.
|
2005-07-08 11:48:37 +00:00
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* The initialize method of this class is called before any new threads
|
|
|
|
|
* run.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
|
|
|
|
@interface GSPerformHolder : NSObject
|
|
|
|
|
{
|
|
|
|
|
id receiver;
|
|
|
|
|
id argument;
|
|
|
|
|
SEL selector;
|
|
|
|
|
NSConditionLock *lock; // Not retained.
|
2008-03-17 15:23:11 +00:00
|
|
|
|
NSArray *modes;
|
2008-03-18 05:45:05 +00:00
|
|
|
|
BOOL invalidated;
|
2005-07-08 11:48:37 +00:00
|
|
|
|
}
|
|
|
|
|
+ (GSPerformHolder*) newForReceiver: (id)r
|
|
|
|
|
argument: (id)a
|
|
|
|
|
selector: (SEL)s
|
|
|
|
|
modes: (NSArray*)m
|
|
|
|
|
lock: (NSConditionLock*)l;
|
|
|
|
|
- (void) fire;
|
2008-03-18 05:45:05 +00:00
|
|
|
|
- (void) invalidate;
|
|
|
|
|
- (BOOL) isInvalidated;
|
2008-03-17 15:23:11 +00:00
|
|
|
|
- (NSArray*) modes;
|
2005-07-08 11:48:37 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2003-07-20 06:37:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sleep until the current date/time is the specified time interval
|
|
|
|
|
* past the reference date/time.<br />
|
|
|
|
|
* Implemented as a function taking an NSTimeInterval argument in order
|
|
|
|
|
* to avoid objc messaging and object allocation/deallocation (NSDate)
|
|
|
|
|
* overheads.<br />
|
|
|
|
|
* Used to implement [NSThread+sleepUntilDate:]
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
GSSleepUntilIntervalSinceReferenceDate(NSTimeInterval when)
|
|
|
|
|
{
|
2003-12-23 17:41:38 +00:00
|
|
|
|
extern NSTimeInterval GSTimeNow(void);
|
2003-07-20 06:37:25 +00:00
|
|
|
|
NSTimeInterval delay;
|
|
|
|
|
|
|
|
|
|
// delay is always the number of seconds we still need to wait
|
|
|
|
|
delay = when - GSTimeNow();
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_NANOSLEEP
|
|
|
|
|
// Avoid any possibility of overflow by sleeping in chunks.
|
|
|
|
|
while (delay > 32768)
|
|
|
|
|
{
|
|
|
|
|
struct timespec request;
|
|
|
|
|
|
|
|
|
|
request.tv_sec = (time_t)32768;
|
|
|
|
|
request.tv_nsec = (long)0;
|
|
|
|
|
nanosleep(&request, 0);
|
|
|
|
|
delay = when - GSTimeNow();
|
|
|
|
|
}
|
|
|
|
|
if (delay > 0)
|
|
|
|
|
{
|
|
|
|
|
struct timespec request;
|
|
|
|
|
struct timespec remainder;
|
|
|
|
|
|
|
|
|
|
request.tv_sec = (time_t)delay;
|
|
|
|
|
request.tv_nsec = (long)((delay - request.tv_sec) * 1000000000);
|
|
|
|
|
remainder.tv_sec = 0;
|
|
|
|
|
remainder.tv_nsec = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* With nanosleep, we can restart the sleep after a signal by using
|
|
|
|
|
* the remainder information ... so we can be sure to sleep to the
|
|
|
|
|
* desired limit without having to re-generate the delay needed.
|
|
|
|
|
*/
|
|
|
|
|
while (nanosleep(&request, &remainder) < 0
|
|
|
|
|
&& (remainder.tv_sec > 0 || remainder.tv_nsec > 0))
|
|
|
|
|
{
|
|
|
|
|
request.tv_sec = remainder.tv_sec;
|
|
|
|
|
request.tv_nsec = remainder.tv_nsec;
|
|
|
|
|
remainder.tv_sec = 0;
|
|
|
|
|
remainder.tv_nsec = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Avoid integer overflow by breaking up long sleeps.
|
|
|
|
|
*/
|
|
|
|
|
while (delay > 30.0*60.0)
|
|
|
|
|
{
|
|
|
|
|
// sleep 30 minutes
|
2005-10-11 19:09:26 +00:00
|
|
|
|
#if defined(__MINGW32__)
|
2003-07-20 06:37:25 +00:00
|
|
|
|
Sleep (30*60*1000);
|
|
|
|
|
#else
|
|
|
|
|
sleep (30*60);
|
|
|
|
|
#endif
|
|
|
|
|
delay = when - GSTimeNow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* sleeping may return early because of signals, so we need to re-calculate
|
|
|
|
|
* the required delay and check to see if we need to sleep again.
|
|
|
|
|
*/
|
|
|
|
|
while (delay > 0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_USLEEP
|
|
|
|
|
usleep ((int)(delay*1000000));
|
|
|
|
|
#else
|
2005-10-11 19:09:26 +00:00
|
|
|
|
#if defined(__MINGW32__)
|
2003-07-20 06:37:25 +00:00
|
|
|
|
Sleep (delay*1000);
|
|
|
|
|
#else
|
|
|
|
|
sleep ((int)delay);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
delay = when - GSTimeNow();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-03 18:56:46 +00:00
|
|
|
|
static NSArray *
|
2003-12-23 17:41:38 +00:00
|
|
|
|
commonModes(void)
|
2002-11-03 18:56:46 +00:00
|
|
|
|
{
|
|
|
|
|
static NSArray *modes = nil;
|
|
|
|
|
|
|
|
|
|
if (modes == nil)
|
|
|
|
|
{
|
|
|
|
|
[gnustep_global_lock lock];
|
|
|
|
|
if (modes == nil)
|
|
|
|
|
{
|
|
|
|
|
Class c = NSClassFromString(@"NSApplication");
|
|
|
|
|
SEL s = @selector(allRunLoopModes);
|
|
|
|
|
|
|
|
|
|
if (c != 0 && [c respondsToSelector: s])
|
|
|
|
|
{
|
|
|
|
|
modes = RETAIN([c performSelector: s]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
modes = [[NSArray alloc] initWithObjects:
|
|
|
|
|
NSDefaultRunLoopMode, NSConnectionReplyMode, nil];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[gnustep_global_lock unlock];
|
|
|
|
|
}
|
|
|
|
|
return modes;
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-31 22:39:16 +00:00
|
|
|
|
#if !defined(HAVE_OBJC_THREAD_ADD) && !defined(NeXT_RUNTIME)
|
2000-11-12 07:41:24 +00:00
|
|
|
|
/* We need to access these private vars in the objc runtime - because
|
|
|
|
|
the objc runtime's API is not enough powerful for the GNUstep
|
|
|
|
|
extensions we want to add. */
|
2001-03-12 02:52:16 +00:00
|
|
|
|
extern objc_mutex_t __objc_runtime_mutex;
|
|
|
|
|
extern int __objc_runtime_threads_alive;
|
2000-11-12 07:41:24 +00:00
|
|
|
|
extern int __objc_is_multi_threaded;
|
|
|
|
|
|
2003-12-23 17:41:38 +00:00
|
|
|
|
inline static void objc_thread_add (void)
|
2000-11-12 07:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
objc_mutex_lock(__objc_runtime_mutex);
|
|
|
|
|
__objc_is_multi_threaded = 1;
|
|
|
|
|
__objc_runtime_threads_alive++;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
objc_mutex_unlock(__objc_runtime_mutex);
|
2000-11-12 07:41:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-12-23 17:41:38 +00:00
|
|
|
|
inline static void objc_thread_remove (void)
|
2000-11-12 07:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
objc_mutex_lock(__objc_runtime_mutex);
|
|
|
|
|
__objc_runtime_threads_alive--;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
objc_mutex_unlock(__objc_runtime_mutex);
|
2000-11-12 07:41:24 +00:00
|
|
|
|
}
|
2001-03-10 21:36:03 +00:00
|
|
|
|
#endif /* not HAVE_OBJC_THREAD_ADD */
|
2000-11-12 07:41:24 +00:00
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* Flag indicating whether the objc runtime ever went multi-threaded.
|
|
|
|
|
*/
|
|
|
|
|
static BOOL entered_multi_threaded_state = NO;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* Default thread.
|
|
|
|
|
*/
|
|
|
|
|
static NSThread *defaultThread = nil;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
2002-05-03 08:17:04 +00:00
|
|
|
|
/**
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* <p>
|
|
|
|
|
* This function is a GNUstep extension. It pretty much
|
|
|
|
|
* duplicates the functionality of [NSThread +currentThread]
|
|
|
|
|
* but is more efficient and is used internally throughout
|
|
|
|
|
* GNUstep.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* Returns the current thread. Could perhaps return <code>nil</code>
|
|
|
|
|
* if executing a thread that was started outside the GNUstep
|
|
|
|
|
* environment and not registered (this should not happen in a
|
|
|
|
|
* well-coded application).
|
|
|
|
|
* </p>
|
2000-04-19 12:29:17 +00:00
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
inline NSThread*
|
2003-12-23 17:41:38 +00:00
|
|
|
|
GSCurrentThread(void)
|
1999-04-19 14:29:52 +00:00
|
|
|
|
{
|
2002-05-03 08:17:04 +00:00
|
|
|
|
NSThread *t;
|
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
if (entered_multi_threaded_state == NO)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* If the NSThread class has been initialized, we will have a default
|
|
|
|
|
* thread set up - otherwise we must make sure the class is initialised.
|
|
|
|
|
*/
|
|
|
|
|
if (defaultThread == nil)
|
|
|
|
|
{
|
2002-05-03 08:17:04 +00:00
|
|
|
|
t = [NSThread currentThread];
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-05-03 08:17:04 +00:00
|
|
|
|
t = defaultThread;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-05-03 08:17:04 +00:00
|
|
|
|
t = (NSThread*)objc_thread_get_data();
|
|
|
|
|
if (t == nil)
|
|
|
|
|
{
|
2003-09-30 18:57:49 +00:00
|
|
|
|
fprintf(stderr,
|
2003-12-23 17:41:38 +00:00
|
|
|
|
"ALERT ... GSCurrentThread() ... objc_thread_get_data() call returned nil!\n"
|
|
|
|
|
"Your application MUST call GSRegisterCurrentThread() before attempting to\n"
|
|
|
|
|
"use any GNUstep code from a thread other than the main GNUstep thread.\n");
|
2002-05-03 08:17:04 +00:00
|
|
|
|
fflush(stderr); // Needed for windoze
|
|
|
|
|
}
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
2002-05-03 08:17:04 +00:00
|
|
|
|
return t;
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-25 14:49:05 +00:00
|
|
|
|
typedef struct { @defs(NSThread) } *TInfo;
|
|
|
|
|
|
2002-05-03 08:17:04 +00:00
|
|
|
|
/**
|
2004-09-27 08:59:04 +00:00
|
|
|
|
* Fast access function for thread dictionary of current thread.<br />
|
2004-11-03 13:20:39 +00:00
|
|
|
|
* If there is no dictionary, creates the dictionary.
|
2000-04-19 12:29:17 +00:00
|
|
|
|
*/
|
1999-04-19 14:29:52 +00:00
|
|
|
|
NSMutableDictionary*
|
2002-10-30 07:45:59 +00:00
|
|
|
|
GSDictionaryForThread(NSThread *t)
|
1999-04-19 14:29:52 +00:00
|
|
|
|
{
|
2002-10-30 07:45:59 +00:00
|
|
|
|
if (t == nil)
|
|
|
|
|
{
|
|
|
|
|
t = GSCurrentThread();
|
|
|
|
|
}
|
|
|
|
|
if (t == nil)
|
2002-05-03 08:17:04 +00:00
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
else
|
2000-04-19 12:29:17 +00:00
|
|
|
|
{
|
2007-11-25 14:49:05 +00:00
|
|
|
|
NSMutableDictionary *dict = ((TInfo)t)->_thread_dictionary;
|
2002-05-03 08:17:04 +00:00
|
|
|
|
|
2004-11-03 13:20:39 +00:00
|
|
|
|
if (dict == nil)
|
2002-05-03 08:17:04 +00:00
|
|
|
|
{
|
2002-10-30 07:45:59 +00:00
|
|
|
|
dict = [t threadDictionary];
|
2002-05-03 08:17:04 +00:00
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
return dict;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
1999-04-19 14:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-10-30 07:45:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Fast access function for thread dictionary of current thread.
|
|
|
|
|
*/
|
|
|
|
|
NSMutableDictionary*
|
2003-12-23 17:41:38 +00:00
|
|
|
|
GSCurrentThreadDictionary(void)
|
2002-10-30 07:45:59 +00:00
|
|
|
|
{
|
|
|
|
|
return GSDictionaryForThread(nil);
|
|
|
|
|
}
|
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* Callback function so send notifications on becoming multi-threaded.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
2003-12-23 17:41:38 +00:00
|
|
|
|
gnustep_base_thread_callback(void)
|
1998-05-29 15:25:41 +00:00
|
|
|
|
{
|
1999-06-22 15:06:21 +00:00
|
|
|
|
/*
|
2003-09-30 18:19:03 +00:00
|
|
|
|
* Protect this function with locking ... to avoid any possibility
|
|
|
|
|
* of multiple threads registering with the system simultaneously,
|
|
|
|
|
* and so that all NSWillBecomeMultiThreadedNotifications are sent
|
|
|
|
|
* out before any second thread can interfere with anything.
|
1999-06-22 15:06:21 +00:00
|
|
|
|
*/
|
2000-04-19 12:29:17 +00:00
|
|
|
|
if (entered_multi_threaded_state == NO)
|
1998-05-29 15:25:41 +00:00
|
|
|
|
{
|
2003-09-30 18:19:03 +00:00
|
|
|
|
[gnustep_global_lock lock];
|
|
|
|
|
if (entered_multi_threaded_state == NO)
|
2002-08-07 13:29:31 +00:00
|
|
|
|
{
|
2004-03-12 16:51:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* For apple compatibility ... and to make things easier for
|
|
|
|
|
* code called indirectly within a will-become-multi-threaded
|
|
|
|
|
* notification handler, we set the flag to say we are multi
|
|
|
|
|
* threaded BEFORE sending the notifications.
|
|
|
|
|
*/
|
|
|
|
|
entered_multi_threaded_state = YES;
|
2003-09-30 18:19:03 +00:00
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
[GSPerformHolder class]; // Force initialization
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Post a notification if this is the first new thread
|
|
|
|
|
* to be created.
|
|
|
|
|
* Won't work properly if threads are not all created
|
|
|
|
|
* by this class, but it's better than nothing.
|
|
|
|
|
*/
|
|
|
|
|
if (nc == nil)
|
|
|
|
|
{
|
2004-11-10 11:45:08 +00:00
|
|
|
|
nc = RETAIN([NSNotificationCenter defaultCenter]);
|
2003-09-30 18:19:03 +00:00
|
|
|
|
}
|
|
|
|
|
[nc postNotificationName: NSWillBecomeMultiThreadedNotification
|
|
|
|
|
object: nil
|
|
|
|
|
userInfo: nil];
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2003-09-30 18:57:49 +00:00
|
|
|
|
fprintf(stderr,
|
2003-12-23 17:41:38 +00:00
|
|
|
|
"ALERT ... exception while becoming multi-threaded ... system may not be\n"
|
|
|
|
|
"properly initialised.\n");
|
2003-09-30 18:57:49 +00:00
|
|
|
|
fflush(stderr);
|
2003-09-30 18:19:03 +00:00
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
2002-08-07 13:29:31 +00:00
|
|
|
|
}
|
2003-09-30 18:19:03 +00:00
|
|
|
|
[gnustep_global_lock unlock];
|
1998-05-29 15:25:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1996-07-15 18:41:44 +00:00
|
|
|
|
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
@implementation NSThread
|
1996-02-13 15:40:05 +00:00
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
+ (NSArray*) callStackReturnAddresses
|
|
|
|
|
{
|
2007-12-03 14:13:57 +00:00
|
|
|
|
NSMutableArray *stack = GSPrivateStackAddresses();
|
|
|
|
|
|
|
|
|
|
return stack;
|
2007-11-25 14:25:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
+ (NSThread*) currentThread
|
|
|
|
|
{
|
2003-07-25 09:27:44 +00:00
|
|
|
|
NSThread *t = nil;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
|
if (entered_multi_threaded_state == NO)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* The NSThread class has been initialized - so we will have a default
|
2003-07-25 09:27:44 +00:00
|
|
|
|
* thread set up unless the default thread subsequently exited.
|
2000-08-07 22:00:31 +00:00
|
|
|
|
*/
|
2002-05-03 08:17:04 +00:00
|
|
|
|
t = defaultThread;
|
2000-08-07 22:00:31 +00:00
|
|
|
|
}
|
2003-07-25 09:27:44 +00:00
|
|
|
|
if (t == nil)
|
2000-08-07 22:00:31 +00:00
|
|
|
|
{
|
2002-05-03 08:17:04 +00:00
|
|
|
|
t = (NSThread*)objc_thread_get_data();
|
|
|
|
|
if (t == nil)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "ALERT ... [NSThread +currentThread] ... the "
|
|
|
|
|
"objc_thread_get_data() call returned nil!");
|
|
|
|
|
fflush(stderr); // Needed for windoze
|
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
|
}
|
2002-05-03 08:17:04 +00:00
|
|
|
|
return t;
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
}
|
1996-03-19 01:45:25 +00:00
|
|
|
|
|
1999-05-10 06:45:36 +00:00
|
|
|
|
+ (void) detachNewThreadSelector: (SEL)aSelector
|
|
|
|
|
toTarget: (id)aTarget
|
|
|
|
|
withObject: (id)anArgument
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
{
|
2000-04-19 12:29:17 +00:00
|
|
|
|
NSThread *thread;
|
|
|
|
|
|
1999-06-22 15:06:21 +00:00
|
|
|
|
/*
|
2000-04-19 12:29:17 +00:00
|
|
|
|
* Create the new thread.
|
1999-06-22 15:06:21 +00:00
|
|
|
|
*/
|
2000-04-19 12:29:17 +00:00
|
|
|
|
thread = (NSThread*)NSAllocateObject(self, 0, NSDefaultMallocZone());
|
2007-11-25 14:25:26 +00:00
|
|
|
|
thread = [thread initWithTarget: aTarget
|
|
|
|
|
selector: aSelector
|
|
|
|
|
object: anArgument];
|
1999-07-14 16:28:43 +00:00
|
|
|
|
|
2007-12-24 17:31:52 +00:00
|
|
|
|
[thread start];
|
|
|
|
|
RELEASE(thread);
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) exit
|
|
|
|
|
{
|
|
|
|
|
NSThread *t;
|
|
|
|
|
|
|
|
|
|
t = GSCurrentThread();
|
|
|
|
|
if (t->_active == YES)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Set the thread to be inactive to avoid any possibility of recursion.
|
|
|
|
|
*/
|
|
|
|
|
t->_active = NO;
|
2008-03-18 05:45:05 +00:00
|
|
|
|
t->_finished = YES;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Let observers know this thread is exiting.
|
|
|
|
|
*/
|
2002-08-07 13:29:31 +00:00
|
|
|
|
if (nc == nil)
|
|
|
|
|
{
|
2004-11-10 11:45:08 +00:00
|
|
|
|
nc = RETAIN([NSNotificationCenter defaultCenter]);
|
2002-08-07 13:29:31 +00:00
|
|
|
|
}
|
|
|
|
|
[nc postNotificationName: NSThreadWillExitNotification
|
|
|
|
|
object: t
|
|
|
|
|
userInfo: nil];
|
2000-04-19 12:29:17 +00:00
|
|
|
|
|
2008-03-18 05:45:05 +00:00
|
|
|
|
[(GSRunLoopThreadInfo*)t->_runLoopInfo invalidate];
|
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* destroy the thread object.
|
|
|
|
|
*/
|
|
|
|
|
DESTROY(t);
|
|
|
|
|
|
2000-11-22 08:41:07 +00:00
|
|
|
|
objc_thread_set_data (NULL);
|
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* Tell the runtime to exit the thread
|
|
|
|
|
*/
|
|
|
|
|
objc_thread_exit();
|
|
|
|
|
}
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* Class initialization
|
|
|
|
|
*/
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [NSThread class])
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* The objc runtime calls this callback AFTER creating a new thread -
|
|
|
|
|
* which is not correct for us, but does at least mean that we can tell
|
|
|
|
|
* if we have become multi-threaded due to a call to the runtime directly
|
|
|
|
|
* rather than via the NSThread class.
|
|
|
|
|
*/
|
|
|
|
|
objc_set_thread_callback(gnustep_base_thread_callback);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Ensure that the default thread exists.
|
|
|
|
|
*/
|
|
|
|
|
defaultThread
|
|
|
|
|
= (NSThread*)NSAllocateObject(self, 0, NSDefaultMallocZone());
|
2007-11-25 14:25:26 +00:00
|
|
|
|
defaultThread = [defaultThread init];
|
2000-04-19 12:29:17 +00:00
|
|
|
|
defaultThread->_active = YES;
|
|
|
|
|
objc_thread_set_data(defaultThread);
|
2001-03-19 12:20:21 +00:00
|
|
|
|
threadClass = self;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
|
2008-03-18 05:45:05 +00:00
|
|
|
|
+ (BOOL) isMainThread
|
|
|
|
|
{
|
|
|
|
|
return (GSCurrentThread() == defaultThread ? YES : NO);
|
|
|
|
|
}
|
|
|
|
|
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
+ (BOOL) isMultiThreaded
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
return entered_multi_threaded_state;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
+ (NSThread*) mainThread
|
|
|
|
|
{
|
|
|
|
|
return defaultThread;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-27 17:02:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Set the priority of the current thread. This is a value in the
|
|
|
|
|
* range 0.0 (lowest) to 1.0 (highest) which is mapped to the underlying
|
|
|
|
|
* system priorities. The current gnu objc runtime supports three
|
|
|
|
|
* priority levels which you can obtain using values of 0.0, 0.5, and 1.0
|
|
|
|
|
*/
|
|
|
|
|
+ (void) setThreadPriority: (double)pri
|
|
|
|
|
{
|
|
|
|
|
int p;
|
|
|
|
|
|
|
|
|
|
if (pri <= 0.3)
|
|
|
|
|
p = OBJC_THREAD_LOW_PRIORITY;
|
|
|
|
|
else if (pri <= 0.6)
|
|
|
|
|
p = OBJC_THREAD_BACKGROUND_PRIORITY;
|
|
|
|
|
else
|
|
|
|
|
p = OBJC_THREAD_INTERACTIVE_PRIORITY;
|
|
|
|
|
|
|
|
|
|
objc_thread_set_priority(p);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
+ (void) sleepForTimeInterval: (NSTimeInterval)ti
|
|
|
|
|
{
|
|
|
|
|
if (ti > 0.0)
|
|
|
|
|
{
|
|
|
|
|
GSSleepUntilIntervalSinceReferenceDate(
|
|
|
|
|
[NSDate timeIntervalSinceReferenceDate] + ti);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-27 17:02:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Delaying a thread ... pause until the specified date.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
*/
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
+ (void) sleepUntilDate: (NSDate*)date
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2003-07-20 06:37:25 +00:00
|
|
|
|
GSSleepUntilIntervalSinceReferenceDate([date timeIntervalSinceReferenceDate]);
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-20 06:37:25 +00:00
|
|
|
|
|
2002-08-27 17:02:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return the priority of the current thread.
|
|
|
|
|
*/
|
|
|
|
|
+ (double) threadPriority
|
|
|
|
|
{
|
|
|
|
|
int p = objc_thread_get_priority();
|
|
|
|
|
|
|
|
|
|
if (p == OBJC_THREAD_LOW_PRIORITY)
|
|
|
|
|
return 0.0;
|
|
|
|
|
else if (p == OBJC_THREAD_BACKGROUND_PRIORITY)
|
|
|
|
|
return 0.5;
|
|
|
|
|
else if (p == OBJC_THREAD_INTERACTIVE_PRIORITY)
|
|
|
|
|
return 1.0;
|
|
|
|
|
else
|
|
|
|
|
return 0.0; // Unknown.
|
|
|
|
|
}
|
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Thread instance methods.
|
|
|
|
|
*/
|
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
- (void) cancel
|
|
|
|
|
{
|
|
|
|
|
_cancelled = YES;
|
|
|
|
|
}
|
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
- (void) dealloc
|
1996-02-13 15:40:05 +00:00
|
|
|
|
{
|
2000-04-19 12:29:17 +00:00
|
|
|
|
if (_active == YES)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Deallocating an active thread without [+exit]!"];
|
|
|
|
|
}
|
2008-03-17 15:23:11 +00:00
|
|
|
|
if (_runLoopInfo != 0)
|
|
|
|
|
{
|
|
|
|
|
GSRunLoopThreadInfo *info = (GSRunLoopThreadInfo*)_runLoopInfo;
|
|
|
|
|
|
|
|
|
|
_runLoopInfo = 0;
|
|
|
|
|
[info release];
|
|
|
|
|
}
|
2000-04-19 12:29:17 +00:00
|
|
|
|
DESTROY(_thread_dictionary);
|
|
|
|
|
DESTROY(_target);
|
|
|
|
|
DESTROY(_arg);
|
2007-11-25 14:25:26 +00:00
|
|
|
|
DESTROY(_name);
|
2006-01-19 06:15:27 +00:00
|
|
|
|
if (_autorelease_vars.pool_cache != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSAutoreleasePool _endThread: self];
|
|
|
|
|
}
|
2000-11-22 08:41:07 +00:00
|
|
|
|
|
2001-03-17 10:18:09 +00:00
|
|
|
|
if (_thread_dictionary != nil)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Try again to get rid of thread dictionary.
|
|
|
|
|
*/
|
|
|
|
|
DESTROY(_thread_dictionary);
|
2006-01-19 06:15:27 +00:00
|
|
|
|
if (_autorelease_vars.pool_cache != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSAutoreleasePool _endThread: self];
|
|
|
|
|
}
|
2001-03-17 10:18:09 +00:00
|
|
|
|
if (_thread_dictionary != nil)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Oops - leak - thread dictionary is %@", _thread_dictionary);
|
2006-01-19 06:15:27 +00:00
|
|
|
|
if (_autorelease_vars.pool_cache != 0)
|
|
|
|
|
{
|
|
|
|
|
[NSAutoreleasePool _endThread: self];
|
|
|
|
|
}
|
2001-03-17 10:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-12-25 20:31:24 +00:00
|
|
|
|
DESTROY(_gcontext);
|
2003-07-25 09:27:44 +00:00
|
|
|
|
if (self == defaultThread)
|
|
|
|
|
{
|
|
|
|
|
defaultThread = nil;
|
|
|
|
|
}
|
2000-04-19 12:29:17 +00:00
|
|
|
|
NSDeallocateObject(self);
|
2006-06-04 06:42:10 +00:00
|
|
|
|
GSNOSUPERDEALLOC;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
1996-03-19 01:45:25 +00:00
|
|
|
|
|
2000-04-19 12:29:17 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
2007-11-25 14:25:26 +00:00
|
|
|
|
return self;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
- (id) initWithTarget: (id)aTarget
|
|
|
|
|
selector: (SEL)aSelector
|
|
|
|
|
object: (id)anArgument
|
2000-04-19 12:29:17 +00:00
|
|
|
|
{
|
|
|
|
|
/* initialize our ivars. */
|
2007-11-25 14:25:26 +00:00
|
|
|
|
_selector = aSelector;
|
|
|
|
|
_target = RETAIN(aTarget);
|
|
|
|
|
_arg = RETAIN(anArgument);
|
2000-04-19 12:29:17 +00:00
|
|
|
|
_thread_dictionary = nil; // Initialize this later only when needed
|
|
|
|
|
_exception_handler = NULL;
|
2007-11-25 14:25:26 +00:00
|
|
|
|
_cancelled = NO;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
_active = NO;
|
2008-03-18 05:45:05 +00:00
|
|
|
|
_finished = NO;
|
2007-11-25 14:25:26 +00:00
|
|
|
|
_name = nil;
|
2000-04-19 12:29:17 +00:00
|
|
|
|
init_autorelease_thread_vars(&_autorelease_vars);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
(NSBecomingMultiThreaded, NSThreadExiting): Initialize the
notification strings as static strings, not in +initialize.
(thread_id_2_nsthread): Renamed from THREAD_LIST. Keep the collection
of NSThread's as a maptable, not a NSArray that takes linear time to
search!
(thread_lock): Renamed from THREAD_LIST_LOCK.
(entered_multi_threaded_state): Renamed from ENTERED_MULTI-THREADED_STATE.
([NSThread +initialize]): Don't initialize notification strings here.
Don't autorelease the lock!
([NSThread -init]): Initialize _thread_autorelease_pool. Set our
thread data to self, for easy, efficient access to this NSThread
object later. Put ourselves in the thread collection here, not in
+detach...
([NSThread +currentThread]): This will be called often and needs to be
fast. Reimplemented so we don't have to acquire a lock and step
through an NSArray of threads; instead, just look ourselves up with
the objc_thread_get_data(), and furthermore, no lock required.
([NSThread +detachNewThreadSelector:toTarget:withObject:]): Avoid race
condition, don't create new NSThread object here.
([NSThread +sleepUntilDate:]): Call -notImplemented:.
([NSThread +exit]): Properly post NSThreadExiting notification, making
sure not to hold the lock while we do so. Get the NSThread object
efficiently.
([NSThread -threadId]): Removed unnecessary private method.
([NSThread -setThreadId]): Likewise.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1530 72102866-910b-0410-8b05-ffd578937521
1996-05-13 15:53:36 +00:00
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
- (BOOL) isCancelled
|
|
|
|
|
{
|
|
|
|
|
return _cancelled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isExecuting
|
|
|
|
|
{
|
|
|
|
|
return _active;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-18 05:45:05 +00:00
|
|
|
|
- (BOOL) isFinished
|
|
|
|
|
{
|
|
|
|
|
return _finished;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
- (BOOL) isMainThread
|
|
|
|
|
{
|
|
|
|
|
return (self == defaultThread ? YES : NO);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-25 14:49:05 +00:00
|
|
|
|
- (void) main
|
|
|
|
|
{
|
2007-12-24 17:31:52 +00:00
|
|
|
|
if (_active == NO)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"[%@-$@] called on inactive thread",
|
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
if (objc_thread_get_data() != nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"[%@-$@] called on running thread",
|
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-27 07:02:27 +00:00
|
|
|
|
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
|
|
|
|
|
if (_stackSize > 0)
|
|
|
|
|
{
|
|
|
|
|
struct rlimit rl;
|
|
|
|
|
|
|
|
|
|
rl.rlim_cur = _stackSize;
|
|
|
|
|
rl.rlim_max = _stackSize;
|
|
|
|
|
if (setrlimit(RLIMIT_STACK, &rl) < 0)
|
|
|
|
|
{
|
|
|
|
|
NSDebugMLog(@"Unable to set thread stack size to %u: %@",
|
|
|
|
|
_stackSize, [NSError _last]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-12-24 17:31:52 +00:00
|
|
|
|
/*
|
|
|
|
|
* We are running in the new thread - so we store ourself in the thread
|
|
|
|
|
* dictionary and release ourself - thus, when the thread exits, we will
|
|
|
|
|
* be deallocated cleanly.
|
|
|
|
|
*/
|
|
|
|
|
objc_thread_set_data(self);
|
|
|
|
|
|
2007-12-27 07:02:27 +00:00
|
|
|
|
#if defined(PTHREAD_JOINABLE)
|
|
|
|
|
/* Hack to work around the fact that
|
|
|
|
|
* some versions of the objective-c
|
|
|
|
|
* library fail to create the thread detached.
|
|
|
|
|
* We should really do this only in such cases.
|
|
|
|
|
*/
|
|
|
|
|
pthread_detach(pthread_self());
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-12-24 17:31:52 +00:00
|
|
|
|
/*
|
|
|
|
|
* Let observers know a new thread is starting.
|
|
|
|
|
*/
|
|
|
|
|
if (nc == nil)
|
|
|
|
|
{
|
|
|
|
|
nc = RETAIN([NSNotificationCenter defaultCenter]);
|
|
|
|
|
}
|
|
|
|
|
[nc postNotificationName: NSThreadDidStartNotification
|
|
|
|
|
object: self
|
|
|
|
|
userInfo: nil];
|
|
|
|
|
|
2007-11-25 14:49:05 +00:00
|
|
|
|
[_target performSelector: _selector withObject: _arg];
|
2007-12-24 17:31:52 +00:00
|
|
|
|
|
|
|
|
|
[NSThread exit];
|
2007-11-25 14:49:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-25 14:25:26 +00:00
|
|
|
|
- (NSString*) name
|
|
|
|
|
{
|
|
|
|
|
return _name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setName: (NSString*)aName
|
|
|
|
|
{
|
|
|
|
|
ASSIGN(_name, aName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setStackSize: (unsigned)stackSize
|
|
|
|
|
{
|
2007-12-27 07:02:27 +00:00
|
|
|
|
_stackSize = stackSize;
|
|
|
|
|
#if !defined(HAVE_SETRLIMIT) || !defined(RLIMIT_STACK)
|
|
|
|
|
GSOnceMLog(@"Warning ... -setStackSize: not implemented on this system");
|
|
|
|
|
#endif
|
2007-11-25 14:25:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) stackSize
|
|
|
|
|
{
|
|
|
|
|
return _stackSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) start
|
2000-04-19 12:29:17 +00:00
|
|
|
|
{
|
2007-12-24 20:51:19 +00:00
|
|
|
|
if (_active == YES)
|
2002-08-07 13:29:31 +00:00
|
|
|
|
{
|
2007-12-24 20:51:19 +00:00
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"[%@-$@] called on active thread",
|
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
if (_cancelled == YES)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"[%@-$@] called on cancelled thread",
|
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
2008-03-18 05:45:05 +00:00
|
|
|
|
if (_finished == YES)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"[%@-$@] called on finished thread",
|
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
2000-11-12 07:41:24 +00:00
|
|
|
|
|
2007-12-24 20:51:19 +00:00
|
|
|
|
/* Make sure the notification is posted BEFORE the new thread starts.
|
|
|
|
|
*/
|
|
|
|
|
gnustep_base_thread_callback();
|
2007-12-24 17:31:52 +00:00
|
|
|
|
|
2007-12-24 20:51:19 +00:00
|
|
|
|
/* The thread must persist until it finishes executing.
|
|
|
|
|
*/
|
|
|
|
|
RETAIN(self);
|
2007-12-24 17:31:52 +00:00
|
|
|
|
|
2007-12-24 20:51:19 +00:00
|
|
|
|
/* Mark the thread as active whiul it's running.
|
|
|
|
|
*/
|
|
|
|
|
_active = YES;
|
|
|
|
|
|
2008-01-06 08:56:59 +00:00
|
|
|
|
errno = 0;
|
2007-12-24 20:51:19 +00:00
|
|
|
|
if (objc_thread_detach(@selector(main), self, nil) == NULL)
|
|
|
|
|
{
|
|
|
|
|
_active = NO;
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
2008-01-06 08:56:59 +00:00
|
|
|
|
format: @"Unable to detach thread (last error %@)",
|
2007-12-25 14:13:18 +00:00
|
|
|
|
[NSError _last]];
|
2007-12-24 17:31:52 +00:00
|
|
|
|
}
|
2000-04-19 12:29:17 +00:00
|
|
|
|
}
|
1996-03-19 01:45:25 +00:00
|
|
|
|
|
2002-08-27 17:02:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return the thread dictionary. This dictionary can be used to store
|
|
|
|
|
* arbitrary thread specific data.<br />
|
2000-04-19 12:29:17 +00:00
|
|
|
|
* NB. This cannot be autoreleased, since we cannot be sure that the
|
|
|
|
|
* autorelease pool for the thread will continue to exist for the entire
|
|
|
|
|
* life of the thread!
|
|
|
|
|
*/
|
|
|
|
|
- (NSMutableDictionary*) threadDictionary
|
|
|
|
|
{
|
2001-03-17 10:18:09 +00:00
|
|
|
|
if (_thread_dictionary == nil)
|
2000-04-19 12:29:17 +00:00
|
|
|
|
{
|
|
|
|
|
_thread_dictionary = [NSMutableDictionary new];
|
|
|
|
|
}
|
|
|
|
|
return _thread_dictionary;
|
1996-02-13 15:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-19 01:45:25 +00:00
|
|
|
|
@end
|
2000-04-19 12:29:17 +00:00
|
|
|
|
|
2002-10-30 07:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
@implementation GSRunLoopThreadInfo
|
|
|
|
|
- (void) addPerformer: (id)performer
|
2002-11-02 16:53:48 +00:00
|
|
|
|
{
|
2008-03-17 15:23:11 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
[performers addObject: performer];
|
2005-10-11 19:09:26 +00:00
|
|
|
|
#if defined(__MINGW32__)
|
2005-02-23 16:05:09 +00:00
|
|
|
|
if (SetEvent(event) == 0)
|
|
|
|
|
{
|
2006-10-20 10:56:27 +00:00
|
|
|
|
NSLog(@"Set event failed - %@", [NSError _last]);
|
2005-02-23 16:05:09 +00:00
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
if (write(outputFd, "0", 1) != 1)
|
|
|
|
|
{
|
2006-10-20 10:56:27 +00:00
|
|
|
|
NSLog(@"Write to pipe failed - %@", [NSError _last]);
|
2005-02-23 16:05:09 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2008-09-18 08:22:53 +00:00
|
|
|
|
[lock unlock];
|
2008-03-17 15:23:11 +00:00
|
|
|
|
}
|
2005-02-23 16:05:09 +00:00
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2008-03-18 05:45:05 +00:00
|
|
|
|
[self invalidate];
|
2008-03-17 15:23:11 +00:00
|
|
|
|
DESTROY(lock);
|
|
|
|
|
DESTROY(loop);
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
2002-11-02 16:53:48 +00:00
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
if ((event = CreateEvent(NULL, TRUE, FALSE, NULL)) == INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Failed to create event to handle perform in thread"];
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
|
|
if (pipe(fd) == 0)
|
|
|
|
|
{
|
2008-09-18 08:22:53 +00:00
|
|
|
|
int e;
|
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
inputFd = fd[0];
|
|
|
|
|
outputFd = fd[1];
|
2008-09-18 08:22:53 +00:00
|
|
|
|
if ((e = fcntl(inputFd, F_GETFL, 0)) >= 0)
|
|
|
|
|
{
|
|
|
|
|
e |= NBLK_OPT;
|
|
|
|
|
if (fcntl(inputFd, F_SETFL, e) < 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Failed to set non block flag for perform in thread"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Failed to get non block flag for perform in thread"];
|
|
|
|
|
}
|
2008-03-17 15:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Failed to create pipe to handle perform in thread"];
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
lock = [NSLock new];
|
|
|
|
|
performers = [NSMutableArray new];
|
|
|
|
|
return self;
|
2002-11-02 16:53:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-18 05:45:05 +00:00
|
|
|
|
- (void) invalidate
|
|
|
|
|
{
|
|
|
|
|
[lock lock];
|
|
|
|
|
[performers makeObjectsPerformSelector: @selector(invalidate)];
|
|
|
|
|
[performers removeAllObjects];
|
2008-03-18 05:55:32 +00:00
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
if (event != INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(event);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
if (inputFd >= 0)
|
|
|
|
|
{
|
|
|
|
|
close(inputFd);
|
|
|
|
|
inputFd = -1;
|
|
|
|
|
}
|
|
|
|
|
if (outputFd >= 0)
|
|
|
|
|
{
|
|
|
|
|
close(outputFd);
|
|
|
|
|
outputFd = -1;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2008-09-18 08:22:53 +00:00
|
|
|
|
[lock unlock];
|
2008-03-18 05:45:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
- (void) fire
|
2002-11-02 16:53:48 +00:00
|
|
|
|
{
|
2005-02-23 16:05:09 +00:00
|
|
|
|
NSArray *toDo;
|
2003-04-06 07:14:50 +00:00
|
|
|
|
unsigned int i;
|
|
|
|
|
unsigned int c;
|
2002-11-03 18:56:46 +00:00
|
|
|
|
|
2008-09-18 08:22:53 +00:00
|
|
|
|
[lock lock];
|
2005-10-11 19:09:26 +00:00
|
|
|
|
#if defined(__MINGW32__)
|
2008-03-18 05:55:32 +00:00
|
|
|
|
if (event != INVALID_HANDLE_VALUE)
|
2005-02-23 16:05:09 +00:00
|
|
|
|
{
|
2008-03-18 05:55:32 +00:00
|
|
|
|
if (ResetEvent(event) == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Reset event failed - %@", [NSError _last]);
|
|
|
|
|
}
|
2005-02-23 16:05:09 +00:00
|
|
|
|
}
|
|
|
|
|
#else
|
2008-03-18 05:55:32 +00:00
|
|
|
|
if (inputFd >= 0)
|
2005-02-23 16:05:09 +00:00
|
|
|
|
{
|
2008-03-18 05:55:32 +00:00
|
|
|
|
if (read(inputFd, &c, 1) != 1)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Read pipe failed - %@", [NSError _last]);
|
|
|
|
|
}
|
2005-02-23 16:05:09 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
toDo = [NSArray arrayWithArray: performers];
|
|
|
|
|
[performers removeAllObjects];
|
|
|
|
|
[lock unlock];
|
2005-02-23 16:05:09 +00:00
|
|
|
|
|
|
|
|
|
c = [toDo count];
|
2003-04-06 07:14:50 +00:00
|
|
|
|
for (i = 0; i < c; i++)
|
2002-11-02 16:53:48 +00:00
|
|
|
|
{
|
2005-02-23 16:05:09 +00:00
|
|
|
|
GSPerformHolder *h = [toDo objectAtIndex: i];
|
2002-11-03 18:56:46 +00:00
|
|
|
|
|
|
|
|
|
[loop performSelector: @selector(fire)
|
|
|
|
|
target: h
|
|
|
|
|
argument: nil
|
|
|
|
|
order: 0
|
2008-03-17 15:23:11 +00:00
|
|
|
|
modes: [h modes]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
GSRunLoopThreadInfo *
|
|
|
|
|
GSRunLoopInfoForThread(NSThread *aThread)
|
|
|
|
|
{
|
|
|
|
|
GSRunLoopThreadInfo *info;
|
|
|
|
|
|
|
|
|
|
if (aThread == nil)
|
|
|
|
|
{
|
|
|
|
|
aThread = GSCurrentThread();
|
|
|
|
|
if (((NSThread_ivars*)aThread)->_runLoopInfo == nil)
|
|
|
|
|
{
|
|
|
|
|
((NSThread_ivars*)aThread)->_runLoopInfo = [GSRunLoopThreadInfo new];
|
|
|
|
|
}
|
2002-11-02 16:53:48 +00:00
|
|
|
|
}
|
2008-03-17 15:23:11 +00:00
|
|
|
|
info = ((NSThread_ivars*)aThread)->_runLoopInfo;
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@implementation GSPerformHolder
|
|
|
|
|
|
|
|
|
|
+ (GSPerformHolder*) newForReceiver: (id)r
|
|
|
|
|
argument: (id)a
|
|
|
|
|
selector: (SEL)s
|
|
|
|
|
modes: (NSArray*)m
|
|
|
|
|
lock: (NSConditionLock*)l
|
|
|
|
|
{
|
|
|
|
|
GSPerformHolder *h;
|
|
|
|
|
|
|
|
|
|
h = (GSPerformHolder*)NSAllocateObject(self, 0, NSDefaultMallocZone());
|
|
|
|
|
h->receiver = RETAIN(r);
|
|
|
|
|
h->argument = RETAIN(a);
|
|
|
|
|
h->selector = s;
|
|
|
|
|
h->modes = RETAIN(m);
|
|
|
|
|
h->lock = l;
|
|
|
|
|
|
|
|
|
|
return h;
|
2002-11-02 16:53:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-03 18:56:46 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
DESTROY(receiver);
|
|
|
|
|
DESTROY(argument);
|
|
|
|
|
DESTROY(modes);
|
|
|
|
|
if (lock != nil)
|
|
|
|
|
{
|
|
|
|
|
[lock lock];
|
|
|
|
|
[lock unlockWithCondition: 1];
|
|
|
|
|
lock = nil;
|
|
|
|
|
}
|
|
|
|
|
NSDeallocateObject(self);
|
2006-06-04 06:42:10 +00:00
|
|
|
|
GSNOSUPERDEALLOC;
|
2002-11-03 18:56:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) fire
|
|
|
|
|
{
|
2008-03-17 15:23:11 +00:00
|
|
|
|
GSRunLoopThreadInfo *threadInfo;
|
|
|
|
|
|
2002-11-03 18:56:46 +00:00
|
|
|
|
if (receiver == nil)
|
|
|
|
|
{
|
|
|
|
|
return; // Already fired!
|
|
|
|
|
}
|
2008-03-17 15:23:11 +00:00
|
|
|
|
threadInfo = GSRunLoopInfoForThread(GSCurrentThread());
|
|
|
|
|
[threadInfo->loop cancelPerformSelectorsWithTarget: self];
|
2002-11-03 18:56:46 +00:00
|
|
|
|
[receiver performSelector: selector withObject: argument];
|
|
|
|
|
DESTROY(receiver);
|
|
|
|
|
DESTROY(argument);
|
|
|
|
|
DESTROY(modes);
|
2008-03-18 05:45:05 +00:00
|
|
|
|
if (lock != nil)
|
2002-11-03 18:56:46 +00:00
|
|
|
|
{
|
2002-12-08 20:18:34 +00:00
|
|
|
|
NSConditionLock *l = lock;
|
|
|
|
|
|
2002-11-03 18:56:46 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
lock = nil;
|
2002-12-08 20:18:34 +00:00
|
|
|
|
[l unlockWithCondition: 1];
|
2002-11-03 18:56:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-03-17 15:23:11 +00:00
|
|
|
|
|
2008-03-18 05:45:05 +00:00
|
|
|
|
- (void) invalidate
|
|
|
|
|
{
|
|
|
|
|
if (invalidated == NO)
|
|
|
|
|
{
|
|
|
|
|
invalidated = YES;
|
|
|
|
|
DESTROY(receiver);
|
|
|
|
|
if (lock != nil)
|
|
|
|
|
{
|
|
|
|
|
NSConditionLock *l = lock;
|
|
|
|
|
|
|
|
|
|
[lock lock];
|
|
|
|
|
lock = nil;
|
|
|
|
|
[l unlockWithCondition: 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isInvalidated
|
|
|
|
|
{
|
|
|
|
|
return invalidated;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
- (NSArray*) modes
|
|
|
|
|
{
|
|
|
|
|
return modes;
|
|
|
|
|
}
|
2002-11-03 18:56:46 +00:00
|
|
|
|
@end
|
2002-11-02 16:53:48 +00:00
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
@implementation NSObject (NSThreadPerformAdditions)
|
2002-10-30 07:45:59 +00:00
|
|
|
|
|
|
|
|
|
- (void) performSelectorOnMainThread: (SEL)aSelector
|
|
|
|
|
withObject: (id)anObject
|
|
|
|
|
waitUntilDone: (BOOL)aFlag
|
2002-11-03 18:56:46 +00:00
|
|
|
|
modes: (NSArray*)anArray
|
2002-10-30 07:45:59 +00:00
|
|
|
|
{
|
2008-03-17 15:23:11 +00:00
|
|
|
|
[self performSelector: aSelector
|
|
|
|
|
onThread: defaultThread
|
|
|
|
|
withObject: anObject
|
|
|
|
|
waitUntilDone: aFlag
|
|
|
|
|
modes: anArray];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) performSelectorOnMainThread: (SEL)aSelector
|
|
|
|
|
withObject: (id)anObject
|
|
|
|
|
waitUntilDone: (BOOL)aFlag
|
|
|
|
|
{
|
|
|
|
|
[self performSelectorOnMainThread: aSelector
|
|
|
|
|
withObject: anObject
|
|
|
|
|
waitUntilDone: aFlag
|
|
|
|
|
modes: commonModes()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) performSelector: (SEL)aSelector
|
|
|
|
|
onThread: (NSThread*)aThread
|
|
|
|
|
withObject: (id)anObject
|
|
|
|
|
waitUntilDone: (BOOL)aFlag
|
|
|
|
|
modes: (NSArray*)anArray
|
|
|
|
|
{
|
|
|
|
|
GSRunLoopThreadInfo *info;
|
|
|
|
|
NSThread *t;
|
2002-10-30 07:45:59 +00:00
|
|
|
|
|
2002-11-03 18:56:46 +00:00
|
|
|
|
if ([anArray count] == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t = GSCurrentThread();
|
2008-03-17 15:23:11 +00:00
|
|
|
|
if (aThread == nil)
|
2002-10-30 07:45:59 +00:00
|
|
|
|
{
|
2008-03-17 15:23:11 +00:00
|
|
|
|
aThread = t;
|
|
|
|
|
}
|
|
|
|
|
info = GSRunLoopInfoForThread(aThread);
|
|
|
|
|
if (t == aThread)
|
|
|
|
|
{
|
2008-03-18 05:45:05 +00:00
|
|
|
|
/* Perform in current thread.
|
|
|
|
|
*/
|
2008-03-17 15:23:11 +00:00
|
|
|
|
if (aFlag == YES || info->loop == nil)
|
2002-11-02 16:53:48 +00:00
|
|
|
|
{
|
2008-03-18 05:45:05 +00:00
|
|
|
|
/* Wait until done or no run loop.
|
|
|
|
|
*/
|
2002-11-02 16:53:48 +00:00
|
|
|
|
[self performSelector: aSelector withObject: anObject];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-03-18 05:45:05 +00:00
|
|
|
|
/* Don't wait ... schedule operation in run loop.
|
|
|
|
|
*/
|
2008-03-17 15:23:11 +00:00
|
|
|
|
[info->loop performSelector: aSelector
|
|
|
|
|
target: self
|
|
|
|
|
argument: anObject
|
|
|
|
|
order: 0
|
|
|
|
|
modes: anArray];
|
2002-11-02 16:53:48 +00:00
|
|
|
|
}
|
2002-10-30 07:45:59 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-03-17 15:23:11 +00:00
|
|
|
|
GSPerformHolder *h;
|
2002-11-03 18:56:46 +00:00
|
|
|
|
NSConditionLock *l = nil;
|
2002-10-30 07:45:59 +00:00
|
|
|
|
|
2008-03-18 05:45:05 +00:00
|
|
|
|
if ([t isFinished] == YES)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"perform on finished thread"];
|
|
|
|
|
}
|
2002-10-30 07:45:59 +00:00
|
|
|
|
if (aFlag == YES)
|
|
|
|
|
{
|
2002-11-02 16:53:48 +00:00
|
|
|
|
l = [[NSConditionLock alloc] init];
|
2002-10-30 07:45:59 +00:00
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2002-10-30 07:45:59 +00:00
|
|
|
|
h = [GSPerformHolder newForReceiver: self
|
2002-11-03 18:56:46 +00:00
|
|
|
|
argument: anObject
|
|
|
|
|
selector: aSelector
|
|
|
|
|
modes: anArray
|
|
|
|
|
lock: l];
|
2008-03-17 15:23:11 +00:00
|
|
|
|
[info addPerformer: h];
|
|
|
|
|
if (l != nil)
|
2002-10-30 07:45:59 +00:00
|
|
|
|
{
|
2002-11-03 18:56:46 +00:00
|
|
|
|
[l lockWhenCondition: 1];
|
2002-10-30 12:37:21 +00:00
|
|
|
|
[l unlock];
|
|
|
|
|
RELEASE(l);
|
2008-03-18 05:45:05 +00:00
|
|
|
|
if ([h isInvalidated] == YES)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"perform on finished thread"];
|
|
|
|
|
RELEASE(h);
|
|
|
|
|
}
|
2002-10-30 07:45:59 +00:00
|
|
|
|
}
|
2008-03-17 15:23:11 +00:00
|
|
|
|
RELEASE(h);
|
2002-10-30 07:45:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-17 15:23:11 +00:00
|
|
|
|
- (void) performSelector: (SEL)aSelector
|
|
|
|
|
onThread: (NSThread*)aThread
|
|
|
|
|
withObject: (id)anObject
|
|
|
|
|
waitUntilDone: (BOOL)aFlag
|
2002-10-30 07:45:59 +00:00
|
|
|
|
{
|
2008-03-17 15:23:11 +00:00
|
|
|
|
[self performSelector: aSelector
|
|
|
|
|
onThread: aThread
|
|
|
|
|
withObject: anObject
|
|
|
|
|
waitUntilDone: aFlag
|
|
|
|
|
modes: commonModes()];
|
2002-10-30 07:45:59 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
2002-11-03 19:09:39 +00:00
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* This function is provided to let threads started by some other
|
|
|
|
|
* software library register themselves to be used with the
|
|
|
|
|
* GNUstep system. All such threads should call this function
|
|
|
|
|
* before attempting to use any GNUstep objects.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* Returns <code>YES</code> if the thread can be registered,
|
|
|
|
|
* <code>NO</code> if it is already registered.
|
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* Sends out a <code>NSWillBecomeMultiThreadedNotification</code>
|
|
|
|
|
* if the process was not already multithreaded.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
2001-03-19 12:20:21 +00:00
|
|
|
|
BOOL
|
|
|
|
|
GSRegisterCurrentThread (void)
|
2000-11-12 07:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
NSThread *thread;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Do nothing and return NO if the thread is known to us.
|
|
|
|
|
*/
|
|
|
|
|
if ((NSThread*)objc_thread_get_data() != nil)
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
2001-03-19 12:20:21 +00:00
|
|
|
|
/*
|
|
|
|
|
* Make sure the Objective-C runtime knows there is an additional thread.
|
|
|
|
|
*/
|
|
|
|
|
objc_thread_add ();
|
|
|
|
|
|
2002-05-03 10:00:53 +00:00
|
|
|
|
if (threadClass == 0)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* If the threadClass has not been set, NSThread has not been
|
|
|
|
|
* initialised, and there is no default thread. So we must
|
|
|
|
|
* initialise now ... which will make the current thread the default.
|
|
|
|
|
*/
|
|
|
|
|
NSCAssert(entered_multi_threaded_state == NO,
|
|
|
|
|
NSInternalInconsistencyException);
|
|
|
|
|
thread = [NSThread currentThread];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Create the new thread object.
|
|
|
|
|
*/
|
2005-02-22 11:22:44 +00:00
|
|
|
|
thread = (NSThread*)NSAllocateObject (threadClass, 0,
|
2001-03-19 12:20:21 +00:00
|
|
|
|
NSDefaultMallocZone ());
|
2007-11-25 14:25:26 +00:00
|
|
|
|
thread = [thread init];
|
2002-05-03 10:00:53 +00:00
|
|
|
|
objc_thread_set_data (thread);
|
|
|
|
|
((NSThread_ivars *)thread)->_active = YES;
|
|
|
|
|
}
|
2000-11-12 07:41:24 +00:00
|
|
|
|
|
|
|
|
|
/*
|
2005-02-22 11:22:44 +00:00
|
|
|
|
* We post the notification after we register the thread.
|
2002-05-03 10:00:53 +00:00
|
|
|
|
* NB. Even if we are the default thread, we do this to register the app
|
|
|
|
|
* as being multi-threaded - this is so that, if this thread is unregistered
|
|
|
|
|
* later, it does not leave us with a bad default thread.
|
2000-11-12 07:41:24 +00:00
|
|
|
|
*/
|
|
|
|
|
gnustep_base_thread_callback();
|
|
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* This function is provided to let threads started by some other
|
|
|
|
|
* software library unregister themselves from the GNUstep threading
|
2005-02-22 11:22:44 +00:00
|
|
|
|
* system.
|
2002-08-20 10:22:05 +00:00
|
|
|
|
* </p>
|
|
|
|
|
* <p>
|
|
|
|
|
* Calling this function causes a
|
|
|
|
|
* <code>NSThreadWillExitNotification</code>
|
|
|
|
|
* to be sent out, and destroys the GNUstep NSThread object
|
|
|
|
|
* associated with the thread.
|
|
|
|
|
* </p>
|
|
|
|
|
*/
|
2001-03-19 12:20:21 +00:00
|
|
|
|
void
|
|
|
|
|
GSUnregisterCurrentThread (void)
|
2000-11-12 07:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
NSThread *thread;
|
|
|
|
|
|
|
|
|
|
thread = GSCurrentThread();
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2001-12-19 03:24:13 +00:00
|
|
|
|
if (((NSThread_ivars *)thread)->_active == YES)
|
2000-11-12 07:41:24 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Set the thread to be inactive to avoid any possibility of recursion.
|
|
|
|
|
*/
|
2001-12-19 03:24:13 +00:00
|
|
|
|
((NSThread_ivars *)thread)->_active = NO;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
2001-03-19 13:15:10 +00:00
|
|
|
|
/*
|
|
|
|
|
* Let observers know this thread is exiting.
|
|
|
|
|
*/
|
2002-08-07 13:29:31 +00:00
|
|
|
|
if (nc == nil)
|
|
|
|
|
{
|
2004-11-10 11:45:08 +00:00
|
|
|
|
nc = RETAIN([NSNotificationCenter defaultCenter]);
|
2002-08-07 13:29:31 +00:00
|
|
|
|
}
|
|
|
|
|
[nc postNotificationName: NSThreadWillExitNotification
|
|
|
|
|
object: thread
|
2005-02-22 11:22:44 +00:00
|
|
|
|
userInfo: nil];
|
2000-11-22 08:41:07 +00:00
|
|
|
|
|
2000-11-12 07:41:24 +00:00
|
|
|
|
/*
|
|
|
|
|
* destroy the thread object.
|
|
|
|
|
*/
|
|
|
|
|
DESTROY (thread);
|
2000-11-22 08:41:07 +00:00
|
|
|
|
|
2000-11-12 07:41:24 +00:00
|
|
|
|
objc_thread_set_data (NULL);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make sure Objc runtime knows there is a thread less to manage
|
|
|
|
|
*/
|
|
|
|
|
objc_thread_remove ();
|
|
|
|
|
}
|
|
|
|
|
}
|