~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/src/md/beos/beos.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* 
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are 
 
17
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the
 
23
 * terms of the GNU General Public License Version 2 or later (the
 
24
 * "GPL"), in which case the provisions of the GPL are applicable 
 
25
 * instead of those above.  If you wish to allow use of your 
 
26
 * version of this file only under the terms of the GPL and not to
 
27
 * allow others to use your version of this file under the MPL,
 
28
 * indicate your decision by deleting the provisions above and
 
29
 * replace them with the notice and other provisions required by
 
30
 * the GPL.  If you do not delete the provisions above, a recipient
 
31
 * may use your version of this file under either the MPL or the
 
32
 * GPL.
 
33
 */
 
34
 
 
35
#include "primpl.h"
 
36
 
 
37
#include <signal.h>
 
38
#include <unistd.h>
 
39
#include <memory.h>
 
40
#include <fcntl.h>
 
41
#include <sys/types.h>
 
42
#include <sys/socket.h>
 
43
#include <sys/time.h>
 
44
#include <sys/ioctl.h>
 
45
#include <errno.h>
 
46
 
 
47
/*
 
48
 * Make sure _PRSockLen_t is 32-bit, because we will cast a PRUint32* or
 
49
 * PRInt32* pointer to a _PRSockLen_t* pointer.
 
50
 */
 
51
#define _PRSockLen_t int
 
52
 
 
53
/*
 
54
** Global lock variable used to bracket calls into rusty libraries that
 
55
** aren't thread safe (like libc, libX, etc).
 
56
*/
 
57
static PRLock *_pr_rename_lock = NULL;
 
58
static PRMonitor *_pr_Xfe_mon = NULL;
 
59
 
 
60
/*
 
61
 * Variables used by the GC code, initialized in _MD_InitSegs().
 
62
 * _pr_zero_fd should be a static variable.  Unfortunately, there is
 
63
 * still some Unix-specific code left in function PR_GrowSegment()
 
64
 * in file memory/prseg.c that references it, so it needs
 
65
 * to be a global variable for now.
 
66
 */
 
67
PRInt32 _pr_zero_fd = -1;
 
68
static PRLock *_pr_md_lock = NULL;
 
69
 
 
70
sigset_t timer_set;
 
71
 
 
72
void _PR_UnixInit()
 
73
{
 
74
        struct sigaction sigact;
 
75
        int rv;
 
76
 
 
77
        sigemptyset(&timer_set);
 
78
 
 
79
        sigact.sa_handler = SIG_IGN;
 
80
        sigemptyset(&sigact.sa_mask);
 
81
        sigact.sa_flags = 0;
 
82
        rv = sigaction(SIGPIPE, &sigact, 0);
 
83
        PR_ASSERT(0 == rv);
 
84
 
 
85
        _pr_rename_lock = PR_NewLock();
 
86
        PR_ASSERT(NULL != _pr_rename_lock);
 
87
        _pr_Xfe_mon = PR_NewMonitor();
 
88
        PR_ASSERT(NULL != _pr_Xfe_mon);
 
89
}
 
90
 
 
91
/*
 
92
 *-----------------------------------------------------------------------
 
93
 *
 
94
 * PR_Now --
 
95
 *
 
96
 *     Returns the current time in microseconds since the epoch.
 
97
 *     The epoch is midnight January 1, 1970 GMT.
 
98
 *     The implementation is machine dependent.  This is the Unix
 
99
 *     implementation.
 
100
 *     Cf. time_t time(time_t *tp)
 
101
 *
 
102
 *-----------------------------------------------------------------------
 
103
 */
 
104
 
 
105
PR_IMPLEMENT(PRTime)
 
106
PR_Now(void)
 
107
{
 
108
        struct timeval tv;
 
109
        PRInt64 s, us, s2us;
 
110
 
 
111
        GETTIMEOFDAY(&tv);
 
112
        LL_I2L(s2us, PR_USEC_PER_SEC);
 
113
        LL_I2L(s, tv.tv_sec);
 
114
        LL_I2L(us, tv.tv_usec);
 
115
        LL_MUL(s, s, s2us);
 
116
        LL_ADD(s, s, us);
 
117
        return s;
 
118
}
 
119
 
 
120
PRIntervalTime
 
121
_PR_UNIX_GetInterval()
 
122
{
 
123
        struct timeval time;
 
124
        PRIntervalTime ticks;
 
125
 
 
126
        (void)GETTIMEOFDAY(&time);  /* fallicy of course */
 
127
        ticks = (PRUint32)time.tv_sec * PR_MSEC_PER_SEC;  /* that's in milliseconds */
 
128
        ticks += (PRUint32)time.tv_usec / PR_USEC_PER_MSEC;  /* so's that */
 
129
        return ticks;
 
130
}  /* _PR_SUNOS_GetInterval */
 
