Added nsIThread and nsIThreadPool.

This commit is contained in:
warren%netscape.com 1999-04-02 09:20:44 +00:00
Родитель 2f272f4af6
Коммит 8bf75dfb46
8 изменённых файлов: 1026 добавлений и 5 удалений

Просмотреть файл

@ -31,6 +31,7 @@ EXPORTS = \
nsIFileStream.h \
nsIString.h \
nsIStringStream.h \
nsIThread.h \
nsSpecialSystemDirectory.h \
$(NULL)

119
base/public/nsIThread.h Normal file
Просмотреть файл

@ -0,0 +1,119 @@
/* -*- 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.
*/
#ifndef nsIThread_h__
#define nsIThread_h__
#include "nsISupports.h"
#include "prthread.h"
#include "nscore.h"
////////////////////////////////////////////////////////////////////////////////
// XXX regenerate:
#define NS_IRUNNABLE_IID \
{ /* 677d9a90-93ee-11d2-816a-006008119d7a */ \
0x677d9a90, \
0x93ee, \
0x11d2, \
{0x81, 0x6a, 0x00, 0x60, 0x19, 0x11, 0x9d, 0x7a} \
}
class nsIRunnable : public nsISupports
{
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_IRUNNABLE_IID);
NS_IMETHOD Run() = 0;
};
////////////////////////////////////////////////////////////////////////////////
// XXX regenerate:
#define NS_ITHREAD_IID \
{ /* 677d9a90-93ee-11d2-816a-006008119d7a */ \
0x677d9a90, \
0x93ee, \
0x11d2, \
{0x81, 0x6a, 0x00, 0x60, 0x29, 0x11, 0x9d, 0x7a} \
}
class nsIThread : public nsISupports
{
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ITHREAD_IID);
NS_IMETHOD Join() = 0;
NS_IMETHOD GetPriority(PRThreadPriority *result) = 0;
NS_IMETHOD SetPriority(PRThreadPriority value) = 0;
NS_IMETHOD Interrupt() = 0;
NS_IMETHOD GetScope(PRThreadScope *result) = 0;
NS_IMETHOD GetType(PRThreadType *result) = 0;
NS_IMETHOD GetState(PRThreadState *result) = 0;
};
extern NS_BASE nsresult
NS_NewThread(nsIThread* *result,
nsIRunnable* runnable,
PRUint32 stackSize = 0,
PRThreadType type = PR_USER_THREAD,
PRThreadPriority priority = PR_PRIORITY_NORMAL,
PRThreadScope scope = PR_GLOBAL_THREAD,
PRThreadState state = PR_JOINABLE_THREAD);
////////////////////////////////////////////////////////////////////////////////
// XXX regenerate:
#define NS_ITHREADPOOL_IID \
{ /* 677d9a90-93ee-11d2-816a-006008119d7a */ \
0x677d9a90, \
0x93ee, \
0x11d2, \
{0x81, 0x6a, 0x00, 0x60, 0x39, 0x11, 0x9d, 0x7a} \
}
/**
* A thread pool is used to create a group of worker threads that
* share in the servicing of requests. Requests are run in the
* context of the worker thread.
*/
class nsIThreadPool : public nsISupports
{
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ITHREADPOOL_IID);
NS_IMETHOD DispatchRequest(nsIRunnable* runnable) = 0;
};
extern NS_BASE nsresult
NS_NewThreadPool(nsIThreadPool* *result,
PRUint32 minThreads, PRUint32 maxThreads,
PRUint32 stackSize = 0,
PRThreadType type = PR_USER_THREAD,
PRThreadPriority priority = PR_PRIORITY_NORMAL,
PRThreadScope scope = PR_GLOBAL_THREAD,
PRThreadState state = PR_JOINABLE_THREAD);
////////////////////////////////////////////////////////////////////////////////
#endif // nsIThread_h__

Просмотреть файл

