Debian Codegraph @

nsprpub/pr/src/md/unix/hpux.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "primpl.h"
7 #include <setjmp.h>
8
9 #if defined(HPUX_LW_TIMER)
10
11 # include <machine/inline.h>
12 # include <machine/clock.h>
13 # include <unistd.h>
14 # include <sys/time.h>
15 # include <sys/pstat.h>
16
17 int __lw_get_thread_times(int which, int64_t* sample, int64_t* time);
18
19 static double msecond_per_itick;
20
21 void _PR_HPUX_LW_IntervalInit(void) {
22 struct pst_processor psp;
23 int iticksperclktick, clk_tck;
24 int rv;
25
26 rv = pstat_getprocessor(&psp, sizeof(psp), 1, 0);
27 PR_ASSERT(rv != -1);
28
29 iticksperclktick = psp.psp_iticksperclktick;
30 clk_tck = sysconf(_SC_CLK_TCK);
31 msecond_per_itick = (1000.0) / (double)(iticksperclktick * clk_tck);
32 }
33
34 PRIntervalTime _PR_HPUX_LW_GetInterval(void) {
35 int64_t time, sample;
36
37 __lw_get_thread_times(1, &sample, &time);
38 /*
39 * Division is slower than float multiplication.
40 * return (time / iticks_per_msecond);
41 */
42 return (time * msecond_per_itick);
43 }
44 #endif /* HPUX_LW_TIMER */
45
46 #if !defined(PTHREADS_USER)
47
48 void _MD_EarlyInit(void) {
49 # ifndef _PR_PTHREADS
50 /*
51 * The following piece of code is taken from ns/nspr/src/md_HP-UX.c.
52 * In the comment for revision 1.6, dated 1995/09/11 23:33:34,
53 * robm says:
54 * This version has some problems which need to be addressed.
55 * First, intercept all system calls and prevent them from
56 * executing the library code which performs stack switches
57 * before normal system call invocation. In order for library
58 * calls which make system calls to work (like stdio), however,
59 * we must also allocate our own stack and switch the primordial
60 * stack to use it. This isn't so bad, except that I fudged the
61 * backtrace length when copying the old stack to the new one.
62 *
63 * This is the original comment of robm in the code:
64 * XXXrobm Horrific. To avoid a problem with HP's system call
65 * code, we allocate a new stack for the primordial thread and
66 * use it. However, we don't know how far back the original stack
67 * goes. We should create a routine that performs a backtrace and
68 * finds out just how much we need to copy. As a temporary measure,
69 * I just copy an arbitrary guess.
70 *
71 * In an email to servereng dated 2 Jan 1997, Mike Patnode (mikep)
72 * suggests that this only needs to be done for HP-UX 9.
73 */
74 # endif /* !_PR_PTHREADS */
75 }
76
77 PRWord* _MD_HomeGCRegisters(PRThread* t, int isCurrent, int* np) {
78 # ifndef _PR_PTHREADS
79 if (isCurrent) {
80 (void)setjmp(CONTEXT(t));
81 }
82 *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
83 return (PRWord*)CONTEXT(t);
84 # else
85 *np = 0;
86 return NULL;
87 # endif
88 }
89
90 # ifndef _PR_PTHREADS
91 void _MD_SET_PRIORITY(_MDThread* thread, PRUintn newPri) { return; }
92
93 PRStatus _MD_InitializeThread(PRThread* thread) { return PR_SUCCESS; }
94
95 PRStatus _MD_WAIT(PRThread* thread, PRIntervalTime ticks) {
96 PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
97 _PR_MD_SWITCH_CONTEXT(thread);
98 return PR_SUCCESS;
99 }
100
101 PRStatus _MD_WAKEUP_WAITER(PRThread* thread) {
102 if (thread) {
103 PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
104 }
105 return PR_SUCCESS;
106 }
107
108 /* These functions should not be called for HP-UX */
109 void _MD_YIELD(void) {
110 PR_NOT_REACHED("_MD_YIELD should not be called for HP-UX.");
111 }
112
113 PRStatus _MD_CREATE_THREAD(PRThread* thread, void (*start)(void*),
114 PRThreadPriority priority, PRThreadScope scope,
115 PRThreadState state, PRUint32 stackSize) {
116 PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for HP-UX.");
117 }
118 # endif /* _PR_PTHREADS */
119
120 void _MD_suspend_thread(PRThread* thread) {
121 # ifdef _PR_PTHREADS
122 # endif
123 }
124
125 void _MD_resume_thread(PRThread* thread) {
126 # ifdef _PR_PTHREADS
127 # endif
128 }
129 #endif /* PTHREADS_USER */
130
131 /*
132 * The HP version of strchr is buggy. It looks past the end of the
133 * string and causes a segmentation fault when our (NSPR) version
134 * of malloc is used.
135 *
136 * A better solution might be to put a cushion in our malloc just in
137 * case HP's version of strchr somehow gets used instead of this one.
138 */
139 char* strchr(const char* s, int c) {
140 char ch;
141
142 if (!s) {
143 return NULL;
144 }
145
146 ch = (char)c;
147
148 while ((*s) && ((*s) != ch)) {
149 s++;
150 }
151
152 if ((*s) == ch) {
153 return (char*)s;
154 }
155
156 return NULL;
157 }
158
159 /*
160 * Implemementation of memcmp in HP-UX (verified on releases A.09.03,
161 * A.09.07, and B.10.10) dumps core if called with:
162 * 1. First operand with address = 1(mod 4).
163 * 2. Size = 1(mod 4)
164 * 3. Last byte of the second operand is the last byte of the page and
165 * next page is not accessible(not mapped or protected)
166 * Thus, using the following naive version (tons of optimizations are
167 * possible;^)
168 */
169
170 int memcmp(const void* s1, const void* s2, size_t n) {
171 register unsigned char *p1 = (unsigned char*)s1, *p2 = (unsigned char*)s2;
172
173 while (n-- > 0) {
174 register int r = ((int)((unsigned int)*p1)) - ((int)((unsigned int)*p2));
175 if (r) {
176 return r;
177 }
178 p1++;
179 p2++;
180 }
181 return 0;
182 }