Back to index

lightning-sunbird  0.9+nobinonly
btmon.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <kernel/OS.h>
00039 
00040 #include "primpl.h"
00041 
00042 /*
00043 ** Create a new monitor. Monitors are re-entrant locks with a single built-in
00044 ** condition variable.
00045 **
00046 ** This may fail if memory is tight or if some operating system resource
00047 ** is low.
00048 */
00049 PR_IMPLEMENT(PRMonitor*)
00050     PR_NewMonitor (void)
00051 {
00052     PRMonitor *mon;
00053     PRCondVar *cvar;
00054     PRLock    *lock;
00055 
00056     mon = PR_NEWZAP( PRMonitor );
00057     if( mon )
00058     {
00059        lock = PR_NewLock();
00060        if( !lock )
00061         {
00062            PR_DELETE( mon );
00063            return( 0 );
00064        }
00065 
00066        cvar = PR_NewCondVar( lock );
00067        if( !cvar )
00068        {
00069            PR_DestroyLock( lock );
00070            PR_DELETE( mon );
00071            return( 0 );
00072        }
00073 
00074        mon->cvar = cvar;
00075        mon->name = NULL;
00076     }
00077 
00078     return( mon );
00079 }
00080 
00081 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name)
00082 {
00083     PRMonitor* mon = PR_NewMonitor();
00084     if( mon )
00085     {
00086         mon->name = name;
00087     }
00088     return mon;
00089 }
00090 
00091 /*
00092 ** Destroy a monitor. The caller is responsible for guaranteeing that the
00093 ** monitor is no longer in use. There must be no thread waiting on the
00094 ** monitor's condition variable and that the lock is not held.
00095 **
00096 */
00097 PR_IMPLEMENT(void)
00098     PR_DestroyMonitor (PRMonitor *mon)
00099 {
00100     PR_DestroyLock( mon->cvar->lock );
00101     PR_DestroyCondVar( mon->cvar );
00102     PR_DELETE( mon );
00103 }
00104 
00105 /*
00106 ** Enter the lock associated with the monitor. If the calling thread currently
00107 ** is in the monitor, the call to enter will silently succeed. In either case,
00108 ** it will increment the entry count by one.
00109 */
00110 PR_IMPLEMENT(void)
00111     PR_EnterMonitor (PRMonitor *mon)
00112 {
00113     if( mon->cvar->lock->owner == find_thread( NULL ) )
00114     {
00115        mon->entryCount++;
00116 
00117     } else
00118     {
00119        PR_Lock( mon->cvar->lock );
00120        mon->entryCount = 1;
00121     }
00122 }
00123 
00124 /*
00125 ** Decrement the entry count associated with the monitor. If the decremented
00126 ** entry count is zero, the monitor is exited. Returns PR_FAILURE if the
00127 ** calling thread has not entered the monitor.
00128 */
00129 PR_IMPLEMENT(PRStatus)
00130     PR_ExitMonitor (PRMonitor *mon)
00131 {
00132     if( mon->cvar->lock->owner != find_thread( NULL ) )
00133     {
00134        return( PR_FAILURE );
00135     }
00136     if( --mon->entryCount == 0 )
00137     {
00138        return( PR_Unlock( mon->cvar->lock ) );
00139     }
00140     return( PR_SUCCESS );
00141 }
00142 
00143 /*
00144 ** Wait for a notify on the monitor's condition variable. Sleep for "ticks"
00145 ** amount of time (if "ticks" is PR_INTERVAL_NO_TIMEOUT then the sleep is
00146 ** indefinite).
00147 **
00148 ** While the thread is waiting it exits the monitor (as if it called
00149 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor).  When
00150 ** the wait has finished the thread regains control of the monitors lock
00151 ** with the same entry count as before the wait began.
00152 **
00153 ** The thread waiting on the monitor will be resumed when the monitor is
00154 ** notified (assuming the thread is the next in line to receive the
00155 ** notify) or when the "ticks" timeout elapses.
00156 **
00157 ** Returns PR_FAILURE if the caller has not entered the monitor.
00158 */
00159 PR_IMPLEMENT(PRStatus)
00160     PR_Wait (PRMonitor *mon, PRIntervalTime ticks)
00161 {
00162     PRUint32 entryCount;
00163     PRUintn  status;
00164     PRThread *meThread;
00165     thread_id me = find_thread( NULL );
00166     meThread = PR_GetCurrentThread();
00167 
00168     if( mon->cvar->lock->owner != me ) return( PR_FAILURE );
00169 
00170     entryCount = mon->entryCount;
00171     mon->entryCount = 0;
00172 
00173     status = PR_WaitCondVar( mon->cvar, ticks );
00174 
00175     mon->entryCount = entryCount;
00176 
00177     return( status );
00178 }
00179 
00180 /*
00181 ** Notify a thread waiting on the monitor's condition variable. If a thread
00182 ** is waiting on the condition variable (using PR_Wait) then it is awakened
00183 ** and attempts to reenter the monitor.
00184 */
00185 PR_IMPLEMENT(PRStatus)
00186     PR_Notify (PRMonitor *mon)
00187 {
00188     if( mon->cvar->lock->owner != find_thread( NULL ) )
00189     {
00190        return( PR_FAILURE );
00191     }
00192 
00193     PR_NotifyCondVar( mon->cvar );
00194     return( PR_SUCCESS );
00195 }
00196 
00197 /*
00198 ** Notify all of the threads waiting on the monitor's condition variable.
00199 ** All of threads waiting on the condition are scheduled to reenter the
00200 ** monitor.
00201 */
00202 PR_IMPLEMENT(PRStatus)
00203     PR_NotifyAll (PRMonitor *mon)
00204 {
00205     if( mon->cvar->lock->owner != find_thread( NULL ) )
00206     {
00207        return( PR_FAILURE );
00208     }
00209 
00210     PR_NotifyAllCondVar( mon->cvar );
00211     return( PR_SUCCESS );
00212 }
00213 
00214 PR_IMPLEMENT(PRIntn)
00215     PR_GetMonitorEntryCount(PRMonitor *mon)
00216 {
00217     return( mon->entryCount );
00218 }
00219