1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
44 #include <sys/types.h>
45 #include <sys/socket.h>
47 #include <sys/ioctl.h>
51 * Make sure _PRSockLen_t is 32-bit, because we will cast a PRUint32* or
52 * PRInt32* pointer to a _PRSockLen_t* pointer.
54 #define _PRSockLen_t int
57 ** Global lock variable used to bracket calls into rusty libraries that
58 ** aren't thread safe (like libc, libX, etc).
60 static PRLock *_pr_rename_lock = NULL;
61 static PRMonitor *_pr_Xfe_mon = NULL;
64 * Variables used by the GC code, initialized in _MD_InitSegs().
65 * _pr_zero_fd should be a static variable. Unfortunately, there is
66 * still some Unix-specific code left in function PR_GrowSegment()
67 * in file memory/prseg.c that references it, so it needs
68 * to be a global variable for now.
70 PRInt32 _pr_zero_fd = -1;
71 static PRLock *_pr_md_lock = NULL;
77 struct sigaction sigact;
80 sigemptyset(&timer_set);
82 sigact.sa_handler = SIG_IGN;
83 sigemptyset(&sigact.sa_mask);
85 rv = sigaction(SIGPIPE, &sigact, 0);
88 _pr_rename_lock = PR_NewLock();
89 PR_ASSERT(NULL != _pr_rename_lock);
90 _pr_Xfe_mon = PR_NewMonitor();
91 PR_ASSERT(NULL != _pr_Xfe_mon);
95 *-----------------------------------------------------------------------
99 * Returns the current time in microseconds since the epoch.
100 * The epoch is midnight January 1, 1970 GMT.
101 * The implementation is machine dependent. This is the Unix
103 * Cf. time_t time(time_t *tp)
105 *-----------------------------------------------------------------------
115 LL_I2L(s2us, PR_USEC_PER_SEC);
116 LL_I2L(s, tv.tv_sec);
117 LL_I2L(us, tv.tv_usec);
124 _PR_UNIX_GetInterval()
127 PRIntervalTime ticks;
129 (void)GETTIMEOFDAY(&time); /* fallicy of course */
130 ticks = (PRUint32)time.tv_sec * PR_MSEC_PER_SEC; /* that's in milliseconds */
131 ticks += (PRUint32)time.tv_usec / PR_USEC_PER_MSEC; /* so's that */
133 } /* _PR_SUNOS_GetInterval */
135 PRIntervalTime _PR_UNIX_TicksPerSecond()
137 return 1000; /* this needs some work :) */
140 /************************************************************************/
143 ** Special hacks for xlib. Xlib/Xt/Xm is not re-entrant nor is it thread
144 ** safe. Unfortunately, neither is mozilla. To make these programs work
145 ** in a pre-emptive threaded environment, we need to use a lock.
150 PR_EnterMonitor(_pr_Xfe_mon);
155 PR_ExitMonitor(_pr_Xfe_mon);
158 PRBool PR_XIsLocked()
160 return (PR_InMonitor(_pr_Xfe_mon)) ? PR_TRUE : PR_FALSE;
163 void PR_XWait(int ms)
165 PR_Wait(_pr_Xfe_mon, PR_MillisecondsToInterval(ms));
168 void PR_XNotify(void)
170 PR_Notify(_pr_Xfe_mon);
173 void PR_XNotifyAll(void)
175 PR_NotifyAll(_pr_Xfe_mon);
179 #ifdef HAVE_BSD_FLOCK
181 #include <sys/file.h>
183 PR_IMPLEMENT(PRStatus)
184 _MD_LOCKFILE (PRInt32 f)
187 rv = flock(f, LOCK_EX);
190 _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
194 PR_IMPLEMENT(PRStatus)
195 _MD_TLOCKFILE (PRInt32 f)
198 rv = flock(f, LOCK_EX|LOCK_NB);
201 _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
205 PR_IMPLEMENT(PRStatus)
206 _MD_UNLOCKFILE (PRInt32 f)
209 rv = flock(f, LOCK_UN);
212 _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
217 PR_IMPLEMENT(PRStatus)
218 _MD_LOCKFILE (PRInt32 f)
221 rv = lockf(f, F_LOCK, 0);
224 _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
228 PR_IMPLEMENT(PRStatus)
229 _MD_TLOCKFILE (PRInt32 f)
232 rv = lockf(f, F_TLOCK, 0);
235 _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
239 PR_IMPLEMENT(PRStatus)
240 _MD_UNLOCKFILE (PRInt32 f)
243 rv = lockf(f, F_ULOCK, 0);
246 _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
251 PR_IMPLEMENT(PRStatus)
252 _MD_GETHOSTNAME (char *name, PRUint32 namelen)
256 rv = gethostname(name, namelen);
260 _PR_MD_MAP_GETHOSTNAME_ERROR(_MD_ERRNO());