Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
ntthread.c File Reference
#include "primpl.h"
#include <process.h>

Go to the source code of this file.

Defines

#define POST_SWITCH_WORK()

Functions

 __declspec (thread)
void _PR_MD_EARLY_INIT ()
void _PR_MD_CLEANUP_BEFORE_EXIT (void)
PRStatus _PR_MD_INIT_THREAD (PRThread *thread)
static unsigned __stdcall pr_root (void *arg)
PRStatus _PR_MD_CREATE_THREAD (PRThread *thread, void(*start)(void *), PRThreadPriority priority, PRThreadScope scope, PRThreadState state, PRUint32 stackSize)
void _PR_MD_JOIN_THREAD (_MDThread *md)
void _PR_MD_END_THREAD (void)
void _PR_MD_YIELD (void)
void _PR_MD_SET_PRIORITY (_MDThread *thread, PRThreadPriority newPri)
void _PR_MD_CLEAN_THREAD (PRThread *thread)
void _PR_MD_EXIT_THREAD (PRThread *thread)
void _PR_MD_EXIT (PRIntn status)
PRInt32 _PR_MD_SETTHREADAFFINITYMASK (PRThread *thread, PRUint32 mask)
PRInt32 _PR_MD_GETTHREADAFFINITYMASK (PRThread *thread, PRUint32 *mask)
void _PR_MD_SUSPEND_CPU (_PRCPU *cpu)
void _PR_MD_RESUME_CPU (_PRCPU *cpu)
void _PR_MD_SUSPEND_THREAD (PRThread *thread)
void _PR_MD_RESUME_THREAD (PRThread *thread)
PRThread_MD_CURRENT_THREAD (void)

Variables

PRLock_pr_schedLock = NULL
_PRInterruptTable _pr_interruptTable [] = { { 0 } }
BOOL _pr_use_static_tls = TRUE

Define Documentation

Value:
PR_BEGIN_MACRO \
        PRThread *restarted_io = \
            (_pr_use_static_tls ? _pr_io_restarted_io \
            : (PRThread *) TlsGetValue(_pr_io_restartedIOIndex)); \
        if (restarted_io) { \
            _nt_handle_restarted_io(restarted_io); \
        } \
        _PR_MD_LAST_THREAD()->no_sched = 0; \
    PR_END_MACRO

Function Documentation

__declspec ( thread  )

Definition at line 46 of file ntthread.c.

            : (PRThread *) TlsGetValue(_pr_io_restartedIOIndex)); \
        if (restarted_io) { \
            _nt_handle_restarted_io(restarted_io); \
        } \
        _PR_MD_LAST_THREAD()->no_sched = 0; \
    PR_END_MACRO

void
_nt_handle_restarted_io(PRThread *restarted_io)
{
    /* After the switch we can resume an IO if needed.
     * XXXMB - this needs to be done in create thread, since that could
     * be the result for a context switch too..
     */
    PR_ASSERT(restarted_io->io_suspended == PR_TRUE);
    PR_ASSERT(restarted_io->md.thr_bound_cpu == restarted_io->cpu);

    _PR_THREAD_LOCK(restarted_io);
    if (restarted_io->io_pending == PR_FALSE) {

        /* The IO already completed, put us back on the runq. */
        int pri = restarted_io->priority;

        restarted_io->state = _PR_RUNNABLE;
        _PR_RUNQ_LOCK(restarted_io->cpu);
        _PR_ADD_RUNQ(restarted_io, restarted_io->cpu, pri);
        _PR_RUNQ_UNLOCK(restarted_io->cpu);
    } else {
        _PR_SLEEPQ_LOCK(restarted_io->cpu);
        _PR_ADD_SLEEPQ(restarted_io, restarted_io->sleep);
        _PR_SLEEPQ_UNLOCK(restarted_io->cpu);
    }
    restarted_io->io_suspended = PR_FALSE;
    restarted_io->md.thr_bound_cpu = NULL;

    _PR_THREAD_UNLOCK(restarted_io);

    if (_pr_use_static_tls) {
        _pr_io_restarted_io = NULL;
    } else {
        TlsSetValue(_pr_io_restartedIOIndex, NULL);
    }
}

Definition at line 547 of file ntthread.c.

{
PRThread *thread;

       thread = _MD_GET_ATTACHED_THREAD();

       if (NULL == thread) {
              thread = _PRI_AttachThread(
            PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL, 0);
       }
       PR_ASSERT(thread != NULL);
       return thread;
}

Here is the call graph for this function:

Definition at line 312 of file ntthread.c.

