michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "primpl.h" michael@0: michael@0: #ifdef AIX_HAVE_ATOMIC_OP_H michael@0: #include michael@0: michael@0: PRInt32 _AIX_AtomicSet(PRInt32 *val, PRInt32 newval) michael@0: { michael@0: PRIntn oldval; michael@0: boolean_t stored; michael@0: oldval = fetch_and_add((atomic_p)val, 0); michael@0: do michael@0: { michael@0: stored = compare_and_swap((atomic_p)val, &oldval, newval); michael@0: } while (!stored); michael@0: return oldval; michael@0: } /* _AIX_AtomicSet */ michael@0: #endif /* AIX_HAVE_ATOMIC_OP_H */ michael@0: michael@0: #if defined(AIX_TIMERS) michael@0: michael@0: #include michael@0: michael@0: static PRUint32 _aix_baseline_epoch; michael@0: michael@0: static void _MD_AixIntervalInit(void) michael@0: { michael@0: timebasestruct_t real_time; michael@0: read_real_time(&real_time, TIMEBASE_SZ); michael@0: (void)time_base_to_time(&real_time, TIMEBASE_SZ); michael@0: _aix_baseline_epoch = real_time.tb_high; michael@0: } /* _MD_AixIntervalInit */ michael@0: michael@0: PRIntervalTime _MD_AixGetInterval(void) michael@0: { michael@0: PRIntn rv; michael@0: PRUint64 temp; michael@0: timebasestruct_t real_time; michael@0: read_real_time(&real_time, TIMEBASE_SZ); michael@0: (void)time_base_to_time(&real_time, TIMEBASE_SZ); michael@0: /* tb_high is in seconds, tb_low in 10(-9)seconds */ michael@0: temp = 1000000000ULL * (PRUint64)(real_time.tb_high - _aix_baseline_epoch); michael@0: temp += (PRUint64)real_time.tb_low; /* everything's 10(-9) seconds */ michael@0: temp >>= 16; /* now it's something way different */ michael@0: return (PRIntervalTime)temp; michael@0: } /* _MD_AixGetInterval */ michael@0: michael@0: PRIntervalTime _MD_AixIntervalPerSec(void) michael@0: { michael@0: return 1000000000ULL >> 16; /* that's 15258, I think */ michael@0: } /* _MD_AixIntervalPerSec */ michael@0: michael@0: #endif /* defined(AIX_TIMERS) */ michael@0: michael@0: #if !defined(PTHREADS_USER) michael@0: michael@0: #if defined(_PR_PTHREADS) michael@0: michael@0: /* michael@0: * AIX 4.3 has sched_yield(). AIX 4.2 has pthread_yield(). michael@0: * So we look up the appropriate function pointer at run time. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: int (*_PT_aix_yield_fcn)() = NULL; michael@0: int _pr_aix_send_file_use_disabled = 0; michael@0: michael@0: void _MD_EarlyInit(void) michael@0: { michael@0: void *main_app_handle; michael@0: char *evp; michael@0: michael@0: main_app_handle = dlopen(NULL, RTLD_NOW); michael@0: PR_ASSERT(NULL != main_app_handle); michael@0: michael@0: _PT_aix_yield_fcn = (int(*)())dlsym(main_app_handle, "sched_yield"); michael@0: if (!_PT_aix_yield_fcn) { michael@0: _PT_aix_yield_fcn = (int(*)())dlsym(main_app_handle,"pthread_yield"); michael@0: PR_ASSERT(NULL != _PT_aix_yield_fcn); michael@0: } michael@0: dlclose(main_app_handle); michael@0: michael@0: if (evp = getenv("NSPR_AIX_SEND_FILE_USE_DISABLED")) { michael@0: if (1 == atoi(evp)) michael@0: _pr_aix_send_file_use_disabled = 1; michael@0: } michael@0: michael@0: #if defined(AIX_TIMERS) michael@0: _MD_AixIntervalInit(); michael@0: #endif michael@0: } michael@0: michael@0: #else /* _PR_PTHREADS */ michael@0: michael@0: void _MD_EarlyInit(void) michael@0: { michael@0: #if defined(AIX_TIMERS) michael@0: _MD_AixIntervalInit(); michael@0: #endif michael@0: } michael@0: michael@0: #endif /* _PR_PTHREADS */ michael@0: michael@0: PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np) michael@0: { michael@0: #ifndef _PR_PTHREADS michael@0: if (isCurrent) { michael@0: (void) setjmp(CONTEXT(t)); michael@0: } michael@0: *np = sizeof(CONTEXT(t)) / sizeof(PRWord); michael@0: return (PRWord *) CONTEXT(t); michael@0: #else michael@0: *np = 0; michael@0: return NULL; michael@0: #endif michael@0: } michael@0: michael@0: #ifndef _PR_PTHREADS michael@0: PR_IMPLEMENT(void) michael@0: _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: _MD_InitializeThread(PRThread *thread) michael@0: { michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: _MD_WAIT(PRThread *thread, PRIntervalTime ticks) michael@0: { michael@0: PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE)); michael@0: _PR_MD_SWITCH_CONTEXT(thread); michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: _MD_WAKEUP_WAITER(PRThread *thread) michael@0: { michael@0: if (thread) { michael@0: PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE)); michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* These functions should not be called for AIX */ michael@0: PR_IMPLEMENT(void) michael@0: _MD_YIELD(void) michael@0: { michael@0: PR_NOT_REACHED("_MD_YIELD should not be called for AIX."); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: _MD_CREATE_THREAD( michael@0: PRThread *thread, michael@0: void (*start) (void *), michael@0: PRThreadPriority priority, michael@0: PRThreadScope scope, michael@0: PRThreadState state, michael@0: PRUint32 stackSize) michael@0: { michael@0: PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for AIX."); michael@0: } michael@0: #endif /* _PR_PTHREADS */ michael@0: #endif /* PTHREADS_USER */ michael@0: michael@0: /* michael@0: * NSPR 2.0 overrides the system select() and poll() functions. michael@0: * On AIX 4.2, we use dlopen("/unix", RTLD_NOW) and dlsym() to get michael@0: * at the original system select() and poll() functions. michael@0: */ michael@0: michael@0: #if !defined(AIX_RENAME_SELECT) michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static int (*aix_select_fcn)() = NULL; michael@0: static int (*aix_poll_fcn)() = NULL; michael@0: michael@0: int _MD_SELECT(int width, fd_set *r, fd_set *w, fd_set *e, struct timeval *t) michael@0: { michael@0: int rv; michael@0: michael@0: if (!aix_select_fcn) { michael@0: void *aix_handle; michael@0: michael@0: aix_handle = dlopen("/unix", RTLD_NOW); michael@0: if (!aix_handle) { michael@0: PR_SetError(PR_UNKNOWN_ERROR, 0); michael@0: return -1; michael@0: } michael@0: aix_select_fcn = (int(*)())dlsym(aix_handle,"select"); michael@0: dlclose(aix_handle); michael@0: if (!aix_select_fcn) { michael@0: PR_SetError(PR_UNKNOWN_ERROR, 0); michael@0: return -1; michael@0: } michael@0: } michael@0: rv = (*aix_select_fcn)(width, r, w, e, t); michael@0: return rv; michael@0: } michael@0: michael@0: int _MD_POLL(void *listptr, unsigned long nfds, long timeout) michael@0: { michael@0: int rv; michael@0: michael@0: if (!aix_poll_fcn) { michael@0: void *aix_handle; michael@0: michael@0: aix_handle = dlopen("/unix", RTLD_NOW); michael@0: if (!aix_handle) { michael@0: PR_SetError(PR_UNKNOWN_ERROR, 0); michael@0: return -1; michael@0: } michael@0: aix_poll_fcn = (int(*)())dlsym(aix_handle,"poll"); michael@0: dlclose(aix_handle); michael@0: if (!aix_poll_fcn) { michael@0: PR_SetError(PR_UNKNOWN_ERROR, 0); michael@0: return -1; michael@0: } michael@0: } michael@0: rv = (*aix_poll_fcn)(listptr, nfds, timeout); michael@0: return rv; michael@0: } michael@0: michael@0: #else michael@0: michael@0: /* michael@0: * In AIX versions prior to 4.2, we use the two-step rename/link trick. michael@0: * The binary must contain at least one "poll" symbol for linker's rename michael@0: * to work. So we must have this dummy function that references poll(). michael@0: */ michael@0: #include michael@0: void _pr_aix_dummy() michael@0: { michael@0: poll(0,0,0); michael@0: } michael@0: michael@0: #endif /* !defined(AIX_RENAME_SELECT) */ michael@0: michael@0: #ifdef _PR_HAVE_ATOMIC_CAS michael@0: michael@0: #include "pratom.h" michael@0: michael@0: #define _PR_AIX_ATOMIC_LOCK -1 michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PR_StackPush(PRStack *stack, PRStackElem *stack_elem) michael@0: { michael@0: PRStackElem *addr; michael@0: boolean_t locked = TRUE; michael@0: michael@0: /* Is it safe to cast a pointer to an int? */ michael@0: PR_ASSERT(sizeof(int) == sizeof(PRStackElem *)); michael@0: do { michael@0: while ((addr = stack->prstk_head.prstk_elem_next) == michael@0: (PRStackElem *)_PR_AIX_ATOMIC_LOCK) michael@0: ; michael@0: locked = _check_lock((atomic_p) &stack->prstk_head.prstk_elem_next, michael@0: (int) addr, _PR_AIX_ATOMIC_LOCK); michael@0: } while (locked == TRUE); michael@0: stack_elem->prstk_elem_next = addr; michael@0: _clear_lock((atomic_p)&stack->prstk_head.prstk_elem_next, (int)stack_elem); michael@0: return; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStackElem *) michael@0: PR_StackPop(PRStack *stack) michael@0: { michael@0: PRStackElem *element; michael@0: boolean_t locked = TRUE; michael@0: michael@0: /* Is it safe to cast a pointer to an int? */ michael@0: PR_ASSERT(sizeof(int) == sizeof(PRStackElem *)); michael@0: do { michael@0: while ((element = stack->prstk_head.prstk_elem_next) == michael@0: (PRStackElem *) _PR_AIX_ATOMIC_LOCK) michael@0: ; michael@0: locked = _check_lock((atomic_p) &stack->prstk_head.prstk_elem_next, michael@0: (int)element, _PR_AIX_ATOMIC_LOCK); michael@0: } while (locked == TRUE); michael@0: michael@0: if (element == NULL) { michael@0: _clear_lock((atomic_p) &stack->prstk_head.prstk_elem_next, NULL); michael@0: } else { michael@0: _clear_lock((atomic_p) &stack->prstk_head.prstk_elem_next, michael@0: (int) element->prstk_elem_next); michael@0: } michael@0: return element; michael@0: } michael@0: michael@0: #endif /* _PR_HAVE_ATOMIC_CAS */