Back to index

lightning-sunbird  0.9+nobinonly
prmon.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 /************************************************************************/
00041 
00042 /*
00043 ** Create a new monitor.
00044 */
00045 PR_IMPLEMENT(PRMonitor*) PR_NewMonitor()
00046 {
00047     PRMonitor *mon;
00048        PRCondVar *cvar;
00049        PRLock *lock;
00050 
00051     mon = PR_NEWZAP(PRMonitor);
00052     if (mon) {
00053               lock = PR_NewLock();
00054            if (!lock) {
00055                      PR_DELETE(mon);
00056                      return 0;
00057        }
00058 
00059            cvar = PR_NewCondVar(lock);
00060            if (!cvar) {
00061               PR_DestroyLock(lock);
00062                      PR_DELETE(mon);
00063                      return 0;
00064        }
00065        mon->cvar = cvar;
00066        mon->name = NULL;
00067     }
00068     return mon;
00069 }
00070 
00071 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name)
00072 {
00073     PRMonitor* mon = PR_NewMonitor();
00074     if (mon)
00075         mon->name = name;
00076     return mon;
00077 }
00078 
00079 /*
00080 ** Destroy a monitor. There must be no thread waiting on the monitor's
00081 ** condition variable. The caller is responsible for guaranteeing that the
00082 ** monitor is no longer in use.
00083 */
00084 PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon)
00085 {
00086        PR_DestroyLock(mon->cvar->lock);
00087     PR_DestroyCondVar(mon->cvar);
00088     PR_DELETE(mon);
00089 }
00090 
00091 /*
00092 ** Enter the lock associated with the monitor.
00093 */
00094 PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon)
00095 {
00096     if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) {
00097               mon->entryCount++;
00098     } else {
00099               PR_Lock(mon->cvar->lock);
00100               mon->entryCount = 1;
00101     }
00102 }
00103 
00104 /*
00105 ** Test and then enter the lock associated with the monitor if it's not
00106 ** already entered by some other thread. Return PR_FALSE if some other
00107 ** thread owned the lock at the time of the call.
00108 */
00109 PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon)
00110 {
00111     if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) {
00112               mon->entryCount++;
00113               return PR_TRUE;
00114     } else {
00115               if (PR_TestAndLock(mon->cvar->lock)) {
00116               mon->entryCount = 1;
00117                      return PR_TRUE;
00118               }
00119     }
00120     return PR_FALSE;
00121 }
00122 
00123 /*
00124 ** Exit the lock associated with the monitor once.
00125 */
00126 PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon)
00127 {
00128     if (mon->cvar->lock->owner != _PR_MD_CURRENT_THREAD()) {
00129         return PR_FAILURE;
00130     }
00131     if (--mon->entryCount == 0) {
00132               return PR_Unlock(mon->cvar->lock);
00133     }
00134     return PR_SUCCESS;
00135 }
00136 
00137 /*
00138 ** Return the number of times that the current thread has entered the
00139 ** lock. Returns zero if the current thread has not entered the lock.
00140 */
00141 PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon)
00142 {
00143     return (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) ?
00144         mon->entryCount : 0;
00145 }
00146 
00147 /*
00148 ** Wait for a notify on the condition variable. Sleep for "ticks" amount
00149 ** of time (if "tick" is 0 then the sleep is indefinite). While
00150 ** the thread is waiting it exits the monitors lock (as if it called
00151 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor).  When
00152 ** the wait has finished the thread regains control of the monitors lock
00153 ** with the same entry count as before the wait began.
00154 **
00155 ** The thread waiting on the monitor will be resumed when the monitor is
00156 ** notified (assuming the thread is the next in line to receive the
00157 ** notify) or when the "ticks" elapses.
00158 **
00159 ** Returns PR_FAILURE if the caller has not locked the lock associated
00160 ** with the condition variable.
00161 ** This routine can return PR_PENDING_INTERRUPT if the waiting thread 
00162 ** has been interrupted.
00163 */
00164 PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks)
00165 {
00166     PRUintn entryCount;
00167        PRStatus status;
00168     PRThread *me = _PR_MD_CURRENT_THREAD();
00169 
00170     if (mon->cvar->lock->owner != me) return PR_FAILURE;
00171 
00172     entryCount = mon->entryCount;
00173     mon->entryCount = 0;
00174 
00175        status = _PR_WaitCondVar(me, mon->cvar, mon->cvar->lock, ticks);
00176 
00177     mon->entryCount = entryCount;
00178 
00179     return status;
00180 }
00181 
00182 /*
00183 ** Notify the highest priority thread waiting on the condition
00184 ** variable. If a thread is waiting on the condition variable (using
00185 ** PR_Wait) then it is awakened and begins waiting on the monitor's lock.
00186 */
00187 PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon)
00188 {
00189     PRThread *me = _PR_MD_CURRENT_THREAD();
00190     if (mon->cvar->lock->owner != me) return PR_FAILURE;
00191     PR_NotifyCondVar(mon->cvar);
00192     return PR_SUCCESS;
00193 }
00194 
00195 /*
00196 ** Notify all of the threads waiting on the condition variable. All of
00197 ** threads are notified in turn. The highest priority thread will
00198 ** probably acquire the monitor first when the monitor is exited.
00199 */
00200 PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon)
00201 {
00202     PRThread *me = _PR_MD_CURRENT_THREAD();
00203     if (mon->cvar->lock->owner != me) return PR_FAILURE;
00204     PR_NotifyAllCondVar(mon->cvar);
00205     return PR_SUCCESS;
00206 }
00207 
00208 /************************************************************************/
00209 
00210 PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen)
00211 {
00212     PRUint32 nb;
00213 
00214     if (mon->cvar->lock->owner) {
00215        nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld",
00216                       mon, mon->cvar->lock->owner->id,
00217                       mon->cvar->lock->owner, mon->entryCount);
00218     } else {
00219        nb = PR_snprintf(buf, buflen, "[%p]", mon);
00220     }
00221     return nb;
00222 }