Back to index

lightning-sunbird  0.9+nobinonly
prustack.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "primpl.h"
00039 
00040 /* List of free stack virtual memory chunks */
00041 PRLock *_pr_stackLock;
00042 PRCList _pr_freeStacks = PR_INIT_STATIC_CLIST(&_pr_freeStacks);
00043 PRIntn _pr_numFreeStacks;
00044 PRIntn _pr_maxFreeStacks = 4;
00045 
00046 #ifdef DEBUG
00047 /*
00048 ** A variable that can be set via the debugger...
00049 */
00050 PRBool _pr_debugStacks = PR_FALSE;
00051 #endif
00052 
00053 /* How much space to leave between the stacks, at each end */
00054 #define REDZONE             (2 << _pr_pageShift)
00055 
00056 #define _PR_THREAD_STACK_PTR(_qp) \
00057     ((PRThreadStack*) ((char*) (_qp) - offsetof(PRThreadStack,links)))
00058 
00059 void _PR_InitStacks(void)
00060 {
00061     _pr_stackLock = PR_NewLock();
00062 }
00063 
00064 void _PR_CleanupStacks(void)
00065 {
00066     if (_pr_stackLock) {
00067         PR_DestroyLock(_pr_stackLock);
00068         _pr_stackLock = NULL;
00069     }
00070 }
00071 
00072 /*
00073 ** Allocate a stack for a thread.
00074 */
00075 PRThreadStack *_PR_NewStack(PRUint32 stackSize)
00076 {
00077     PRCList *qp;
00078     PRThreadStack *ts;
00079     PRThread *thr;
00080 
00081     /*
00082     ** Trim the list of free stacks. Trim it backwards, tossing out the
00083     ** oldest stack found first (this way more recent stacks have a
00084     ** chance of being present in the data cache).
00085     */
00086     PR_Lock(_pr_stackLock);
00087     qp = _pr_freeStacks.prev;
00088     while ((_pr_numFreeStacks > _pr_maxFreeStacks) && (qp != &_pr_freeStacks)) {
00089        ts = _PR_THREAD_STACK_PTR(qp);
00090        thr = _PR_THREAD_STACK_TO_PTR(ts);
00091        qp = qp->prev;
00092        /*
00093         * skip stacks which are still being used
00094         */
00095        if (thr->no_sched)
00096               continue;
00097        PR_REMOVE_LINK(&ts->links);
00098 
00099        /* Give platform OS to clear out the stack for debugging */
00100        _PR_MD_CLEAR_STACK(ts);
00101 
00102        _pr_numFreeStacks--;
00103        _PR_DestroySegment(ts->seg);
00104        PR_DELETE(ts);
00105     }
00106 
00107     /*
00108     ** Find a free thread stack. This searches the list of free'd up
00109     ** virtually mapped thread stacks.
00110     */
00111     qp = _pr_freeStacks.next;
00112     ts = 0;
00113     while (qp != &_pr_freeStacks) {
00114        ts = _PR_THREAD_STACK_PTR(qp);
00115        thr = _PR_THREAD_STACK_TO_PTR(ts);
00116        qp = qp->next;
00117        /*
00118         * skip stacks which are still being used
00119         */
00120        if ((!(thr->no_sched)) && ((ts->allocSize - 2*REDZONE) >= stackSize)) {
00121            /*
00122            ** Found a stack that is not in use and is big enough. Change
00123            ** stackSize to fit it.
00124            */
00125            stackSize = ts->allocSize - 2*REDZONE;
00126            PR_REMOVE_LINK(&ts->links);
00127            _pr_numFreeStacks--;
00128            ts->links.next = 0;
00129            ts->links.prev = 0;
00130            PR_Unlock(_pr_stackLock);
00131            goto done;
00132        }
00133        ts = 0;
00134     }
00135     PR_Unlock(_pr_stackLock);
00136 
00137     if (!ts) {
00138        /* Make a new thread stack object. */
00139        ts = PR_NEWZAP(PRThreadStack);
00140        if (!ts) {
00141            return NULL;
00142        }
00143 
00144        /*
00145        ** Assign some of the virtual space to the new stack object. We
00146        ** may not get that piece of VM, but if nothing else we will
00147        ** advance the pointer so we don't collide (unless the OS screws
00148        ** up).
00149        */
00150        ts->allocSize = stackSize + 2*REDZONE;
00151        ts->seg = _PR_NewSegment(ts->allocSize, 0);
00152        if (!ts->seg) {
00153            PR_DELETE(ts);
00154            return NULL;
00155        }
00156        }
00157 
00158   done:
00159     ts->allocBase = (char*)ts->seg->vaddr;
00160     ts->flags = _PR_STACK_MAPPED;
00161     ts->stackSize = stackSize;
00162 
00163 #ifdef HAVE_STACK_GROWING_UP
00164     ts->stackTop = ts->allocBase + REDZONE;
00165     ts->stackBottom = ts->stackTop + stackSize;
00166 #else
00167     ts->stackBottom = ts->allocBase + REDZONE;
00168     ts->stackTop = ts->stackBottom + stackSize;
00169 #endif
00170 
00171     PR_LOG(_pr_thread_lm, PR_LOG_NOTICE,
00172           ("thread stack: base=0x%x limit=0x%x bottom=0x%x top=0x%x\n",
00173            ts->allocBase, ts->allocBase + ts->allocSize - 1,
00174            ts->allocBase + REDZONE,
00175            ts->allocBase + REDZONE + stackSize - 1));
00176            
00177     _PR_MD_INIT_STACK(ts,REDZONE);
00178 
00179     return ts;
00180 }
00181 
00182 /*
00183 ** Free the stack for the current thread
00184 */
00185 void _PR_FreeStack(PRThreadStack *ts)
00186 {
00187     if (!ts) {
00188        return;
00189     }
00190     if (ts->flags & _PR_STACK_PRIMORDIAL) {
00191        PR_DELETE(ts);
00192        return;
00193     }
00194 
00195     /*
00196     ** Put the stack on the free list. This is done because we are still
00197     ** using the stack. Next time a thread is created we will trim the
00198     ** list down; it's safe to do it then because we will have had to
00199     ** context switch to a live stack before another thread can be
00200     ** created.
00201     */
00202     PR_Lock(_pr_stackLock);
00203     PR_APPEND_LINK(&ts->links, _pr_freeStacks.prev);
00204     _pr_numFreeStacks++;
00205     PR_Unlock(_pr_stackLock);
00206 }