michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "primpl.h" michael@0: michael@0: _PRCPU *_pr_primordialCPU = NULL; michael@0: michael@0: PRInt32 _pr_md_idle_cpus; /* number of idle cpus */ michael@0: /* michael@0: * The idle threads in MxN models increment/decrement _pr_md_idle_cpus. michael@0: * If _PR_HAVE_ATOMIC_OPS is not defined, they can't use the atomic michael@0: * increment/decrement routines (which are based on PR_Lock/PR_Unlock), michael@0: * because PR_Lock asserts that the calling thread is not an idle thread. michael@0: * So we use a _MDLock to protect _pr_md_idle_cpus. michael@0: */ michael@0: #if !defined(_PR_LOCAL_THREADS_ONLY) && !defined(_PR_GLOBAL_THREADS_ONLY) michael@0: #ifndef _PR_HAVE_ATOMIC_OPS michael@0: static _MDLock _pr_md_idle_cpus_lock; michael@0: #endif michael@0: #endif michael@0: PRUintn _pr_numCPU; michael@0: PRInt32 _pr_cpus_exit; michael@0: PRUint32 _pr_cpu_affinity_mask = 0; michael@0: michael@0: #if !defined (_PR_GLOBAL_THREADS_ONLY) michael@0: michael@0: static PRUintn _pr_cpuID; michael@0: michael@0: static void PR_CALLBACK _PR_CPU_Idle(void *); michael@0: michael@0: static _PRCPU *_PR_CreateCPU(void); michael@0: static PRStatus _PR_StartCPU(_PRCPU *cpu, PRThread *thread); michael@0: michael@0: #if !defined(_PR_LOCAL_THREADS_ONLY) michael@0: static void _PR_RunCPU(void *arg); michael@0: #endif michael@0: michael@0: void _PR_InitCPUs() michael@0: { michael@0: PRThread *me = _PR_MD_CURRENT_THREAD(); michael@0: michael@0: if (_native_threads_only) michael@0: return; michael@0: michael@0: _pr_cpuID = 0; michael@0: _MD_NEW_LOCK( &_pr_cpuLock); michael@0: #if !defined(_PR_LOCAL_THREADS_ONLY) && !defined(_PR_GLOBAL_THREADS_ONLY) michael@0: #ifndef _PR_HAVE_ATOMIC_OPS michael@0: _MD_NEW_LOCK(&_pr_md_idle_cpus_lock); michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef _PR_LOCAL_THREADS_ONLY michael@0: michael@0: #ifdef HAVE_CUSTOM_USER_THREADS michael@0: _PR_MD_CREATE_PRIMORDIAL_USER_THREAD(me); michael@0: #endif michael@0: michael@0: /* Now start the first CPU. */ michael@0: _pr_primordialCPU = _PR_CreateCPU(); michael@0: _pr_numCPU = 1; michael@0: _PR_StartCPU(_pr_primordialCPU, me); michael@0: michael@0: _PR_MD_SET_CURRENT_CPU(_pr_primordialCPU); michael@0: michael@0: /* Initialize cpu for current thread (could be different from me) */ michael@0: _PR_MD_CURRENT_THREAD()->cpu = _pr_primordialCPU; michael@0: michael@0: _PR_MD_SET_LAST_THREAD(me); michael@0: michael@0: #else /* Combined MxN model */ michael@0: michael@0: _pr_primordialCPU = _PR_CreateCPU(); michael@0: _pr_numCPU = 1; michael@0: _PR_CreateThread(PR_SYSTEM_THREAD, michael@0: _PR_RunCPU, michael@0: _pr_primordialCPU, michael@0: PR_PRIORITY_NORMAL, michael@0: PR_GLOBAL_THREAD, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0, michael@0: _PR_IDLE_THREAD); michael@0: michael@0: #endif /* _PR_LOCAL_THREADS_ONLY */ michael@0: michael@0: _PR_MD_INIT_CPUS(); michael@0: } michael@0: michael@0: #ifdef WINNT michael@0: /* michael@0: * Right now this function merely stops the CPUs and does michael@0: * not do any other cleanup. michael@0: * michael@0: * It is only implemented for WINNT because bug 161998 only michael@0: * affects the WINNT version of NSPR, but it would be nice michael@0: * to implement this function for other platforms too. michael@0: */ michael@0: void _PR_CleanupCPUs(void) michael@0: { michael@0: PRUintn i; michael@0: PRCList *qp; michael@0: _PRCPU *cpu; michael@0: michael@0: _pr_cpus_exit = 1; michael@0: for (i = 0; i < _pr_numCPU; i++) { michael@0: _PR_MD_WAKEUP_WAITER(NULL); michael@0: } michael@0: for (qp = _PR_CPUQ().next; qp != &_PR_CPUQ(); qp = qp->next) { michael@0: cpu = _PR_CPU_PTR(qp); michael@0: _PR_MD_JOIN_THREAD(&cpu->thread->md); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: static _PRCPUQueue *_PR_CreateCPUQueue(void) michael@0: { michael@0: PRInt32 index; michael@0: _PRCPUQueue *cpuQueue; michael@0: cpuQueue = PR_NEWZAP(_PRCPUQueue); michael@0: michael@0: _MD_NEW_LOCK( &cpuQueue->runQLock ); michael@0: _MD_NEW_LOCK( &cpuQueue->sleepQLock ); michael@0: _MD_NEW_LOCK( &cpuQueue->miscQLock ); michael@0: michael@0: for (index = 0; index < PR_PRIORITY_LAST + 1; index++) michael@0: PR_INIT_CLIST( &(cpuQueue->runQ[index]) ); michael@0: PR_INIT_CLIST( &(cpuQueue->sleepQ) ); michael@0: PR_INIT_CLIST( &(cpuQueue->pauseQ) ); michael@0: PR_INIT_CLIST( &(cpuQueue->suspendQ) ); michael@0: PR_INIT_CLIST( &(cpuQueue->waitingToJoinQ) ); michael@0: michael@0: cpuQueue->numCPUs = 1; michael@0: michael@0: return cpuQueue; michael@0: } michael@0: michael@0: /* michael@0: * Create a new CPU. michael@0: * michael@0: * This function initializes enough of the _PRCPU structure so michael@0: * that it can be accessed safely by a global thread or another michael@0: * CPU. This function does not create the native thread that michael@0: * will run the CPU nor does it initialize the parts of _PRCPU michael@0: * that must be initialized by that native thread. michael@0: * michael@0: * The reason we cannot simply have the native thread create michael@0: * and fully initialize a new CPU is that we need to be able to michael@0: * create a usable _pr_primordialCPU in _PR_InitCPUs without michael@0: * assuming that the primordial CPU thread we created can run michael@0: * during NSPR initialization. For example, on Windows while michael@0: * new threads can be created by DllMain, they won't be able michael@0: * to run during DLL initialization. If NSPR is initialized michael@0: * by DllMain, the primordial CPU thread won't run until DLL michael@0: * initialization is finished. michael@0: */ michael@0: static _PRCPU *_PR_CreateCPU(void) michael@0: { michael@0: _PRCPU *cpu; michael@0: michael@0: cpu = PR_NEWZAP(_PRCPU); michael@0: if (cpu) { michael@0: cpu->queue = _PR_CreateCPUQueue(); michael@0: if (!cpu->queue) { michael@0: PR_DELETE(cpu); michael@0: return NULL; michael@0: } michael@0: } michael@0: return cpu; michael@0: } michael@0: michael@0: /* michael@0: * Start a new CPU. michael@0: * michael@0: * 'cpu' is a _PRCPU structure created by _PR_CreateCPU(). michael@0: * 'thread' is the native thread that will run the CPU. michael@0: * michael@0: * If this function fails, 'cpu' is destroyed. michael@0: */ michael@0: static PRStatus _PR_StartCPU(_PRCPU *cpu, PRThread *thread) michael@0: { michael@0: /* michael@0: ** Start a new cpu. The assumption this code makes is that the michael@0: ** underlying operating system creates a stack to go with the new michael@0: ** native thread. That stack will be used by the cpu when pausing. michael@0: */ michael@0: michael@0: PR_ASSERT(!_native_threads_only); michael@0: michael@0: cpu->last_clock = PR_IntervalNow(); michael@0: michael@0: /* Before we create any threads on this CPU we have to michael@0: * set the current CPU michael@0: */ michael@0: _PR_MD_SET_CURRENT_CPU(cpu); michael@0: _PR_MD_INIT_RUNNING_CPU(cpu); michael@0: thread->cpu = cpu; michael@0: michael@0: cpu->idle_thread = _PR_CreateThread(PR_SYSTEM_THREAD, michael@0: _PR_CPU_Idle, michael@0: (void *)cpu, michael@0: PR_PRIORITY_NORMAL, michael@0: PR_LOCAL_THREAD, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0, michael@0: _PR_IDLE_THREAD); michael@0: michael@0: if (!cpu->idle_thread) { michael@0: /* didn't clean up CPU queue XXXMB */ michael@0: PR_DELETE(cpu); michael@0: return PR_FAILURE; michael@0: } michael@0: PR_ASSERT(cpu->idle_thread->cpu == cpu); michael@0: michael@0: cpu->idle_thread->no_sched = 0; michael@0: michael@0: cpu->thread = thread; michael@0: michael@0: if (_pr_cpu_affinity_mask) michael@0: PR_SetThreadAffinityMask(thread, _pr_cpu_affinity_mask); michael@0: michael@0: /* Created and started a new CPU */ michael@0: _PR_CPU_LIST_LOCK(); michael@0: cpu->id = _pr_cpuID++; michael@0: PR_APPEND_LINK(&cpu->links, &_PR_CPUQ()); michael@0: _PR_CPU_LIST_UNLOCK(); michael@0: michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: #if !defined(_PR_GLOBAL_THREADS_ONLY) && !defined(_PR_LOCAL_THREADS_ONLY) michael@0: /* michael@0: ** This code is used during a cpu's initial creation. michael@0: */ michael@0: static void _PR_RunCPU(void *arg) michael@0: { michael@0: _PRCPU *cpu = (_PRCPU *)arg; michael@0: PRThread *me = _PR_MD_CURRENT_THREAD(); michael@0: michael@0: PR_ASSERT(NULL != me); michael@0: michael@0: /* michael@0: * _PR_StartCPU calls _PR_CreateThread to create the michael@0: * idle thread. Because _PR_CreateThread calls PR_Lock, michael@0: * the current thread has to remain a global thread michael@0: * during the _PR_StartCPU call so that it can wait for michael@0: * the lock if the lock is held by another thread. If michael@0: * we clear the _PR_GLOBAL_SCOPE flag in michael@0: * _PR_MD_CREATE_PRIMORDIAL_THREAD, the current thread michael@0: * will be treated as a local thread and have trouble michael@0: * waiting for the lock because the CPU is not fully michael@0: * constructed yet. michael@0: * michael@0: * After the CPU is started, it is safe to mark the michael@0: * current thread as a local thread. michael@0: */ michael@0: michael@0: #ifdef HAVE_CUSTOM_USER_THREADS michael@0: _PR_MD_CREATE_PRIMORDIAL_USER_THREAD(me); michael@0: #endif michael@0: michael@0: me->no_sched = 1; michael@0: _PR_StartCPU(cpu, me); michael@0: michael@0: #ifdef HAVE_CUSTOM_USER_THREADS michael@0: me->flags &= (~_PR_GLOBAL_SCOPE); michael@0: #endif michael@0: michael@0: _PR_MD_SET_CURRENT_CPU(cpu); michael@0: _PR_MD_SET_CURRENT_THREAD(cpu->thread); michael@0: me->cpu = cpu; michael@0: michael@0: while(1) { michael@0: PRInt32 is; michael@0: if (!_PR_IS_NATIVE_THREAD(me)) _PR_INTSOFF(is); michael@0: _PR_MD_START_INTERRUPTS(); michael@0: _PR_MD_SWITCH_CONTEXT(me); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: static void PR_CALLBACK _PR_CPU_Idle(void *_cpu) michael@0: { michael@0: _PRCPU *cpu = (_PRCPU *)_cpu; michael@0: PRThread *me = _PR_MD_CURRENT_THREAD(); michael@0: michael@0: PR_ASSERT(NULL != me); michael@0: michael@0: me->cpu = cpu; michael@0: cpu->idle_thread = me; michael@0: if (_MD_LAST_THREAD()) michael@0: _MD_LAST_THREAD()->no_sched = 0; michael@0: if (!_PR_IS_NATIVE_THREAD(me)) _PR_MD_SET_INTSOFF(0); michael@0: while(1) { michael@0: PRInt32 is; michael@0: PRIntervalTime timeout; michael@0: if (!_PR_IS_NATIVE_THREAD(me)) _PR_INTSOFF(is); michael@0: michael@0: _PR_RUNQ_LOCK(cpu); michael@0: #if !defined(_PR_LOCAL_THREADS_ONLY) && !defined(_PR_GLOBAL_THREADS_ONLY) michael@0: #ifdef _PR_HAVE_ATOMIC_OPS michael@0: _PR_MD_ATOMIC_INCREMENT(&_pr_md_idle_cpus); michael@0: #else michael@0: _PR_MD_LOCK(&_pr_md_idle_cpus_lock); michael@0: _pr_md_idle_cpus++; michael@0: _PR_MD_UNLOCK(&_pr_md_idle_cpus_lock); michael@0: #endif /* _PR_HAVE_ATOMIC_OPS */ michael@0: #endif michael@0: /* If someone on runq; do a nonblocking PAUSECPU */ michael@0: if (_PR_RUNQREADYMASK(me->cpu) != 0) { michael@0: _PR_RUNQ_UNLOCK(cpu); michael@0: timeout = PR_INTERVAL_NO_WAIT; michael@0: } else { michael@0: _PR_RUNQ_UNLOCK(cpu); michael@0: michael@0: _PR_SLEEPQ_LOCK(cpu); michael@0: if (PR_CLIST_IS_EMPTY(&_PR_SLEEPQ(me->cpu))) { michael@0: timeout = PR_INTERVAL_NO_TIMEOUT; michael@0: } else { michael@0: PRThread *wakeThread; michael@0: wakeThread = _PR_THREAD_PTR(_PR_SLEEPQ(me->cpu).next); michael@0: timeout = wakeThread->sleep; michael@0: } michael@0: _PR_SLEEPQ_UNLOCK(cpu); michael@0: } michael@0: michael@0: /* Wait for an IO to complete */ michael@0: (void)_PR_MD_PAUSE_CPU(timeout); michael@0: michael@0: #ifdef WINNT michael@0: if (_pr_cpus_exit) { michael@0: /* _PR_CleanupCPUs tells us to exit */ michael@0: _PR_MD_END_THREAD(); michael@0: } michael@0: #endif michael@0: michael@0: #if !defined(_PR_LOCAL_THREADS_ONLY) && !defined(_PR_GLOBAL_THREADS_ONLY) michael@0: #ifdef _PR_HAVE_ATOMIC_OPS michael@0: _PR_MD_ATOMIC_DECREMENT(&_pr_md_idle_cpus); michael@0: #else michael@0: _PR_MD_LOCK(&_pr_md_idle_cpus_lock); michael@0: _pr_md_idle_cpus--; michael@0: _PR_MD_UNLOCK(&_pr_md_idle_cpus_lock); michael@0: #endif /* _PR_HAVE_ATOMIC_OPS */ michael@0: #endif michael@0: michael@0: _PR_ClockInterrupt(); michael@0: michael@0: /* Now schedule any thread that is on the runq michael@0: * INTS must be OFF when calling PR_Schedule() michael@0: */ michael@0: me->state = _PR_RUNNABLE; michael@0: _PR_MD_SWITCH_CONTEXT(me); michael@0: if (!_PR_IS_NATIVE_THREAD(me)) _PR_FAST_INTSON(is); michael@0: } michael@0: } michael@0: #endif /* _PR_GLOBAL_THREADS_ONLY */ michael@0: michael@0: PR_IMPLEMENT(void) PR_SetConcurrency(PRUintn numCPUs) michael@0: { michael@0: #if defined(_PR_GLOBAL_THREADS_ONLY) || defined(_PR_LOCAL_THREADS_ONLY) michael@0: michael@0: /* do nothing */ michael@0: michael@0: #else /* combined, MxN thread model */ michael@0: michael@0: PRUintn newCPU; michael@0: _PRCPU *cpu; michael@0: PRThread *thr; michael@0: michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: if (_native_threads_only) michael@0: return; michael@0: michael@0: _PR_CPU_LIST_LOCK(); michael@0: if (_pr_numCPU < numCPUs) { michael@0: newCPU = numCPUs - _pr_numCPU; michael@0: _pr_numCPU = numCPUs; michael@0: } else newCPU = 0; michael@0: _PR_CPU_LIST_UNLOCK(); michael@0: michael@0: for (; newCPU; newCPU--) { michael@0: cpu = _PR_CreateCPU(); michael@0: thr = _PR_CreateThread(PR_SYSTEM_THREAD, michael@0: _PR_RunCPU, michael@0: cpu, michael@0: PR_PRIORITY_NORMAL, michael@0: PR_GLOBAL_THREAD, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0, michael@0: _PR_IDLE_THREAD); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(_PRCPU *) _PR_GetPrimordialCPU(void) michael@0: { michael@0: if (_pr_primordialCPU) michael@0: return _pr_primordialCPU; michael@0: else michael@0: return _PR_MD_CURRENT_CPU(); michael@0: }