Make NS_IsMainThread faster on our major platforms. (Bug 521750) r=dbaron

This commit is contained in:
Benjamin Smedberg 2009-10-28 10:28:57 -07:00
parent 402e13d377
commit 7d138d4786
5 changed files with 57 additions and 11 deletions

View File

@ -4160,7 +4160,15 @@ if test "$ac_cv_trouble_comparing_to_zero" = yes ; then
AC_DEFINE(HAVE_CPP_TROUBLE_COMPARING_TO_ZERO)
fi
AC_CACHE_CHECK(for __thread keyword for TLS variables,
ac_cv_thread_keyword,
[AC_TRY_COMPILE([__thread bool tlsIsMainThread = false;],
[return tlsIsMainThread;],
ac_cv_thread_keyword=yes,
ac_cv_thread_keyword=no)])
if test "$ac_cv_thread_keyword" = yes; then
AC_DEFINE(HAVE_THREAD_TLS_KEYWORD)
fi
dnl End of C++ language/feature checks
AC_LANG_C

View File

@ -466,6 +466,12 @@ typedef PRUint32 nsrefcnt;
#define XPCOM_GLUE_AVOID_NSPR
#endif
#if defined(_MSC_VER) && !defined(WINCE)
#define NS_TLS __declspec(thread)
#elif defined(HAVE_THREAD_TLS_KEYWORD)
#define NS_TLS __thread
#endif
/**
* Static type annotations, enforced when static-checking is enabled:
*

View File

@ -116,20 +116,29 @@ NS_GetMainThread(nsIThread **result)
#endif
}
NS_METHOD_(PRBool)
NS_IsMainThread()
#ifndef MOZILLA_INTERNAL_API
bool NS_IsMainThread()
{
PRBool result = PR_FALSE;
#ifdef MOZILLA_INTERNAL_API
nsThreadManager::get()->nsThreadManager::GetIsMainThread(&result);
#else
nsCOMPtr<nsIThreadManager> mgr =
do_GetService(NS_THREADMANAGER_CONTRACTID);
do_GetService(NS_THREADMANAGER_CONTRACTID);
if (mgr)
mgr->GetIsMainThread(&result);
#endif
return result;
return bool(result);
}
#elif !defined(NS_TLS)
bool NS_IsMainThread()
{
PRBool result = PR_FALSE;
nsThreadManager::get()->nsThreadManager::GetIsMainThread(&result);
return bool(result);
}
#elif !defined(MOZ_ENABLE_LIBXUL)
bool NS_IsMainThread()
{
return gTLSIsMainThread;
}
#endif
NS_METHOD
NS_DispatchToCurrentThread(nsIRunnable *event)

View File

@ -98,14 +98,28 @@ NS_GetCurrentThread(nsIThread **result);
extern NS_COM_GLUE NS_METHOD
NS_GetMainThread(nsIThread **result);
#if defined(MOZILLA_INTERNAL_API) && defined(NS_TLS)
// This is defined in nsThreadManager.cpp and initialized to `true` for the
// main thread by nsThreadManager::Init.
extern NS_TLS bool gTLSIsMainThread;
#ifdef MOZ_ENABLE_LIBXUL
inline bool NS_IsMainThread()
{
return gTLSIsMainThread;
}
#else
NS_COM bool NS_IsMainThread();
#endif
#else
/**
* Test to see if the current thread is the main thread.
*
* @returns PR_TRUE if the current thread is the main thread, and PR_FALSE
* otherwise.
*/
extern NS_COM_GLUE NS_METHOD_(PRBool)
NS_IsMainThread();
extern NS_COM_GLUE bool NS_IsMainThread();
#endif
/**
* Dispatch the given event to the current thread.

View File

@ -38,11 +38,16 @@
#include "nsThreadManager.h"
#include "nsThread.h"
#include "nsThreadUtils.h"
#include "nsIClassInfoImpl.h"
#include "nsTArray.h"
#include "nsAutoPtr.h"
#include "nsAutoLock.h"
#ifdef NS_TLS
NS_TLS bool gTLSIsMainThread = false;
#endif
typedef nsTArray< nsRefPtr<nsThread> > nsThreadArray;
//-----------------------------------------------------------------------------
@ -101,6 +106,10 @@ nsThreadManager::Init()
// GetIsMainThread calls that occur post-Shutdown.
mMainThread->GetPRThread(&mMainPRThread);
#ifdef NS_TLS
gTLSIsMainThread = true;
#endif
mInitialized = PR_TRUE;
return NS_OK;
}