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 
8 #ifdef AIX_HAVE_ATOMIC_OP_H
9 #include <sys/atomic_op.h>
10 
_AIX_AtomicSet(PRInt32 * val,PRInt32 newval)11 PRInt32 _AIX_AtomicSet(PRInt32 *val, PRInt32 newval)
12 {
13     PRIntn oldval;
14     boolean_t stored;
15     oldval = fetch_and_add((atomic_p)val, 0);
16     do
17     {
18         stored = compare_and_swap((atomic_p)val, &oldval, newval);
19     } while (!stored);
20     return oldval;
21 }  /* _AIX_AtomicSet */
22 #endif /* AIX_HAVE_ATOMIC_OP_H */
23 
24 #if defined(AIX_TIMERS)
25 
26 #include <sys/time.h>
27 
28 static PRUint32 _aix_baseline_epoch;
29 
_MD_AixIntervalInit(void)30 static void _MD_AixIntervalInit(void)
31 {
32     timebasestruct_t real_time;
33     read_real_time(&real_time, TIMEBASE_SZ);
34     (void)time_base_to_time(&real_time, TIMEBASE_SZ);
35     _aix_baseline_epoch = real_time.tb_high;
36 }  /* _MD_AixIntervalInit */
37 
_MD_AixGetInterval(void)38 PRIntervalTime _MD_AixGetInterval(void)
39 {
40     PRIntn rv;
41     PRUint64 temp;
42     timebasestruct_t real_time;
43     read_real_time(&real_time, TIMEBASE_SZ);
44     (void)time_base_to_time(&real_time, TIMEBASE_SZ);
45     /* tb_high is in seconds, tb_low in 10(-9)seconds */
46     temp = 1000000000ULL * (PRUint64)(real_time.tb_high - _aix_baseline_epoch);
47     temp += (PRUint64)real_time.tb_low;  /* everything's 10(-9) seconds */
48     temp >>= 16;  /* now it's something way different */
49     return (PRIntervalTime)temp;
50 }  /* _MD_AixGetInterval */
51 
_MD_AixIntervalPerSec(void)52 PRIntervalTime _MD_AixIntervalPerSec(void)
53 {
54     return 1000000000ULL >> 16;  /* that's 15258, I think */
55 }  /* _MD_AixIntervalPerSec */
56 
57 #endif /* defined(AIX_TIMERS) */
58 
59 #if !defined(PTHREADS_USER)
60 
61 #if defined(_PR_PTHREADS)
62 
63 /*
64  * AIX 4.3 has sched_yield().  AIX 4.2 has pthread_yield().
65  * So we look up the appropriate function pointer at run time.
66  */
67 
68 #include <dlfcn.h>
69 
70 int (*_PT_aix_yield_fcn)() = NULL;
71 int _pr_aix_send_file_use_disabled = 0;
72 
_MD_EarlyInit(void)73 void _MD_EarlyInit(void)
74 {
75     void *main_app_handle;
76     char *evp;
77 
78     main_app_handle = dlopen(NULL, RTLD_NOW);
79     PR_ASSERT(NULL != main_app_handle);
80 
81     _PT_aix_yield_fcn = (int(*)())dlsym(main_app_handle, "sched_yield");
82     if (!_PT_aix_yield_fcn) {
83         _PT_aix_yield_fcn = (int(*)())dlsym(main_app_handle,"pthread_yield");
84         PR_ASSERT(NULL != _PT_aix_yield_fcn);
85     }
86     dlclose(main_app_handle);
87 
88     if (evp = getenv("NSPR_AIX_SEND_FILE_USE_DISABLED")) {
89         if (1 == atoi(evp)) {
90             _pr_aix_send_file_use_disabled = 1;
91         }
92     }
93 
94 #if defined(AIX_TIMERS)
95     _MD_AixIntervalInit();
96 #endif
97 }
98 
99 #else /* _PR_PTHREADS */
100 
_MD_EarlyInit(void)101 void _MD_EarlyInit(void)
102 {
103 #if defined(AIX_TIMERS)
104     _MD_AixIntervalInit();
105 #endif
106 }
107 
108 #endif /* _PR_PTHREADS */
109 
_MD_HomeGCRegisters(PRThread * t,int isCurrent,int * np)110 PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
111 {
112 #ifndef _PR_PTHREADS
113     if (isCurrent) {
114         (void) setjmp(CONTEXT(t));
115     }
116     *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
117     return (PRWord *) CONTEXT(t);
118 #else
119     *np = 0;
120     return NULL;
121 #endif
122 }
123 
124 #ifndef _PR_PTHREADS
125 PR_IMPLEMENT(void)
_MD_SET_PRIORITY(_MDThread * thread,PRUintn newPri)126 _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
127 {
128     return;
129 }
130 
131 PR_IMPLEMENT(PRStatus)
_MD_InitializeThread(PRThread * thread)132 _MD_InitializeThread(PRThread *thread)
133 {
134     return PR_SUCCESS;
135 }
136 
137 PR_IMPLEMENT(PRStatus)
_MD_WAIT(PRThread * thread,PRIntervalTime ticks)138 _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
139 {
140     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
141     _PR_MD_SWITCH_CONTEXT(thread);
142     return PR_SUCCESS;
143 }
144 
145 PR_IMPLEMENT(PRStatus)
_MD_WAKEUP_WAITER(PRThread * thread)146 _MD_WAKEUP_WAITER(PRThread *thread)
147 {
148     if (thread) {
149         PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
150     }
151     return PR_SUCCESS;
152 }
153 
154 /* These functions should not be called for AIX */
155 PR_IMPLEMENT(void)
_MD_YIELD(void)156 _MD_YIELD(void)
157 {
158     PR_NOT_REACHED("_MD_YIELD should not be called for AIX.");
159 }
160 
161 PR_IMPLEMENT(PRStatus)
_MD_CREATE_THREAD(PRThread * thread,void (* start)(void *),PRThreadPriority priority,PRThreadScope scope,PRThreadState state,PRUint32 stackSize)162 _MD_CREATE_THREAD(
163     PRThread *thread,
164     void (*start) (void *),
165     PRThreadPriority priority,
166     PRThreadScope scope,
167     PRThreadState state,
168     PRUint32 stackSize)
169 {
170     PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for AIX.");
171 }
172 #endif /* _PR_PTHREADS */
173 #endif /* PTHREADS_USER */
174 
175 /*
176  * NSPR 2.0 overrides the system select() and poll() functions.
177  * On AIX 4.2, we use dlopen("/unix", RTLD_NOW) and dlsym() to get
178  * at the original system select() and poll() functions.
179  */
180 
181 #if !defined(AIX_RENAME_SELECT)
182 
183 #include <sys/select.h>
184 #include <sys/poll.h>
185 #include <dlfcn.h>
186 
187 static int (*aix_select_fcn)() = NULL;
188 static int (*aix_poll_fcn)() = NULL;
189 
_MD_SELECT(int width,fd_set * r,fd_set * w,fd_set * e,struct timeval * t)190 int _MD_SELECT(int width, fd_set *r, fd_set *w, fd_set *e, struct timeval *t)
191 {
192     int rv;
193 
194     if (!aix_select_fcn) {
195         void *aix_handle;
196 
197         aix_handle = dlopen("/unix", RTLD_NOW);
198         if (!aix_handle) {
199             PR_SetError(PR_UNKNOWN_ERROR, 0);
200             return -1;
201         }
202         aix_select_fcn = (int(*)())dlsym(aix_handle,"select");
203         dlclose(aix_handle);
204         if (!aix_select_fcn) {
205             PR_SetError(PR_UNKNOWN_ERROR, 0);
206             return -1;
207         }
208     }
209     rv = (*aix_select_fcn)(width, r, w, e, t);
210     return rv;
211 }
212 
_MD_POLL(void * listptr,unsigned long nfds,long timeout)213 int _MD_POLL(void *listptr, unsigned long nfds, long timeout)
214 {
215     int rv;
216 
217     if (!aix_poll_fcn) {
218         void *aix_handle;
219 
220         aix_handle = dlopen("/unix", RTLD_NOW);
221         if (!aix_handle) {
222             PR_SetError(PR_UNKNOWN_ERROR, 0);
223             return -1;
224         }
225         aix_poll_fcn = (int(*)())dlsym(aix_handle,"poll");
226         dlclose(aix_handle);
227         if (!aix_poll_fcn) {
228             PR_SetError(PR_UNKNOWN_ERROR, 0);
229             return -1;
230         }
231     }
232     rv = (*aix_poll_fcn)(listptr, nfds, timeout);
233     return rv;
234 }
235 
236 #else
237 
238 /*
239  * In AIX versions prior to 4.2, we use the two-step rename/link trick.
240  * The binary must contain at least one "poll" symbol for linker's rename
241  * to work.  So we must have this dummy function that references poll().
242  */
243 #include <sys/poll.h>
_pr_aix_dummy()244 void _pr_aix_dummy()
245 {
246     poll(0,0,0);
247 }
248 
249 #endif /* !defined(AIX_RENAME_SELECT) */
250 
251 #ifdef _PR_HAVE_ATOMIC_CAS
252 
253 #include "pratom.h"
254 
255 #define _PR_AIX_ATOMIC_LOCK -1
256 
257 PR_IMPLEMENT(void)
PR_StackPush(PRStack * stack,PRStackElem * stack_elem)258 PR_StackPush(PRStack *stack, PRStackElem *stack_elem)
259 {
260     PRStackElem *addr;
261     boolean_t locked = TRUE;
262 
263     /* Is it safe to cast a pointer to an int? */
264     PR_ASSERT(sizeof(int) == sizeof(PRStackElem *));
265     do {
266         while ((addr = stack->prstk_head.prstk_elem_next) ==
267                (PRStackElem *)_PR_AIX_ATOMIC_LOCK)
268             ;
269         locked = _check_lock((atomic_p) &stack->prstk_head.prstk_elem_next,
270                              (int) addr, _PR_AIX_ATOMIC_LOCK);
271     } while (locked == TRUE);
272     stack_elem->prstk_elem_next = addr;
273     _clear_lock((atomic_p)&stack->prstk_head.prstk_elem_next, (int)stack_elem);
274     return;
275 }
276 
277 PR_IMPLEMENT(PRStackElem *)
PR_StackPop(PRStack * stack)278 PR_StackPop(PRStack *stack)
279 {
280     PRStackElem *element;
281     boolean_t locked = TRUE;
282 
283     /* Is it safe to cast a pointer to an int? */
284     PR_ASSERT(sizeof(int) == sizeof(PRStackElem *));
285     do {
286         while ((element = stack->prstk_head.prstk_elem_next) ==
287                (PRStackElem *) _PR_AIX_ATOMIC_LOCK)
288             ;
289         locked = _check_lock((atomic_p) &stack->prstk_head.prstk_elem_next,
290                              (int)element, _PR_AIX_ATOMIC_LOCK);
291     } while (locked == TRUE);
292 
293     if (element == NULL) {
294         _clear_lock((atomic_p) &stack->prstk_head.prstk_elem_next, NULL);
295     } else {
296         _clear_lock((atomic_p) &stack->prstk_head.prstk_elem_next,
297                     (int) element->prstk_elem_next);
298     }
299     return element;
300 }
301 
302 #endif  /* _PR_HAVE_ATOMIC_CAS */
303