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 cf6adad881
commit 1823ed419e
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) AC_DEFINE(HAVE_CPP_TROUBLE_COMPARING_TO_ZERO)
fi 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 dnl End of C++ language/feature checks
AC_LANG_C AC_LANG_C

View File

@@ -466,6 +466,12 @@ typedef PRUint32 nsrefcnt;
#define XPCOM_GLUE_AVOID_NSPR #define XPCOM_GLUE_AVOID_NSPR
#endif #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: * Static type annotations, enforced when static-checking is enabled:
* *

View File

@@ -116,20 +116,29 @@ NS_GetMainThread(nsIThread **result)
#endif #endif
} }
NS_METHOD_(PRBool) #ifndef MOZILLA_INTERNAL_API
NS_IsMainThread() bool NS_IsMainThread()
{ {
PRBool result = PR_FALSE; PRBool result = PR_FALSE;
#ifdef MOZILLA_INTERNAL_API
nsThreadManager::get()->nsThreadManager::GetIsMainThread(&result);
#else
nsCOMPtr<nsIThreadManager> mgr = nsCOMPtr<nsIThreadManager> mgr =
do_GetService(NS_THREADMANAGER_CONTRACTID); do_GetService(NS_THREADMANAGER_CONTRACTID);
if (mgr) if (mgr)
mgr->GetIsMainThread(&result); mgr->GetIsMainThread(&result);
#endif return bool(result);
return 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_METHOD
NS_DispatchToCurrentThread(nsIRunnable *event) NS_DispatchToCurrentThread(nsIRunnable *event)

View File

@@ -98,14 +98,28 @@ NS_GetCurrentThread(nsIThread **result);
extern NS_COM_GLUE NS_METHOD extern NS_COM_GLUE NS_METHOD
NS_GetMainThread(nsIThread **result); 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. * 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 * @returns PR_TRUE if the current thread is the main thread, and PR_FALSE
* otherwise. * otherwise.
*/ */
extern NS_COM_GLUE NS_METHOD_(PRBool) extern NS_COM_GLUE bool NS_IsMainThread();
NS_IsMainThread(); #endif
/** /**
* Dispatch the given event to the current thread. * Dispatch the given event to the current thread.

View File

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