gecko-dev/xpcom/threads/nsThread.cpp

559 строки
14 KiB
C++
Исходник Обычный вид История

1999-04-02 13:20:44 +04:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsThread.h"
#include "prmem.h"
1999-06-15 08:57:07 +04:00
#include "prlog.h"
1999-04-02 13:20:44 +04:00
1999-04-13 22:15:27 +04:00
PRUintn nsThread::kIThreadSelfIndex = 0;
static nsIThread *gMainThread = 0;
1999-06-15 08:57:07 +04:00
#if defined(PR_LOGGING)
//
// Log module for nsIThread logging...
//
// To enable logging (see prlog.h for full details):
//
// set NSPR_LOG_MODULES=nsIThread:5
// set NSPR_LOG_FILE=nspr.log
//
// this enables PR_LOG_DEBUG level information and places all output in
// the file nspr.log
//
// gSocketLog is defined in nsSocketTransport.cpp
//
PRLogModuleInfo* nsIThreadLog = nsnull;
#endif /* PR_LOGGING */
1999-04-02 13:20:44 +04:00
////////////////////////////////////////////////////////////////////////////////
nsThread::nsThread()
1999-04-13 22:15:27 +04:00
: mThread(nsnull), mRunnable(nsnull), mDead(PR_FALSE)
1999-04-02 13:20:44 +04:00
{
NS_INIT_REFCNT();
1999-06-15 08:57:07 +04:00
#if defined(PR_LOGGING)
//
// Initialize the global PRLogModule for nsIThread logging
// if necessary...
//
if (nsIThreadLog == nsnull) {
nsIThreadLog = PR_NewLogModule("nsIThread");
}
#endif /* PR_LOGGING */
1999-04-02 13:20:44 +04:00
}
nsresult
nsThread::Init(nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
mRunnable = runnable;
NS_ADDREF(mRunnable);
1999-04-13 22:15:27 +04:00
NS_ADDREF_THIS(); // released in nsThread::Exit
if (state == PR_JOINABLE_THREAD)
NS_ADDREF_THIS(); // released in nsThread::Join
mThread = PR_CreateThread(PR_USER_THREAD, Main, this,
priority, scope, state, stackSize);
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThread %p created\n", this));
1999-04-02 13:20:44 +04:00
if (mThread == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
nsThread::~nsThread()
{
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThread %p destroyed\n", this));
1999-04-06 08:57:44 +04:00
NS_IF_RELEASE(mRunnable);
1999-04-02 13:20:44 +04:00
}
void
nsThread::Main(void* arg)
{
nsThread* self = (nsThread*)arg;
1999-04-02 13:20:44 +04:00
nsresult rv = NS_OK;
rv = self->RegisterThreadSelf();
NS_ASSERTION(rv == NS_OK, "failed to set thread self");
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThread %p start run %p\n", self, self->mRunnable));
1999-04-02 13:20:44 +04:00
rv = self->mRunnable->Run();
NS_ASSERTION(NS_SUCCEEDED(rv), "runnable failed");
PRThreadState state;
rv = self->GetState(&state);
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThread %p end run %p\n", self, self->mRunnable));
}
void
nsThread::Exit(void* arg)
{
nsThread* self = (nsThread*)arg;
nsresult rv = NS_OK;
1999-04-13 22:15:27 +04:00
self->mDead = PR_TRUE;
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThread %p exited\n", self));
NS_RELEASE(self);
1999-04-02 13:20:44 +04:00
}
NS_IMPL_ISUPPORTS(nsThread, nsIThread::GetIID());
NS_IMETHODIMP
nsThread::Join()
{
1999-04-13 22:15:27 +04:00
// don't check for mDead here because nspr calls Exit (cleaning up
// thread-local storage) before they let us join with the thread
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThread %p start join\n", this));
1999-04-02 13:20:44 +04:00
PRStatus status = PR_JoinThread(mThread);
1999-04-13 22:15:27 +04:00
// XXX can't use NS_RELEASE here because the macro wants to set
// this to null (bad c++)
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThread %p end join\n", this));
1999-04-13 22:15:27 +04:00
if (status == PR_SUCCESS) {
this->Release(); // most likely the final release of this thread
return NS_OK;
}
else
return NS_ERROR_FAILURE;
1999-04-02 13:20:44 +04:00
}
NS_IMETHODIMP
nsThread::GetPriority(PRThreadPriority *result)
{
1999-04-13 22:15:27 +04:00
if (mDead)
return NS_ERROR_FAILURE;
1999-04-02 13:20:44 +04:00
*result = PR_GetThreadPriority(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::SetPriority(PRThreadPriority value)
{
1999-04-13 22:15:27 +04:00
if (mDead)
return NS_ERROR_FAILURE;
1999-04-02 13:20:44 +04:00
PR_SetThreadPriority(mThread, value);
return NS_OK;
}
NS_IMETHODIMP
nsThread::Interrupt()
{
1999-04-13 22:15:27 +04:00
if (mDead)
return NS_ERROR_FAILURE;
1999-04-02 13:20:44 +04:00
PRStatus status = PR_Interrupt(mThread);
return status == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsThread::GetScope(PRThreadScope *result)
{
1999-04-13 22:15:27 +04:00
if (mDead)
return NS_ERROR_FAILURE;
1999-04-02 13:20:44 +04:00
*result = PR_GetThreadScope(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::GetState(PRThreadState *result)
{
1999-04-13 22:15:27 +04:00
if (mDead)
return NS_ERROR_FAILURE;
1999-04-02 13:20:44 +04:00
*result = PR_GetThreadState(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::GetPRThread(PRThread* *result)
{
1999-04-13 22:15:27 +04:00
if (mDead)
return NS_ERROR_FAILURE;
*result = mThread;
return NS_OK;
}
1999-05-26 05:38:36 +04:00
NS_COM nsresult
1999-04-02 13:20:44 +04:00
NS_NewThread(nsIThread* *result,
nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
nsresult rv;
nsThread* thread = new nsThread();
if (thread == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
1999-04-13 22:15:27 +04:00
rv = thread->Init(runnable, stackSize, priority, scope, state);
1999-04-02 13:20:44 +04:00
if (NS_FAILED(rv)) {
delete thread;
return rv;
}
NS_ADDREF(thread);
*result = thread;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
nsresult
nsThread::RegisterThreadSelf()
{
PRStatus status;
1999-04-13 22:15:27 +04:00
if (kIThreadSelfIndex == 0) {
status = PR_NewThreadPrivateIndex(&kIThreadSelfIndex, Exit);
if (status != PR_SUCCESS) return NS_ERROR_FAILURE;
1999-04-13 22:15:27 +04:00
NS_ASSERTION(kIThreadSelfIndex != 0, "couldn't get thread private index");
}
1999-04-13 22:15:27 +04:00
status = PR_SetThreadPrivate(kIThreadSelfIndex, this);
if (status != PR_SUCCESS) return NS_ERROR_FAILURE;
return NS_OK;
}
1999-05-26 05:38:36 +04:00
NS_COM nsresult
nsIThread::GetCurrent(nsIThread* *result)
1999-04-06 10:09:15 +04:00
{
return GetIThread(PR_CurrentThread(), result);
}
1999-05-26 05:38:36 +04:00
NS_COM nsresult
1999-04-06 10:09:15 +04:00
nsIThread::GetIThread(PRThread* prthread, nsIThread* *result)
{
PRStatus status;
nsThread* thread;
1999-04-13 22:15:27 +04:00
if (nsThread::kIThreadSelfIndex == 0) {
status = PR_NewThreadPrivateIndex(&nsThread::kIThreadSelfIndex, nsThread::Exit);
if (status != PR_SUCCESS) return NS_ERROR_FAILURE;
1999-04-13 22:15:27 +04:00
NS_ASSERTION(nsThread::kIThreadSelfIndex != 0, "couldn't get thread private index");
}
1999-04-13 22:15:27 +04:00
thread = (nsThread*)PR_GetThreadPrivate(nsThread::kIThreadSelfIndex);
if (thread == nsnull) {
// if the current thread doesn't have an nsIThread associated
// with it, make one
thread = new nsThread();
if (thread == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
1999-04-06 08:57:44 +04:00
NS_ADDREF(thread);
1999-04-06 10:09:15 +04:00
thread->SetPRThread(prthread);
nsresult rv = thread->RegisterThreadSelf();
if (NS_FAILED(rv)) return rv;
}
NS_ADDREF(thread);
*result = thread;
return NS_OK;
}
NS_COM nsresult
nsIThread::SetMainThread()
{
// strictly speaking, it could be set twice. but practically speaking,
// it's almost certainly an error if it is
if (gMainThread != 0)
return NS_ERROR_FAILURE;
return GetCurrent(&gMainThread);
}
NS_COM nsresult
nsIThread::GetMainThread(nsIThread **result)
{
if (gMainThread == 0)
return NS_ERROR_FAILURE;
if (*result == 0)
return NS_ERROR_NULL_POINTER;
*result = gMainThread;
NS_ADDREF(gMainThread);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
1999-04-02 13:20:44 +04:00
nsThreadPool::nsThreadPool(PRUint32 minThreads, PRUint32 maxThreads)
: mThreads(nsnull), mRequests(nsnull),
1999-04-06 05:42:01 +04:00
mMinThreads(minThreads), mMaxThreads(maxThreads), mShuttingDown(PR_FALSE)
1999-04-02 13:20:44 +04:00
{
NS_INIT_REFCNT();
}
nsresult
nsThreadPool::Init(PRUint32 stackSize,
PRThreadPriority priority,
1999-04-13 22:15:27 +04:00
PRThreadScope scope)
1999-04-02 13:20:44 +04:00
{
nsresult rv;
rv = NS_NewISupportsArray(&mThreads);
if (NS_FAILED(rv)) return rv;
rv = NS_NewISupportsArray(&mRequests);
if (NS_FAILED(rv)) return rv;
mRequestMonitor = PR_NewMonitor();
if (mRequestMonitor == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
1999-04-02 13:20:44 +04:00
PR_CEnterMonitor(this);
1999-04-02 13:20:44 +04:00
for (PRUint32 i = 0; i < mMinThreads; i++) {
nsThreadPoolRunnable* runnable =
new nsThreadPoolRunnable(this);
if (runnable == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(runnable);
nsIThread* thread;
1999-04-13 22:15:27 +04:00
rv = NS_NewThread(&thread, runnable, stackSize, priority, scope,
PR_JOINABLE_THREAD); // needed for Shutdown
1999-04-02 13:20:44 +04:00
NS_RELEASE(runnable);
if (NS_FAILED(rv)) goto exit;
1999-04-02 13:20:44 +04:00
rv = mThreads->AppendElement(thread) ? NS_OK : NS_ERROR_FAILURE;
1999-04-02 13:20:44 +04:00
NS_RELEASE(thread);
if (NS_FAILED(rv)) goto exit;
1999-04-02 13:20:44 +04:00
}
// wait for some worker thread to be ready
PR_CWait(this, PR_INTERVAL_NO_TIMEOUT);
exit:
PR_CExitMonitor(this);
return rv;
1999-04-02 13:20:44 +04:00
}
nsThreadPool::~nsThreadPool()
{
if (mThreads) {
1999-04-13 22:15:27 +04:00
Shutdown();
NS_RELEASE(mThreads);
}
NS_IF_RELEASE(mRequests);
if (mRequestMonitor) {
PR_DestroyMonitor(mRequestMonitor);
}
1999-04-02 13:20:44 +04:00
}
NS_IMPL_ISUPPORTS(nsThreadPool, nsIThreadPool::GetIID());
NS_IMETHODIMP
nsThreadPool::DispatchRequest(nsIRunnable* runnable)
{
nsresult rv;
PR_EnterMonitor(mRequestMonitor);
1999-04-02 13:20:44 +04:00
1999-04-06 05:42:01 +04:00
if (mShuttingDown) {
rv = NS_ERROR_FAILURE;
}
else {
// XXX for now AppendElement returns a PRBool
rv = ((PRBool) mRequests->AppendElement(runnable)) ? NS_OK : NS_ERROR_FAILURE;
1999-04-06 05:42:01 +04:00
if (NS_SUCCEEDED(rv))
PR_Notify(mRequestMonitor);
}
PR_ExitMonitor(mRequestMonitor);
1999-04-02 13:20:44 +04:00
return rv;
}
nsIRunnable*
nsThreadPool::GetRequest()
1999-04-02 13:20:44 +04:00
{
nsresult rv = NS_OK;
nsIRunnable* request = nsnull;
PR_EnterMonitor(mRequestMonitor);
PRUint32 cnt;
while (PR_TRUE) {
rv = mRequests->Count(&cnt);
if (NS_FAILED(rv) || cnt != 0)
break;
1999-04-06 05:42:01 +04:00
if (mShuttingDown) {
rv = NS_ERROR_FAILURE;
break;
}
1999-06-15 08:57:07 +04:00
#if defined(PR_LOGGING)
nsIThread* th;
nsIThread::GetCurrent(&th);
#endif
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThreadPool thread %p waiting\n", th));
PRStatus status = PR_Wait(mRequestMonitor, PR_INTERVAL_NO_TIMEOUT);
1999-04-06 05:42:01 +04:00
if (status != PR_SUCCESS || mShuttingDown) {
rv = NS_ERROR_FAILURE;
1999-04-06 05:42:01 +04:00
break;
}
}
1999-04-02 13:20:44 +04:00
if (NS_SUCCEEDED(rv)) {
NS_ASSERTION((NS_SUCCEEDED(mRequests->Count(&cnt)) && cnt > 0),
"request queue out of sync");
request = (nsIRunnable*)mRequests->ElementAt(0);
NS_ASSERTION(request != nsnull, "null runnable");
1999-04-02 13:20:44 +04:00
PRBool removed = mRequests->RemoveElementAt(0);
NS_ASSERTION(removed, "nsISupportsArray broken");
}
PR_ExitMonitor(mRequestMonitor);
1999-04-02 13:20:44 +04:00
return request;
}
NS_IMETHODIMP
1999-04-13 22:15:27 +04:00
nsThreadPool::ProcessPendingRequests()
{
1999-04-13 22:15:27 +04:00
nsresult rv;
PR_CEnterMonitor(this);
1999-04-13 22:15:27 +04:00
while (PR_TRUE) {
PRUint32 cnt;
rv = mRequests->Count(&cnt);
if (NS_FAILED(rv) || cnt == 0)
break;
PRStatus status = PR_CWait(this, PR_INTERVAL_NO_TIMEOUT);
if (status != PR_SUCCESS) {
rv = NS_ERROR_FAILURE; // our thread was interrupted!
break;
}
}
1999-04-13 22:15:27 +04:00
PR_CExitMonitor(this);
1999-04-13 22:15:27 +04:00
return rv;
}
NS_IMETHODIMP
nsThreadPool::Shutdown()
{
nsresult rv = NS_OK;
PRUint32 count = 0;
1999-04-13 22:15:27 +04:00
PRUint32 i;
1999-04-06 05:42:01 +04:00
mShuttingDown = PR_TRUE;
1999-04-13 22:15:27 +04:00
ProcessPendingRequests();
// then interrupt the threads and join them
rv = mThreads->Count(&count);
NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");
for (i = 0; i < count; i++) {
nsIThread* thread = (nsIThread*)(mThreads->ElementAt(0));
1999-04-13 22:15:27 +04:00
// we don't care about the error from Interrupt, because the
// thread may have already terminated its event loop
(void)thread->Interrupt();
rv = thread->Join();
1999-04-13 22:15:27 +04:00
// don't break out of the loop because of an error here
NS_ASSERTION(NS_SUCCEEDED(rv), "Join failed");
NS_RELEASE(thread);
1999-04-06 05:42:01 +04:00
rv = mThreads->RemoveElementAt(0);
1999-04-13 22:15:27 +04:00
// don't break out of the loop because of an error here
NS_ASSERTION(NS_SUCCEEDED(rv), "RemoveElementAt failed");
}
1999-04-06 05:42:01 +04:00
return rv;
}
1999-05-26 05:38:36 +04:00
NS_COM nsresult
1999-04-02 13:20:44 +04:00
NS_NewThreadPool(nsIThreadPool* *result,
PRUint32 minThreads, PRUint32 maxThreads,
PRUint32 stackSize,
PRThreadPriority priority,
1999-04-13 22:15:27 +04:00
PRThreadScope scope)
1999-04-02 13:20:44 +04:00
{
nsresult rv;
nsThreadPool* pool = new nsThreadPool(minThreads, maxThreads);
if (pool == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
1999-04-13 22:15:27 +04:00
rv = pool->Init(stackSize, priority, scope);
1999-04-02 13:20:44 +04:00
if (NS_FAILED(rv)) {
delete pool;
return rv;
}
NS_ADDREF(pool);
*result = pool;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
nsThreadPoolRunnable::nsThreadPoolRunnable(nsThreadPool* pool)
: mPool(pool)
{
NS_INIT_REFCNT();
NS_ADDREF(mPool);
}
nsThreadPoolRunnable::~nsThreadPoolRunnable()
{
NS_RELEASE(mPool);
}
NS_IMPL_ISUPPORTS(nsThreadPoolRunnable, nsIRunnable::GetIID());
NS_IMETHODIMP
nsThreadPoolRunnable::Run()
{
nsresult rv = NS_OK;
nsIRunnable* request;
1999-04-02 13:20:44 +04:00
// let the thread pool know we're ready
PR_CEnterMonitor(mPool);
PR_CNotify(mPool);
PR_CExitMonitor(mPool);
1999-04-02 13:20:44 +04:00
1999-06-15 08:57:07 +04:00
#if defined(PR_LOGGING)
nsIThread* th;
nsIThread::GetCurrent(&th);
#endif
while ((request = mPool->GetRequest()) != nsnull) {
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThreadPool thread %p running %p\n", th, this));
rv = request->Run();
1999-04-02 13:20:44 +04:00
NS_ASSERTION(NS_SUCCEEDED(rv), "runnable failed");
1999-06-15 08:57:07 +04:00
// let the thread pool know we've finished a run
PR_CEnterMonitor(mPool);
PR_CNotify(mPool);
PR_CExitMonitor(mPool);
1999-04-02 13:20:44 +04:00
}
1999-06-15 08:57:07 +04:00
PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
("nsIThreadPool thread %p quitting %x\n", th, this));
return rv;
1999-04-02 13:20:44 +04:00
}
////////////////////////////////////////////////////////////////////////////////