Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Attributes | Private Member Functions
nsThread Class Reference

#include <nsThread.h>

Inheritance diagram for nsThread:
Inheritance graph
[legend]
Collaboration diagram for nsThread:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS NS_DECL_NSITHREAD nsThread ()
nsresult RegisterThreadSelf ()
void SetPRThread (PRThread *thread)
void WaitUntilReadyToStartMain ()
void join ()
void interrupt ()
PRThread GetPRThread ()
void init (in nsIRunnable aRunnable, in PRUint32 aStackSize, in PRThreadPriority aPriority, in PRThreadScope aScope, in PRThreadState aState)
void sleep (in PRUint32 msec)

Static Public Member Functions

static void PR_CALLBACK Main (void *arg)
static void PR_CALLBACK Exit (void *arg)
static void PR_CALLBACK Shutdown ()
static NS_METHOD Create (nsISupports *outer, const nsIID &aIID, void **aInstancePtr)
static NS_COM nsresult GetCurrent (nsIThread **result)
static NS_COM nsresult GetIThread (PRThread *prthread, nsIThread **result)
static NS_COM nsresult SetMainThread ()
static NS_COM nsresult GetMainThread (nsIThread **result)
static NS_COM PRBool IsMainThread ()

Public Attributes

const PRUint32 PRIORITY_LOW = 0
const PRUint32 PRIORITY_NORMAL = 1
const PRUint32 PRIORITY_HIGH = 2
const PRUint32 PRIORITY_URGENT = 3
const PRUint32 SCOPE_LOCAL = 0
const PRUint32 SCOPE_GLOBAL = 1
const PRUint32 SCOPE_BOUND = 2
const PRUint32 STATE_JOINABLE = 0
const PRUint32 STATE_UNJOINABLE = 1
attribute PRThreadPriority priority
readonly attribute PRThreadScope scope
readonly attribute PRThreadState state
readonly attribute nsIThread currentThread

Static Public Attributes

static PRUintn kIThreadSelfIndex = 0

Protected Attributes

PRThreadmThread
nsCOMPtr< nsIRunnablemRunnable
PRBool mDead
PRLockmStartLock

Private Member Functions

 ~nsThread ()

Detailed Description

Definition at line 56 of file nsThread.h.


Constructor & Destructor Documentation

Definition at line 66 of file nsThread.cpp.

    : mThread(nsnull), mDead(PR_FALSE), mStartLock(nsnull)
{
#if defined(PR_LOGGING)
    //
    // Initialize the global PRLogModule for nsIThread logging 
    // if necessary...
    //
    if (nsIThreadLog == nsnull) {
        nsIThreadLog = PR_NewLogModule("nsIThread");
    }
#endif /* PR_LOGGING */

    // enforce matching of constants to enums in prthread.h
    NS_ASSERTION(int(nsIThread::PRIORITY_LOW)     == int(PR_PRIORITY_LOW) &&
                 int(nsIThread::PRIORITY_NORMAL)  == int(PRIORITY_NORMAL) &&
                 int(nsIThread::PRIORITY_HIGH)    == int(PRIORITY_HIGH) &&
                 int(nsIThread::PRIORITY_URGENT)  == int(PRIORITY_URGENT) &&
                 int(nsIThread::SCOPE_LOCAL)      == int(PR_LOCAL_THREAD) &&
                 int(nsIThread::SCOPE_GLOBAL)     == int(PR_GLOBAL_THREAD) &&
                 int(nsIThread::STATE_JOINABLE)   == int(PR_JOINABLE_THREAD) &&
                 int(nsIThread::STATE_UNJOINABLE) == int(PR_UNJOINABLE_THREAD),
                 "Bad constant in nsIThread!");
}

Here is the caller graph for this function:

nsThread::~nsThread ( ) [private]

Definition at line 91 of file nsThread.cpp.

{
    if (mStartLock)
        PR_DestroyLock(mStartLock);

    PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
           ("nsIThread %p destroyed\n", this));

    // This code used to free the nsIThreadLog loginfo stuff
    // Don't do that; loginfo structures are owned by nspr
    // and would be freed if we ever called PR_Cleanup()
    // see bug 142072
}

Here is the call graph for this function:


Member Function Documentation

NS_METHOD nsThread::Create ( nsISupports *  outer,
const nsIID aIID,
void **  aInstancePtr 
) [static]

Definition at line 159 of file nsThread.cpp.

{
    nsThread* thread = new nsThread();
    if (!thread) return NS_ERROR_OUT_OF_MEMORY;
    nsresult rv = thread->QueryInterface(aIID, aResult);
    if (NS_FAILED(rv)) delete thread;
    return rv;
}

Here is the call graph for this function:

void nsThread::Exit ( void arg) [static]

Definition at line 138 of file nsThread.cpp.

{
    nsThread* self = (nsThread*)arg;

    if (self->mDead) {
        NS_ERROR("attempt to Exit() thread twice");
        return;
    }

    self->mDead = PR_TRUE;

#if defined(PR_LOGGING)
    if (nsIThreadLog) {
        PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
               ("nsIThread %p exited\n", self));
    }
#endif
    NS_RELEASE(self);
}

Here is the caller graph for this function:

NS_COM nsresult nsIThread::GetCurrent ( nsIThread **  result) [static, inherited]

Definition at line 402 of file nsThread.cpp.