{
    BOOL rv;

    if (thread->md.acceptex_buf) {
        PR_DELETE(thread->md.acceptex_buf);
    }

    if (thread->md.xmit_bufs) {
        PR_DELETE(thread->md.xmit_bufs);
    }

    if (thread->md.blocked_sema) {
        rv = CloseHandle(thread->md.blocked_sema);
        PR_ASSERT(rv);
        thread->md.blocked_sema = 0;
    }
       if (_native_threads_only) {
              if (thread->md.thr_event) {
                     rv = CloseHandle(thread->md.thr_event);
                     PR_ASSERT(rv);
                     thread->md.thr_event = 0;
              }
       }

    if (thread->md.handle) {
        rv = CloseHandle(thread->md.handle);
        PR_ASSERT(rv);
        thread->md.handle = 0;
    }

    /* Don't call DeleteFiber on current fiber or we'll kill the whole thread.
     * Don't call free(thread) until we've switched off the thread.
     * So put this fiber (or thread) on a list to be deleted by the idle
     * fiber next time we have a chance.
     */
    if (!(thread->flags & (_PR_ATTACHED|_PR_GLOBAL_SCOPE))) {
        _MD_LOCK(&_nt_idleLock);
        _nt_idleCount++;
        PR_APPEND_LINK(&thread->links, &_nt_idleList);
        _MD_UNLOCK(&_nt_idleLock);
    }
}

Definition at line 135 of file ntthread.c.

{
    _PR_NT_FreeSids();

    WSACleanup();

    if (!_pr_use_static_tls) {
        TlsFree(_pr_currentFiberIndex);
        TlsFree(_pr_lastFiberIndex);
        TlsFree(_pr_currentCPUIndex);
        TlsFree(_pr_intsOffIndex);
        TlsFree(_pr_io_restartedIOIndex);
    }
}

Here is the call graph for this function:

PRStatus _PR_MD_CREATE_THREAD ( PRThread thread,
void(*)(void *)  start,
PRThreadPriority  priority,
PRThreadScope  scope,
PRThreadState  state,
PRUint32  stackSize 
)

Definition at line 203 of file ntthread.c.

{

    thread->md.start = start;
    thread->md.handle = (HANDLE) _beginthreadex(
                    NULL,
                    thread->stack->stackSize,
                    pr_root,
                    (void *)thread,
                    CREATE_SUSPENDED,
                    &(thread->id));
    if(!thread->md.handle) {
        PRErrorCode prerror;
        thread->md.fiber_last_error = GetLastError();
        switch (errno) {
            case ENOMEM:
                prerror = PR_OUT_OF_MEMORY_ERROR;
                break;
            case EAGAIN:
                prerror = PR_INSUFFICIENT_RESOURCES_ERROR;
                break;
            case EINVAL:
                prerror = PR_INVALID_ARGUMENT_ERROR;
                break;
            default:
                prerror = PR_UNKNOWN_ERROR;
        }
        PR_SetError(prerror, errno);
        return PR_FAILURE;
    }

    thread->md.id = thread->id;
    /*
     * On windows, a thread is created with a thread priority of
     * THREAD_PRIORITY_NORMAL.
     */
    if (priority != PR_PRIORITY_NORMAL) {
        _PR_MD_SET_PRIORITY(&(thread->md), priority);
    }

    /* Activate the thread */
    if ( ResumeThread( thread->md.handle ) != -1)
        return PR_SUCCESS;

    PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
    return PR_FAILURE;
}

Here is the call graph for this function:

Definition at line 112 of file ntthread.c.

{
    _MD_NEW_LOCK( &_nt_idleLock );
    _nt_idleCount = 0;
    PR_INIT_CLIST(&_nt_idleList);

#if 0
    /* Make the clock tick at least once per millisecond */
    if ( timeBeginPeriod(1) == TIMERR_NOCANDO) {
        /* deep yoghurt; clock doesn't tick fast enough! */
        PR_ASSERT(0);
    }
#endif

    if (!_pr_use_static_tls) {
        _pr_currentFiberIndex = TlsAlloc();
        _pr_lastFiberIndex = TlsAlloc();
        _pr_currentCPUIndex = TlsAlloc();
        _pr_intsOffIndex = TlsAlloc();
        _pr_io_restartedIOIndex = TlsAlloc();
    }
}

Here is the call graph for this function:

Definition at line 266 of file ntthread.c.

{
    _endthreadex(0);
}
void _PR_MD_EXIT ( PRIntn  status)

Definition at line 396 of file ntthread.c.

{
    _exit(status);
}

Definition at line 357 of file ntthread.c.

{
    BOOL rv;

    if (thread->md.acceptex_buf) {
        PR_DELETE(thread->md.acceptex_buf);
    }

    if (thread->md.xmit_bufs) {
        PR_DELETE(thread->md.xmit_bufs);
    }

    if (thread->md.blocked_sema) {
        rv = CloseHandle(thread->md.blocked_sema);
        PR_ASSERT(rv);
        thread->md.blocked_sema = 0;
    }

       if (_native_threads_only) {
              if (thread->md.thr_event) {
                     rv = CloseHandle(thread->md.thr_event);
                     PR_ASSERT(rv);
                     thread->md.thr_event = 0;
              }
       }

    if (thread->md.handle) {
        rv = CloseHandle(thread->md.handle);
        PR_ASSERT(rv);
        thread->md.handle = 0;
    }

    if (thread->flags & _PR_GLOBAL_SCOPE) {
        _MD_SET_CURRENT_THREAD(NULL);
    }
}