@ -44,8 +44,9 @@ CPPSRCS = \
nsFileStream.cpp \
nsStringStream.cpp \
nsEscape.cpp \
nsIFileStream.cpp \
nsSpecialSystemDirectory.cpp \
nsIFileStream.cpp \
nsThread.cpp \
nsSpecialSystemDirectory.cpp \
$(NULL)
CPP_OBJS = \
@ -68,9 +69,10 @@ CPP_OBJS = \
.\$(OBJDIR)\nsFileSpec.obj \
.\$(OBJDIR)\nsFileStream.obj \
.\$(OBJDIR)\nsEscape.obj \
.\$(OBJDIR)\nsIFileStream.obj \
.\$(OBJDIR)\nsIStringStream.obj \
.\$(OBJDIR)\nsSpecialSystemDirectory.obj \
.\$(OBJDIR)\nsIFileStream.obj \
.\$(OBJDIR)\nsThread.obj \
.\$(OBJDIR)\nsIStringStream.obj \
.\$(OBJDIR)\nsSpecialSystemDirectory.obj \
$(NULL)
EXPORTS=nscore.h nsIArena.h nsIAtom.h nsIByteBuffer.h \

285
base/src/nsThread.cpp Normal file
Просмотреть файл

@ -0,0 +1,285 @@
/* -*- 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"
////////////////////////////////////////////////////////////////////////////////
nsThread::nsThread()
: mThread(nsnull), mRunnable(nsnull)
{
NS_INIT_REFCNT();
}
nsresult
nsThread::Init(nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
mRunnable = runnable;
NS_ADDREF(mRunnable);
mThread = PR_CreateThread(type, Main, this,
priority, scope, PR_JOINABLE_THREAD, stackSize);
if (mThread == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
nsThread::~nsThread()
{
if (mThread) {
PRStatus status = PR_SUCCESS;
status = PR_Interrupt(mThread);
NS_ASSERTION(status == PR_SUCCESS, "failed to interrupt worker");
status = PR_JoinThread(mThread);
NS_ASSERTION(status == PR_SUCCESS, "failed to join with worker");
PR_Free(mThread); // XXX right?
mThread = nsnull;
}
}
void
nsThread::Main(void* arg)
{
nsThread* self = (nsThread*)arg;
nsresult rv = NS_OK;
rv = self->mRunnable->Run();
NS_ASSERTION(NS_SUCCEEDED(rv), "runnable failed");
}
NS_IMPL_ISUPPORTS(nsThread, nsIThread::GetIID());
NS_IMETHODIMP
nsThread::Join()
{
PRStatus status = PR_JoinThread(mThread);
return status == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsThread::GetPriority(PRThreadPriority *result)
{
*result = PR_GetThreadPriority(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::SetPriority(PRThreadPriority value)
{
PR_SetThreadPriority(mThread, value);
return NS_OK;
}
NS_IMETHODIMP
nsThread::Interrupt()
{
PRStatus status = PR_Interrupt(mThread);
return status == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsThread::GetScope(PRThreadScope *result)
{
*result = PR_GetThreadScope(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::GetType(PRThreadType *result)
{
*result = PR_GetThreadType(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::GetState(PRThreadState *result)
{
*result = PR_GetThreadState(mThread);
return NS_OK;
}
NS_BASE nsresult
NS_NewThread(nsIThread* *result,
nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
nsresult rv;
nsThread* thread = new nsThread();
if (thread == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
rv = thread->Init(runnable, stackSize, type, priority, scope, state);
if (NS_FAILED(rv)) {
delete thread;
return rv;
}
NS_ADDREF(thread);
*result = thread;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
nsThreadPool::nsThreadPool(PRUint32 minThreads, PRUint32 maxThreads)
: mThreads(nsnull), mRequests(nsnull),
mMinThreads(minThreads), mMaxThreads(maxThreads)
{
NS_INIT_REFCNT();
}
nsresult
nsThreadPool::Init(PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
nsresult rv;
rv = NS_NewISupportsArray(&mThreads);
if (NS_FAILED(rv)) return rv;
rv = NS_NewISupportsArray(&mRequests);
if (NS_FAILED(rv)) return rv;
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;
rv = NS_NewThread(&thread, runnable, stackSize, type,
priority, scope, state);
NS_RELEASE(runnable);
if (NS_FAILED(rv)) return rv;
rv = mThreads->AppendElement(thread);
NS_RELEASE(thread);
if (NS_FAILED(rv)) return rv;
}
return NS_OK;
}
nsThreadPool::~nsThreadPool()
{
NS_RELEASE(mThreads);
NS_RELEASE(mRequests);
}
NS_IMPL_ISUPPORTS(nsThreadPool, nsIThreadPool::GetIID());
NS_IMETHODIMP
nsThreadPool::DispatchRequest(nsIRunnable* runnable)
{
nsresult rv;
PR_CEnterMonitor(this);
rv = mRequests->AppendElement(runnable);
if (NS_SUCCEEDED(rv))
PR_CNotify(this);
PR_CExitMonitor(this);
return rv;
}
nsIRunnable*
nsThreadPool::Dequeue()
{
PR_CEnterMonitor(this);
NS_ASSERTION(mRequests->Count() > 0, "request queue out of sync");
nsIRunnable* request = (nsIRunnable*)(*mRequests)[0];
PRBool removed = mRequests->RemoveElementAt(0);
NS_ASSERTION(removed, "nsISupportsArray broken");
PR_CExitMonitor(this);
return request;
}
NS_BASE nsresult
NS_NewThreadPool(nsIThreadPool* *result,
PRUint32 minThreads, PRUint32 maxThreads,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
nsresult rv;
nsThreadPool* pool = new nsThreadPool(minThreads, maxThreads);
if (pool == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
rv = pool->Init(stackSize, type, priority, scope, state);
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()
{
PR_CEnterMonitor(mPool);
while (PR_TRUE) {
PRStatus status = PR_CWait(mPool, PR_INTERVAL_NO_TIMEOUT);
if (status != PR_SUCCESS) break; // interrupted -- quit
nsIRunnable* runnable = mPool->Dequeue();
nsresult rv = NS_OK;
rv = runnable->Run();
NS_ASSERTION(NS_SUCCEEDED(rv), "runnable failed");
}
PR_CExitMonitor(mPool);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////

105
base/src/nsThread.h Normal file
Просмотреть файл

@ -0,0 +1,105 @@
/* -*- 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.
*/
#ifndef nsThread_h__
#define nsThread_h__
#include "nsIThread.h"
#include "nsISupportsArray.h"
class nsThread : public nsIThread
{
public:
NS_DECL_ISUPPORTS
// nsIThread methods:
NS_IMETHOD Join();
NS_IMETHOD GetPriority(PRThreadPriority *result);
NS_IMETHOD SetPriority(PRThreadPriority value);
NS_IMETHOD Interrupt();
NS_IMETHOD GetScope(PRThreadScope *result);
NS_IMETHOD GetType(PRThreadType *result);
NS_IMETHOD GetState(PRThreadState *result);
// nsThread methods:
nsThread();
virtual ~nsThread();
nsresult Init(nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state);
static void Main(void* arg);
protected:
PRThread* mThread;
nsIRunnable* mRunnable;
};
////////////////////////////////////////////////////////////////////////////////
class nsThreadPool : public nsIThreadPool
{
public:
NS_DECL_ISUPPORTS
// nsIThreadPool methods:
NS_IMETHOD DispatchRequest(nsIRunnable* runnable);
// nsThreadPool methods:
nsThreadPool(PRUint32 minThreads, PRUint32 maxThreads);
virtual ~nsThreadPool();
nsresult Init(PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state);
nsIRunnable* Dequeue();
protected:
nsISupportsArray* mThreads;
nsISupportsArray* mRequests;
PRUint32 mMinThreads;
PRUint32 mMaxThreads;
};
////////////////////////////////////////////////////////////////////////////////
class nsThreadPoolRunnable : public nsIRunnable
{
public:
NS_DECL_ISUPPORTS
// nsIRunnable methods:
NS_IMETHOD Run();
// nsThreadPoolRunnable methods:
nsThreadPoolRunnable(nsThreadPool* pool);
virtual ~nsThreadPoolRunnable();
protected:
nsThreadPool* mPool;
};
////////////////////////////////////////////////////////////////////////////////
#endif // nsThread_h__

