Bug 664341 - Provide API to specify thread stack size. r=bsmedberg

This commit is contained in:
Chris Pearce 2011-07-27 15:26:47 +12:00
parent cbfd8e2789
commit 3683db2819
7 changed files with 43 additions and 11 deletions

View File

@ -66,19 +66,19 @@ nsRunnable::Run()
//-----------------------------------------------------------------------------
NS_METHOD
NS_NewThread(nsIThread **result, nsIRunnable *event)
NS_NewThread(nsIThread **result, nsIRunnable *event, PRUint32 stackSize)
{
nsCOMPtr<nsIThread> thread;
#ifdef MOZILLA_INTERNAL_API
nsresult rv = nsThreadManager::get()->
nsThreadManager::NewThread(0, getter_AddRefs(thread));
nsThreadManager::NewThread(0, stackSize, getter_AddRefs(thread));
#else
nsresult rv;
nsCOMPtr<nsIThreadManager> mgr =
do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = mgr->NewThread(0, getter_AddRefs(thread));
rv = mgr->NewThread(0, stackSize, getter_AddRefs(thread));
#endif
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -75,12 +75,16 @@
* The resulting nsIThread object.
* @param initialEvent
* The initial event to run on this thread. This parameter may be null.
* @param stackSize
* The size in bytes to reserve for the thread's stack.
*
* @returns NS_ERROR_INVALID_ARG
* Indicates that the given name is not unique.
*/
extern NS_COM_GLUE NS_METHOD
NS_NewThread(nsIThread **result, nsIRunnable *initialEvent = nsnull);
NS_NewThread(nsIThread **result,
nsIRunnable *initialEvent = nsnull,
PRUint32 stackSize = nsIThreadManager::DEFAULT_STACK_SIZE);
/**
* Get a reference to the current thread.

View File

@ -45,20 +45,28 @@ interface nsIThread;
/**
* An interface for creating and locating nsIThread instances.
*/
[scriptable, uuid(487c10bf-0a22-4148-89fa-790d819dd559)]
[scriptable, uuid(2bbbc38c-cf96-4099-ba6b-f6a44d8b014c)]
interface nsIThreadManager : nsISupports
{
/**
* Default number of bytes reserved for a thread's stack, if no stack size
* is specified in newThread(). 0 means use platform default.
*/
const unsigned long DEFAULT_STACK_SIZE = 0;
/**
* Create a new thread (a global, user PRThread).
*
* @param creationFlags
* Reserved for future use. Pass 0.
* @param stackSize
* Number of bytes to reserve for the thread's stack.
*
* @returns
* The newly created nsIThread object.
*/
nsIThread newThread(in unsigned long creationFlags);
nsIThread newThread(in unsigned long creationFlags, [optional] in unsigned long stackSize);
/**
* Get the nsIThread object (if any) corresponding to the given PRThread.
* This method returns null if there is no corresponding nsIThread.

View File

@ -312,6 +312,20 @@ nsThread::nsThread()
, mPriority(PRIORITY_NORMAL)
, mThread(nsnull)
, mRunningEvent(0)
, mStackSize(0)
, mShutdownContext(nsnull)
, mShutdownRequired(PR_FALSE)
, mEventsAreDoomed(PR_FALSE)
{
}
nsThread::nsThread(PRUint32 aStackSize)
: mLock("nsThread.mLock")
, mEvents(&mEventsRoot)
, mPriority(PRIORITY_NORMAL)
, mThread(nsnull)
, mRunningEvent(0)
, mStackSize(aStackSize)
, mShutdownContext(nsnull)
, mShutdownRequired(PR_FALSE)
, mEventsAreDoomed(PR_FALSE)
@ -336,7 +350,7 @@ nsThread::Init()
// ThreadFunc is responsible for setting mThread
PRThread *thr = PR_CreateThread(PR_USER_THREAD, ThreadFunc, this,
PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
PR_JOINABLE_THREAD, 0);
PR_JOINABLE_THREAD, mStackSize);
if (!thr) {
NS_RELEASE_THIS();
return NS_ERROR_OUT_OF_MEMORY;

View File

@ -58,6 +58,7 @@ public:
NS_DECL_NSISUPPORTSPRIORITY
nsThread();
nsThread(PRUint32 aStackSize);
// Initialize this as a wrapper for a new PRThread.
nsresult Init();
@ -138,6 +139,7 @@ private:
PRInt32 mPriority;
PRThread *mThread;
PRUint32 mRunningEvent; // counter
PRUint32 mStackSize;
struct nsThreadShutdownContext *mShutdownContext;

View File

@ -232,12 +232,14 @@ nsThreadManager::GetCurrentThread()
}
NS_IMETHODIMP
nsThreadManager::NewThread(PRUint32 creationFlags, nsIThread **result)
nsThreadManager::NewThread(PRUint32 creationFlags,
PRUint32 stackSize,
nsIThread **result)
{
// No new threads during Shutdown
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
nsThread *thr = new nsThread();
nsThread *thr = new nsThread(stackSize);
if (!thr)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(thr);

View File

@ -110,7 +110,9 @@ nsThreadPool::PutEvent(nsIRunnable *event)
return NS_OK;
nsCOMPtr<nsIThread> thread;
nsThreadManager::get()->NewThread(0, getter_AddRefs(thread));
nsThreadManager::get()->NewThread(0,
nsIThreadManager::DEFAULT_STACK_SIZE,
getter_AddRefs(thread));
NS_ENSURE_STATE(thread);
PRBool killThread = PR_FALSE;