Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / src / md / beos / beos.c
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is the Netscape Portable Runtime (NSPR).
16  *
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.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include "primpl.h"
39
40 #include <signal.h>
41 #include <unistd.h>
42 #include <memory.h>
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/time.h>
47 #include <sys/ioctl.h>
48 #include <errno.h>
49
50 /*
51  * Make sure _PRSockLen_t is 32-bit, because we will cast a PRUint32* or
52  * PRInt32* pointer to a _PRSockLen_t* pointer.
53  */
54 #define _PRSockLen_t int
55
56 /*
57 ** Global lock variable used to bracket calls into rusty libraries that
58 ** aren't thread safe (like libc, libX, etc).
59 */
60 static PRLock *_pr_rename_lock = NULL;
61 static PRMonitor *_pr_Xfe_mon = NULL;
62
63 /*
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.
69  */
70 PRInt32 _pr_zero_fd = -1;
71 static PRLock *_pr_md_lock = NULL;
72
73 sigset_t timer_set;
74
75 void _PR_UnixInit()
76 {
77         struct sigaction sigact;
78         int rv;
79
80         sigemptyset(&timer_set);
81
82         sigact.sa_handler = SIG_IGN;
83         sigemptyset(&sigact.sa_mask);
84         sigact.sa_flags = 0;
85         rv = sigaction(SIGPIPE, &sigact, 0);
86         PR_ASSERT(0 == rv);
87
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);
92 }
93
94 /*
95  *-----------------------------------------------------------------------
96  *
97  * PR_Now --
98  *
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
102  *     implementation.
103  *     Cf. time_t time(time_t *tp)
104  *
105  *-----------------------------------------------------------------------
106  */
107
108 PR_IMPLEMENT(PRTime)
109 PR_Now(void)
110 {
111         struct timeval tv;
112         PRInt64 s, us, s2us;
113
114         GETTIMEOFDAY(&tv);
115         LL_I2L(s2us, PR_USEC_PER_SEC);
116         LL_I2L(s, tv.tv_sec);
117         LL_I2L(us, tv.tv_usec);
118         LL_MUL(s, s, s2us);
119         LL_ADD(s, s, us);
120         return s;
121 }
122
123 PRIntervalTime
124 _PR_UNIX_GetInterval()
125 {
126         struct timeval time;
127         PRIntervalTime ticks;
128
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 */
132         return ticks;
133 }  /* _PR_SUNOS_GetInterval */
134
135 PRIntervalTime _PR_UNIX_TicksPerSecond()
136 {
137         return 1000;  /* this needs some work :) */
138 }
139
140 /************************************************************************/
141
142 /*
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.
146 */
147
148 void PR_XLock()
149 {
150         PR_EnterMonitor(_pr_Xfe_mon);
151 }
152
153 void PR_XUnlock()
154 {
155         PR_ExitMonitor(_pr_Xfe_mon);
156 }
157
158 PRBool PR_XIsLocked()
159 {
160         return (PR_InMonitor(_pr_Xfe_mon)) ? PR_TRUE : PR_FALSE;
161 }
162
163 void PR_XWait(int ms)
164 {
165         PR_Wait(_pr_Xfe_mon, PR_MillisecondsToInterval(ms));
166 }
167
168 void PR_XNotify(void)
169 {
170         PR_Notify(_pr_Xfe_mon);
171 }
172
173 void PR_XNotifyAll(void)
174 {
175         PR_NotifyAll(_pr_Xfe_mon);
176 }
177
178 #if !defined(BEOS)
179 #ifdef HAVE_BSD_FLOCK
180
181 #include <sys/file.h>
182
183 PR_IMPLEMENT(PRStatus)
184 _MD_LOCKFILE (PRInt32 f)
185 {
186         PRInt32 rv;
187         rv = flock(f, LOCK_EX);
188         if (rv == 0)
189                 return PR_SUCCESS;
190         _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
191         return PR_FAILURE;
192 }
193
194 PR_IMPLEMENT(PRStatus)
195 _MD_TLOCKFILE (PRInt32 f)
196 {
197         PRInt32 rv;
198         rv = flock(f, LOCK_EX|LOCK_NB);
199         if (rv == 0)
200                 return PR_SUCCESS;
201         _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
202         return PR_FAILURE;
203 }
204
205 PR_IMPLEMENT(PRStatus)
206 _MD_UNLOCKFILE (PRInt32 f)
207 {
208         PRInt32 rv;
209         rv = flock(f, LOCK_UN);
210         if (rv == 0)
211                 return PR_SUCCESS;
212         _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
213         return PR_FAILURE;
214 }
215 #else
216
217 PR_IMPLEMENT(PRStatus)
218 _MD_LOCKFILE (PRInt32 f)
219 {
220         PRInt32 rv;
221         rv = lockf(f, F_LOCK, 0);
222         if (rv == 0)
223                 return PR_SUCCESS;
224         _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
225         return PR_FAILURE;
226 }
227
228 PR_IMPLEMENT(PRStatus)
229 _MD_TLOCKFILE (PRInt32 f)
230 {
231         PRInt32 rv;
232         rv = lockf(f, F_TLOCK, 0);
233         if (rv == 0)
234                 return PR_SUCCESS;
235         _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
236         return PR_FAILURE;
237 }
238
239 PR_IMPLEMENT(PRStatus)
240 _MD_UNLOCKFILE (PRInt32 f)
241 {
242         PRInt32 rv;
243         rv = lockf(f, F_ULOCK, 0);
244         if (rv == 0)
245                 return PR_SUCCESS;
246         _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
247         return PR_FAILURE;
248 }
249 #endif
250
251 PR_IMPLEMENT(PRStatus)
252   _MD_GETHOSTNAME (char *name, PRUint32 namelen)
253 {
254     PRIntn rv;
255
256     rv = gethostname(name, namelen);
257     if (0 == rv) {
258                 return PR_SUCCESS;
259     }
260         _PR_MD_MAP_GETHOSTNAME_ERROR(_MD_ERRNO());
261     return PR_FAILURE;
262 }
263
264 #endif