119
xpcom/threads/nsIThread.h Normal file
Просмотреть файл

@ -0,0 +1,119 @@
/* -*- 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.
*/
#ifndef nsIThread_h__
#define nsIThread_h__
#include "nsISupports.h"
#include "prthread.h"
#include "nscore.h"
////////////////////////////////////////////////////////////////////////////////
// XXX regenerate:
#define NS_IRUNNABLE_IID \
{ /* 677d9a90-93ee-11d2-816a-006008119d7a */ \
0x677d9a90, \
0x93ee, \
0x11d2, \
{0x81, 0x6a, 0x00, 0x60, 0x19, 0x11, 0x9d, 0x7a} \
}
class nsIRunnable : public nsISupports
{
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_IRUNNABLE_IID);
NS_IMETHOD Run() = 0;
};
////////////////////////////////////////////////////////////////////////////////
// XXX regenerate:
#define NS_ITHREAD_IID \
{ /* 677d9a90-93ee-11d2-816a-006008119d7a */ \
0x677d9a90, \
0x93ee, \
0x11d2, \
{0x81, 0x6a, 0x00, 0x60, 0x29, 0x11, 0x9d, 0x7a} \
}
class nsIThread : public nsISupports
{
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ITHREAD_IID);
NS_IMETHOD Join() = 0;
NS_IMETHOD GetPriority(PRThreadPriority *result) = 0;
NS_IMETHOD SetPriority(PRThreadPriority value) = 0;
NS_IMETHOD Interrupt() = 0;
NS_IMETHOD GetScope(PRThreadScope *result) = 0;
NS_IMETHOD GetType(PRThreadType *result) = 0;
NS_IMETHOD GetState(PRThreadState *result) = 0;
};
extern NS_BASE nsresult
NS_NewThread(nsIThread* *result,
nsIRunnable* runnable,
PRUint32 stackSize = 0,
PRThreadType type = PR_USER_THREAD,
PRThreadPriority priority = PR_PRIORITY_NORMAL,
PRThreadScope scope = PR_GLOBAL_THREAD,
PRThreadState state = PR_JOINABLE_THREAD);
////////////////////////////////////////////////////////////////////////////////
// XXX regenerate:
#define NS_ITHREADPOOL_IID \
{ /* 677d9a90-93ee-11d2-816a-006008119d7a */ \
0x677d9a90, \
0x93ee, \
0x11d2, \
{0x81, 0x6a, 0x00, 0x60, 0x39, 0x11, 0x9d, 0x7a} \
}
/**
* A thread pool is used to create a group of worker threads that
* share in the servicing of requests. Requests are run in the
* context of the worker thread.
*/
class nsIThreadPool : public nsISupports
{
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ITHREADPOOL_IID);
NS_IMETHOD DispatchRequest(nsIRunnable* runnable) = 0;
};
extern NS_BASE nsresult
NS_NewThreadPool(nsIThreadPool* *result,
PRUint32 minThreads, PRUint32 maxThreads,
PRUint32 stackSize = 0,
PRThreadType type = PR_USER_THREAD,
PRThreadPriority priority = PR_PRIORITY_NORMAL,
PRThreadScope scope = PR_GLOBAL_THREAD,
PRThreadState state = PR_JOINABLE_THREAD);
////////////////////////////////////////////////////////////////////////////////
#endif // nsIThread_h__