Definition at line 503 of file ntthread.c.

{
    PRInt32 rv, system_mask;

    rv = GetProcessAffinityMask(GetCurrentProcess(), mask, &system_mask);
    
    return rv?0:-1;
}

Definition at line 151 of file ntthread.c.

{
    thread->md.overlapped.ioModel = _MD_BlockingIO;
    thread->md.overlapped.data.mdThread = &thread->md;

    if (thread->flags & _PR_GLOBAL_SCOPE) {
        if (thread->flags & (_PR_PRIMORDIAL | _PR_ATTACHED)) {
            /*
            ** Warning:
            ** --------
            ** NSPR requires a real handle to every thread.
            ** GetCurrentThread() returns a pseudo-handle which
            ** is not suitable for some thread operations (e.g.,
            ** suspending).  Therefore, get a real handle from
            ** the pseudo handle via DuplicateHandle(...)
            */
            DuplicateHandle(
                    GetCurrentProcess(),     /* Process of source handle */
                    GetCurrentThread(),      /* Pseudo Handle to dup */
                    GetCurrentProcess(),     /* Process of handle */
                    &(thread->md.handle),    /* resulting handle */
                    0L,                      /* access flags */
                    FALSE,                   /* Inheritable */
                    DUPLICATE_SAME_ACCESS);  /* Options */
        }

        /* Create the blocking IO semaphore */
        thread->md.blocked_sema = CreateSemaphore(NULL, 0, 1, NULL);
        if (thread->md.blocked_sema == NULL) {
            return PR_FAILURE;
        }
              if (_native_threads_only) {
                     /* Create the blocking IO semaphore */
                     thread->md.thr_event = CreateEvent(NULL, TRUE, FALSE, NULL);
                     if (thread->md.thr_event == NULL) {
                            return PR_FAILURE;
                     }
              }
    }

    return PR_SUCCESS;
}

Here is the call graph for this function:

Definition at line 257 of file ntthread.c.

{
    DWORD rv;

    rv = WaitForSingleObject(md->handle, INFINITE);
    PR_ASSERT(WAIT_OBJECT_0 == rv);
}

Definition at line 519 of file ntthread.c.

Definition at line 539 of file ntthread.c.

{
    if (_PR_IS_NATIVE_THREAD(thread)) {
        ResumeThread(thread->md.handle);
    }
}
void _PR_MD_SET_PRIORITY ( _MDThread thread,
PRThreadPriority  newPri 
)

Definition at line 279 of file ntthread.c.

{
    int nativePri;
    BOOL rv;

    if (newPri < PR_PRIORITY_FIRST) {
        newPri = PR_PRIORITY_FIRST;
    } else if (newPri > PR_PRIORITY_LAST) {
        newPri = PR_PRIORITY_LAST;
    }
    switch (newPri) {
        case PR_PRIORITY_LOW:
            nativePri = THREAD_PRIORITY_BELOW_NORMAL;
            break;
        case PR_PRIORITY_NORMAL:
            nativePri = THREAD_PRIORITY_NORMAL;
            break;
        case PR_PRIORITY_HIGH:
            nativePri = THREAD_PRIORITY_ABOVE_NORMAL;
            break;
        case PR_PRIORITY_URGENT:
            nativePri = THREAD_PRIORITY_HIGHEST;
    }
    rv = SetThreadPriority(thread->handle, nativePri);
    PR_ASSERT(rv);
    if (!rv) {
       PR_LOG(_pr_thread_lm, PR_LOG_MIN,
                ("PR_SetThreadPriority: can't set thread priority\n"));
    }
    return;
}

Definition at line 494 of file ntthread.c.

{
    int rv;

    rv = SetThreadAffinityMask(thread->md.handle, mask);

    return rv?0:-1;
}

Definition at line 513 of file ntthread.c.

Definition at line 525 of file ntthread.c.

{
    if (_PR_IS_NATIVE_THREAD(thread)) {
        /*
        ** There seems to be some doubt about whether or not SuspendThread
        ** is a synchronous function. The test afterwards is to help veriry
        ** that it is, which is what Microsoft says it is.
        */
        PRUintn rv = SuspendThread(thread->md.handle);
        PR_ASSERT(0xffffffffUL != rv);
    }
}

Definition at line 272 of file ntthread.c.

{
    /* Can NT really yield at all? */
    Sleep(0);
}
static unsigned __stdcall pr_root ( void arg) [static]

Definition at line 195 of file ntthread.c.

{
    PRThread *thread = (PRThread *)arg;
    thread->md.start(thread);
    return 0;
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 43 of file ntthread.c.

Definition at line 42 of file ntthread.c.

Definition at line 45 of file ntthread.c.