Back to index

lightning-sunbird  0.9+nobinonly
beos.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 #include <signal.h>
00041 #include <unistd.h>
00042 #include <memory.h>
00043 #include <fcntl.h>
00044 #include <sys/types.h>
00045 #include <sys/socket.h>
00046 #include <sys/time.h>
00047 #include <sys/ioctl.h>
00048 #include <errno.h>
00049 
00050 /*
00051  * Make sure _PRSockLen_t is 32-bit, because we will cast a PRUint32* or
00052  * PRInt32* pointer to a _PRSockLen_t* pointer.
00053  */
00054 #define _PRSockLen_t int
00055 
00056 /*
00057 ** Global lock variable used to bracket calls into rusty libraries that
00058 ** aren't thread safe (like libc, libX, etc).
00059 */
00060 static PRLock *_pr_rename_lock = NULL;
00061 static PRMonitor *_pr_Xfe_mon = NULL;
00062 
00063 /*
00064  * Variables used by the GC code, initialized in _MD_InitSegs().
00065  * _pr_zero_fd should be a static variable.  Unfortunately, there is
00066  * still some Unix-specific code left in function PR_GrowSegment()
00067  * in file memory/prseg.c that references it, so it needs
00068  * to be a global variable for now.
00069  */
00070 PRInt32 _pr_zero_fd = -1;
00071 static PRLock *_pr_md_lock = NULL;
00072 
00073 sigset_t timer_set;
00074 
00075 void _PR_UnixInit()
00076 {
00077        struct sigaction sigact;
00078        int rv;
00079 
00080        sigemptyset(&timer_set);
00081 
00082        sigact.sa_handler = SIG_IGN;
00083        sigemptyset(&sigact.sa_mask);
00084        sigact.sa_flags = 0;
00085        rv = sigaction(SIGPIPE, &sigact, 0);
00086        PR_ASSERT(0 == rv);
00087 
00088        _pr_rename_lock = PR_NewLock();
00089        PR_ASSERT(NULL != _pr_rename_lock);
00090        _pr_Xfe_mon = PR_NewMonitor();
00091        PR_ASSERT(NULL != _pr_Xfe_mon);
00092 }
00093 
00094 /*
00095  *-----------------------------------------------------------------------
00096  *
00097  * PR_Now --
00098  *
00099  *     Returns the current time in microseconds since the epoch.
00100  *     The epoch is midnight January 1, 1970 GMT.
00101  *     The implementation is machine dependent.  This is the Unix
00102  *     implementation.
00103  *     Cf. time_t time(time_t *tp)
00104  *
00105  *-----------------------------------------------------------------------
00106  */
00107 
00108 PR_IMPLEMENT(PRTime)
00109 PR_Now(void)
00110 {
00111        struct timeval tv;
00112        PRInt64 s, us, s2us;
00113 
00114        GETTIMEOFDAY(&tv);
00115        LL_I2L(s2us, PR_USEC_PER_SEC);
00116        LL_I2L(s, tv.tv_sec);
00117        LL_I2L(us, tv.tv_usec);
00118        LL_MUL(s, s, s2us);
00119        LL_ADD(s, s, us);
00120        return s;
00121 }
00122 
00123 PRIntervalTime
00124 _PR_UNIX_GetInterval()
00125 {
00126        struct timeval time;
00127        PRIntervalTime ticks;
00128 
00129        (void)GETTIMEOFDAY(&time);  /* fallicy of course */
00130        ticks = (PRUint32)time.tv_sec * PR_MSEC_PER_SEC;  /* that's in milliseconds */
00131        ticks += (PRUint32)time.tv_usec / PR_USEC_PER_MSEC;  /* so's that */
00132        return ticks;
00133 }  /* _PR_SUNOS_GetInterval */
00134 
00135 PRIntervalTime _PR_UNIX_TicksPerSecond()
00136 {
00137        return 1000;  /* this needs some work :) */
00138 }
00139 
00140 /************************************************************************/
00141 
00142 /*
00143 ** Special hacks for xlib. Xlib/Xt/Xm is not re-entrant nor is it thread
00144 ** safe.  Unfortunately, neither is mozilla. To make these programs work
00145 ** in a pre-emptive threaded environment, we need to use a lock.
00146 */
00147 
00148 void PR_XLock()
00149 {
00150        PR_EnterMonitor(_pr_Xfe_mon);
00151 }
00152 
00153 void PR_XUnlock()
00154 {
00155        PR_ExitMonitor(_pr_Xfe_mon);
00156 }
00157 
00158 PRBool PR_XIsLocked()
00159 {
00160        return (PR_InMonitor(_pr_Xfe_mon)) ? PR_TRUE : PR_FALSE;
00161 }
00162 
00163 void PR_XWait(int ms)
00164 {
00165        PR_Wait(_pr_Xfe_mon, PR_MillisecondsToInterval(ms));
00166 }
00167 
00168 void PR_XNotify(void)
00169 {
00170        PR_Notify(_pr_Xfe_mon);
00171 }
00172 
00173 void PR_XNotifyAll(void)
00174 {
00175        PR_NotifyAll(_pr_Xfe_mon);
00176 }
00177 
00178 #if !defined(BEOS)
00179 #ifdef HAVE_BSD_FLOCK
00180 
00181 #include <sys/file.h>
00182 
00183 PR_IMPLEMENT(PRStatus)
00184 _MD_LOCKFILE (PRInt32 f)
00185 {
00186        PRInt32 rv;
00187        rv = flock(f, LOCK_EX);
00188        if (rv == 0)
00189               return PR_SUCCESS;
00190        _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
00191        return PR_FAILURE;
00192 }
00193 
00194 PR_IMPLEMENT(PRStatus)
00195 _MD_TLOCKFILE (PRInt32 f)
00196 {
00197        PRInt32 rv;
00198        rv = flock(f, LOCK_EX|LOCK_NB);
00199        if (rv == 0)
00200               return PR_SUCCESS;
00201        _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
00202        return PR_FAILURE;
00203 }
00204 
00205 PR_IMPLEMENT(PRStatus)
00206 _MD_UNLOCKFILE (PRInt32 f)
00207 {
00208        PRInt32 rv;
00209        rv = flock(f, LOCK_UN);
00210        if (rv == 0)
00211               return PR_SUCCESS;
00212        _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
00213        return PR_FAILURE;
00214 }
00215 #else
00216 
00217 PR_IMPLEMENT(PRStatus)
00218 _MD_LOCKFILE (PRInt32 f)
00219 {
00220        PRInt32 rv;
00221        rv = lockf(f, F_LOCK, 0);
00222        if (rv == 0)
00223               return PR_SUCCESS;
00224        _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
00225        return PR_FAILURE;
00226 }
00227 
00228 PR_IMPLEMENT(PRStatus)
00229 _MD_TLOCKFILE (PRInt32 f)
00230 {
00231        PRInt32 rv;
00232        rv = lockf(f, F_TLOCK, 0);
00233        if (rv == 0)
00234               return PR_SUCCESS;
00235        _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
00236        return PR_FAILURE;
00237 }
00238 
00239 PR_IMPLEMENT(PRStatus)
00240 _MD_UNLOCKFILE (PRInt32 f)
00241 {
00242        PRInt32 rv;
00243        rv = lockf(f, F_ULOCK, 0);
00244        if (rv == 0)
00245               return PR_SUCCESS;
00246        _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
00247        return PR_FAILURE;
00248 }
00249 #endif
00250 
00251 PR_IMPLEMENT(PRStatus)
00252   _MD_GETHOSTNAME (char *name, PRUint32 namelen)
00253 {
00254     PRIntn rv;
00255 
00256     rv = gethostname(name, namelen);
00257     if (0 == rv) {
00258               return PR_SUCCESS;
00259     }
00260        _PR_MD_MAP_GETHOSTNAME_ERROR(_MD_ERRNO());
00261     return PR_FAILURE;
00262 }
00263 
00264 #endif