285
xpcom/threads/nsThread.cpp Normal file
Просмотреть файл

@ -0,0 +1,285 @@
/* -*- 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"
////////////////////////////////////////////////////////////////////////////////
nsThread::nsThread()
: mThread(nsnull), mRunnable(nsnull)
{
NS_INIT_REFCNT();
}
nsresult
nsThread::Init(nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
mRunnable = runnable;
NS_ADDREF(mRunnable);
mThread = PR_CreateThread(type, Main, this,
priority, scope, PR_JOINABLE_THREAD, stackSize);
if (mThread == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
nsThread::~nsThread()
{
if (mThread) {
PRStatus status = PR_SUCCESS;
status = PR_Interrupt(mThread);
NS_ASSERTION(status == PR_SUCCESS, "failed to interrupt worker");
status = PR_JoinThread(mThread);
NS_ASSERTION(status == PR_SUCCESS, "failed to join with worker");
PR_Free(mThread); // XXX right?
mThread = nsnull;
}
}
void
nsThread::Main(void* arg)
{
nsThread* self = (nsThread*)arg;
nsresult rv = NS_OK;
rv = self->mRunnable->Run();
NS_ASSERTION(NS_SUCCEEDED(rv), "runnable failed");
}
NS_IMPL_ISUPPORTS(nsThread, nsIThread::GetIID());
NS_IMETHODIMP
nsThread::Join()
{
PRStatus status = PR_JoinThread(mThread);
return status == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsThread::GetPriority(PRThreadPriority *result)
{
*result = PR_GetThreadPriority(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::SetPriority(PRThreadPriority value)
{
PR_SetThreadPriority(mThread, value);
return NS_OK;
}
NS_IMETHODIMP
nsThread::Interrupt()
{
PRStatus status = PR_Interrupt(mThread);
return status == PR_SUCCESS ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsThread::GetScope(PRThreadScope *result)
{
*result = PR_GetThreadScope(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::GetType(PRThreadType *result)
{
*result = PR_GetThreadType(mThread);
return NS_OK;
}
NS_IMETHODIMP
nsThread::GetState(PRThreadState *result)
{
*result = PR_GetThreadState(mThread);
return NS_OK;
}
NS_BASE nsresult
NS_NewThread(nsIThread* *result,
nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
nsresult rv;
nsThread* thread = new nsThread();
if (thread == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
rv = thread->Init(runnable, stackSize, type, priority, scope, state);
if (NS_FAILED(rv)) {
delete thread;
return rv;
}
NS_ADDREF(thread);
*result = thread;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
nsThreadPool::nsThreadPool(PRUint32 minThreads, PRUint32 maxThreads)
: mThreads(nsnull), mRequests(nsnull),
mMinThreads(minThreads), mMaxThreads(maxThreads)
{
NS_INIT_REFCNT();
}
nsresult
nsThreadPool::Init(PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
nsresult rv;
rv = NS_NewISupportsArray(&mThreads);
if (NS_FAILED(rv)) return rv;
rv = NS_NewISupportsArray(&mRequests);
if (NS_FAILED(rv)) return rv;
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;
rv = NS_NewThread(&thread, runnable, stackSize, type,
priority, scope, state);
NS_RELEASE(runnable);
if (NS_FAILED(rv)) return rv;
rv = mThreads->AppendElement(thread);
NS_RELEASE(thread);
if (NS_FAILED(rv)) return rv;
}
return NS_OK;
}
nsThreadPool::~nsThreadPool()
{
NS_RELEASE(mThreads);
NS_RELEASE(mRequests);
}
NS_IMPL_ISUPPORTS(nsThreadPool, nsIThreadPool::GetIID());
NS_IMETHODIMP
nsThreadPool::DispatchRequest(nsIRunnable* runnable)
{
nsresult rv;
PR_CEnterMonitor(this);
rv = mRequests->AppendElement(runnable);
if (NS_SUCCEEDED(rv))
PR_CNotify(this);
PR_CExitMonitor(this);
return rv;
}
nsIRunnable*
nsThreadPool::Dequeue()
{
PR_CEnterMonitor(this);
NS_ASSERTION(mRequests->Count() > 0, "request queue out of sync");
nsIRunnable* request = (nsIRunnable*)(*mRequests)[0];
PRBool removed = mRequests->RemoveElementAt(0);
NS_ASSERTION(removed, "nsISupportsArray broken");
PR_CExitMonitor(this);
return request;
}
NS_BASE nsresult
NS_NewThreadPool(nsIThreadPool* *result,
PRUint32 minThreads, PRUint32 maxThreads,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state)
{
nsresult rv;
nsThreadPool* pool = new nsThreadPool(minThreads, maxThreads);
if (pool == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
rv = pool->Init(stackSize, type, priority, scope, state);
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()
{
PR_CEnterMonitor(mPool);
while (PR_TRUE) {
PRStatus status = PR_CWait(mPool, PR_INTERVAL_NO_TIMEOUT);
if (status != PR_SUCCESS) break; // interrupted -- quit
nsIRunnable* runnable = mPool->Dequeue();
nsresult rv = NS_OK;
rv = runnable->Run();
NS_ASSERTION(NS_SUCCEEDED(rv), "runnable failed");
}
PR_CExitMonitor(mPool);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////

105
xpcom/threads/nsThread.h Normal file
Просмотреть файл

@ -0,0 +1,105 @@
/* -*- 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.
*/
#ifndef nsThread_h__
#define nsThread_h__
#include "nsIThread.h"
#include "nsISupportsArray.h"
class nsThread : public nsIThread
{
public:
NS_DECL_ISUPPORTS
// nsIThread methods:
NS_IMETHOD Join();
NS_IMETHOD GetPriority(PRThreadPriority *result);
NS_IMETHOD SetPriority(PRThreadPriority value);
NS_IMETHOD Interrupt();
NS_IMETHOD GetScope(PRThreadScope *result);
NS_IMETHOD GetType(PRThreadType *result);
NS_IMETHOD GetState(PRThreadState *result);
// nsThread methods:
nsThread();
virtual ~nsThread();
nsresult Init(nsIRunnable* runnable,
PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state);
static void Main(void* arg);
protected:
PRThread* mThread;
nsIRunnable* mRunnable;
};
////////////////////////////////////////////////////////////////////////////////
class nsThreadPool : public nsIThreadPool
{
public:
NS_DECL_ISUPPORTS
// nsIThreadPool methods:
NS_IMETHOD DispatchRequest(nsIRunnable* runnable);
// nsThreadPool methods:
nsThreadPool(PRUint32 minThreads, PRUint32 maxThreads);
virtual ~nsThreadPool();
nsresult Init(PRUint32 stackSize,
PRThreadType type,
PRThreadPriority priority,
PRThreadScope scope,
PRThreadState state);
nsIRunnable* Dequeue();
protected:
nsISupportsArray* mThreads;
nsISupportsArray* mRequests;
PRUint32 mMinThreads;
PRUint32 mMaxThreads;
};
////////////////////////////////////////////////////////////////////////////////
class nsThreadPoolRunnable : public nsIRunnable
{
public:
NS_DECL_ISUPPORTS
// nsIRunnable methods:
NS_IMETHOD Run();
// nsThreadPoolRunnable methods:
nsThreadPoolRunnable(nsThreadPool* pool);
virtual ~nsThreadPoolRunnable();
protected:
nsThreadPool* mPool;
};
////////////////////////////////////////////////////////////////////////////////
#endif // nsThread_h__