131
 
 
132
PRIntervalTime _PR_UNIX_TicksPerSecond()
 
133
{
 
134
        return 1000;  /* this needs some work :) */
 
135
}
 
136
 
 
137
/************************************************************************/
 
138
 
 
139
/*
 
140
** Special hacks for xlib. Xlib/Xt/Xm is not re-entrant nor is it thread
 
141
** safe.  Unfortunately, neither is mozilla. To make these programs work
 
142
** in a pre-emptive threaded environment, we need to use a lock.
 
143
*/
 
144
 
 
145
void PR_XLock()
 
146
{
 
147
        PR_EnterMonitor(_pr_Xfe_mon);
 
148
}
 
149
 
 
150
void PR_XUnlock()
 
151
{
 
152
        PR_ExitMonitor(_pr_Xfe_mon);
 
153
}
 
154
 
 
155
PRBool PR_XIsLocked()
 
156
{
 
157
        return (PR_InMonitor(_pr_Xfe_mon)) ? PR_TRUE : PR_FALSE;
 
158
}
 
159
 
 
160
void PR_XWait(int ms)
 
161
{
 
162
        PR_Wait(_pr_Xfe_mon, PR_MillisecondsToInterval(ms));
 
163
}
 
164
 
 
165
void PR_XNotify(void)
 
166
{
 
167
        PR_Notify(_pr_Xfe_mon);
 
168
}
 
169
 
 
170
void PR_XNotifyAll(void)
 
171
{
 
172
        PR_NotifyAll(_pr_Xfe_mon);
 
173
}
 
174
 
 
175
#if !defined(BEOS)
 
176
#ifdef HAVE_BSD_FLOCK
 
177
 
 
178
#include <sys/file.h>
 
179
 
 
180
PR_IMPLEMENT(PRStatus)
 
181
_MD_LOCKFILE (PRInt32 f)
 
182
{
 
183
        PRInt32 rv;
 
184
        rv = flock(f, LOCK_EX);
 
185
        if (rv == 0)
 
186
                return PR_SUCCESS;
 
187
        _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
 
188
        return PR_FAILURE;
 
189
}
 
190
 
 
191
PR_IMPLEMENT(PRStatus)
 
192
_MD_TLOCKFILE (PRInt32 f)
 
193
{
 
194
        PRInt32 rv;
 
195
        rv = flock(f, LOCK_EX|LOCK_NB);
 
196
        if (rv == 0)
 
197
                return PR_SUCCESS;
 
198
        _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
 
199
        return PR_FAILURE;
 
200
}
 
201
 
 
202
PR_IMPLEMENT(PRStatus)
 
203
_MD_UNLOCKFILE (PRInt32 f)
 
204
{
 
205
        PRInt32 rv;
 
206
        rv = flock(f, LOCK_UN);
 
207
        if (rv == 0)
 
208
                return PR_SUCCESS;
 
209
        _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
 
210
        return PR_FAILURE;
 
211
}
 
212
#else
 
213
 
 
214
PR_IMPLEMENT(PRStatus)
 
215
_MD_LOCKFILE (PRInt32 f)
 
216
{
 
217
        PRInt32 rv;
 
218
        rv = lockf(f, F_LOCK, 0);
 
219
        if (rv == 0)
 
220
                return PR_SUCCESS;
 
221
        _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
 
222
        return PR_FAILURE;
 
223
}
 
224
 
 
225
PR_IMPLEMENT(PRStatus)
 
226
_MD_TLOCKFILE (PRInt32 f)
 
227
{
 
228
        PRInt32 rv;
 
229
        rv = lockf(f, F_TLOCK, 0);
 
230
        if (rv == 0)
 
231
                return PR_SUCCESS;
 
232
        _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
 
233
        return PR_FAILURE;
 
234
}
 
235
 
 
236
PR_IMPLEMENT(PRStatus)
 
237
_MD_UNLOCKFILE (PRInt32 f)
 
238
{
 
239
        PRInt32 rv;
 
240
        rv = lockf(f, F_ULOCK, 0);
 
241
        if (rv == 0)
 
242
                return PR_SUCCESS;
 
243
        _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
 
244
        return PR_FAILURE;
 
245
}
 
246
#endif
 
247
 
 
248
PR_IMPLEMENT(PRStatus)
 
249
  _MD_GETHOSTNAME (char *name, PRUint32 namelen)
 
250
{
 
251
    PRIntn rv;
 
252
 
 
253
    rv = gethostname(name, namelen);
 
254
    if (0 == rv) {
 
255
                return PR_SUCCESS;
 
256
    }
 
257
        _PR_MD_MAP_GETHOSTNAME_ERROR(_MD_ERRNO());
 
258
    return PR_FAILURE;
 
259
}
 
260
 
 
261
#endif