{
    return GetIThread(PR_GetCurrentThread(), result);
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM nsresult nsIThread::GetIThread ( PRThread prthread,
nsIThread **  result 
) [static, inherited]

Definition at line 408 of file nsThread.cpp.

{
    PRStatus status;
    nsThread* thread;

    if (nsThread::kIThreadSelfIndex == 0) {
        status = PR_NewThreadPrivateIndex(&nsThread::kIThreadSelfIndex, nsThread::Exit);
        if (status != PR_SUCCESS) return NS_ERROR_FAILURE;
    }

    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;
        NS_ADDREF(thread);      // released by Exit
        thread->SetPRThread(prthread);
        nsresult rv = thread->RegisterThreadSelf();
        if (NS_FAILED(rv)) return rv;
    }
    NS_ADDREF(thread);
    *result = thread;
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM nsresult nsIThread::GetMainThread ( nsIThread **  result) [static, inherited]

Definition at line 448 of file nsThread.cpp.

{
    NS_ASSERTION(result, "bad result pointer");
    if (gMainThread == 0)
        return NS_ERROR_FAILURE;
    *result = gMainThread;
    NS_ADDREF(gMainThread);
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Here is the caller graph for this function:

void nsIThread::init ( in nsIRunnable  aRunnable,
in PRUint32  aStackSize,
in PRThreadPriority  aPriority,
in PRThreadScope  aScope,
in PRThreadState  aState 
) [inherited]
void nsIThread::interrupt ( ) [inherited]
NS_COM PRBool nsIThread::IsMainThread ( ) [static, inherited]

Definition at line 459 of file nsThread.cpp.

{
    if (gMainThread == 0)
        return PR_TRUE;
    
    PRThread *theMainThread;
    gMainThread->GetPRThread(&theMainThread);
    return theMainThread == PR_GetCurrentThread();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIThread::join ( ) [inherited]
void nsThread::Main ( void arg) [static]

Definition at line 106 of file nsThread.cpp.

{
    nsThread* self = (nsThread*)arg;

    self->WaitUntilReadyToStartMain();

    nsresult rv = NS_OK;
    rv = self->RegisterThreadSelf();
    NS_ASSERTION(rv == NS_OK, "failed to set thread self");

    PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
           ("nsIThread %p start run %p\n", self, self->mRunnable.get()));
    rv = self->mRunnable->Run();
    NS_ASSERTION(NS_SUCCEEDED(rv), "runnable failed");

#ifdef DEBUG
    // Because a thread can die after gMainThread dies and takes nsIThreadLog with it,
    // we need to check for it being null so that we don't crash on shutdown.
    if (nsIThreadLog) {
        PRThreadState state;
        rv = self->GetState(&state);
        PR_LOG(nsIThreadLog, PR_LOG_DEBUG,
               ("nsIThread %p end run %p\n", self, self->mRunnable.get()));
    }
#endif

    // explicitly drop the runnable now in case there are circular references
    // between it and the thread object
    self->mRunnable = nsnull;
}

Here is the call graph for this function:

Definition at line 377 of file nsThread.cpp.

{
    PRStatus status;

    if (kIThreadSelfIndex == 0) {
        status = PR_NewThreadPrivateIndex(&kIThreadSelfIndex, Exit);
        if (status != PR_SUCCESS) return NS_ERROR_FAILURE;
    }

    status = PR_SetThreadPrivate(kIThreadSelfIndex, this);
    if (status != PR_SUCCESS) return NS_ERROR_FAILURE;

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM nsresult nsIThread::SetMainThread ( ) [static, inherited]

Definition at line 436 of file nsThread.cpp.

{
    // strictly speaking, it could be set twice. but practically speaking,
    // it's almost certainly an error if it is
    if (gMainThread != 0) {
        NS_ERROR("Setting main thread twice?");
        return NS_ERROR_FAILURE;
    }
    return GetCurrent(&gMainThread);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsThread::SetPRThread ( PRThread thread) [inline]

Definition at line 68 of file nsThread.h.

{ mThread = thread; }

Here is the caller graph for this function:

void nsThread::Shutdown ( void  ) [static]

Definition at line 470 of file nsThread.cpp.

{
    if (gMainThread) {
        // In most recent versions of NSPR the main thread's destructor
        // callback will get called.
        // In older versions of NSPR it will not get called,
        // (unless we call PR_Cleanup).
        // Because of that we:
        // - call the function ourselves
        // - set the data pointer to NULL to ensure the function will
        //   not get called again by NSPR
        // The PR_SetThreadPrivate call does both of these.
        // See also bugs 379550, 362768.
        PR_SetThreadPrivate(kIThreadSelfIndex, NULL);
        nsrefcnt cnt;
        NS_RELEASE2(gMainThread, cnt);
        NS_WARN_IF_FALSE(cnt == 0, "Main thread being held past XPCOM shutdown.");
        gMainThread = nsnull;
        
        kIThreadSelfIndex = 0;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIThread::sleep ( in PRUint32  msec) [inherited]

Definition at line 393 of file nsThread.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 102 of file nsIThread.idl.

PRUintn nsThread::kIThreadSelfIndex = 0 [static]

Definition at line 75 of file nsThread.h.

PRBool nsThread::mDead [protected]

Definition at line 86 of file nsThread.h.

Definition at line 85 of file nsThread.h.

Definition at line 87 of file nsThread.h.

Definition at line 84 of file nsThread.h.

Definition at line 87 of file nsIThread.idl.

Definition at line 74 of file nsIThread.idl.

Definition at line 72 of file nsIThread.idl.

Definition at line 73 of file nsIThread.idl.

Definition at line 75 of file nsIThread.idl.

Definition at line 88 of file nsIThread.idl.

Definition at line 79 of file nsIThread.idl.

Definition at line 78 of file nsIThread.idl.

Definition at line 77 of file nsIThread.idl.

Definition at line 89 of file nsIThread.idl.

Definition at line 81 of file nsIThread.idl.

Definition at line 82 of file nsIThread.idl.


The documentation for this class was generated from the following files: