PageRenderTime 67ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/ppanhoto/Freeswitch-mod_mp4
C | 261 lines | 147 code | 31 blank | 83 comment | 12 complexity | 47e39128f91585eb4eae376080435cf7 MD5 | raw file
  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. #include "primpl.h"
  38. #include <setjmp.h>
  39. #if defined(HPUX_LW_TIMER)
  40. #include <machine/inline.h>
  41. #include <machine/clock.h>
  42. #include <unistd.h>
  43. #include <sys/time.h>
  44. #include <sys/pstat.h>
  45. int __lw_get_thread_times(int which, int64_t *sample, int64_t *time);
  46. static double msecond_per_itick;
  47. void _PR_HPUX_LW_IntervalInit(void)
  48. {
  49. struct pst_processor psp;
  50. int iticksperclktick, clk_tck;
  51. int rv;
  52. rv = pstat_getprocessor(&psp, sizeof(psp), 1, 0);
  53. PR_ASSERT(rv != -1);
  54. iticksperclktick = psp.psp_iticksperclktick;
  55. clk_tck = sysconf(_SC_CLK_TCK);
  56. msecond_per_itick = (1000.0)/(double)(iticksperclktick * clk_tck);
  57. }
  58. PRIntervalTime _PR_HPUX_LW_GetInterval(void)
  59. {
  60. int64_t time, sample;
  61. __lw_get_thread_times(1, &sample, &time);
  62. /*
  63. * Division is slower than float multiplication.
  64. * return (time / iticks_per_msecond);
  65. */
  66. return (time * msecond_per_itick);
  67. }
  68. #endif /* HPUX_LW_TIMER */
  69. #if !defined(PTHREADS_USER)
  70. void _MD_EarlyInit(void)
  71. {
  72. #ifndef _PR_PTHREADS
  73. /*
  74. * The following piece of code is taken from ns/nspr/src/md_HP-UX.c.
  75. * In the comment for revision 1.6, dated 1995/09/11 23:33:34,
  76. * robm says:
  77. * This version has some problems which need to be addressed.
  78. * First, intercept all system calls and prevent them from
  79. * executing the library code which performs stack switches
  80. * before normal system call invocation. In order for library
  81. * calls which make system calls to work (like stdio), however,
  82. * we must also allocate our own stack and switch the primordial
  83. * stack to use it. This isn't so bad, except that I fudged the
  84. * backtrace length when copying the old stack to the new one.
  85. *
  86. * This is the original comment of robm in the code:
  87. * XXXrobm Horrific. To avoid a problem with HP's system call
  88. * code, we allocate a new stack for the primordial thread and
  89. * use it. However, we don't know how far back the original stack
  90. * goes. We should create a routine that performs a backtrace and
  91. * finds out just how much we need to copy. As a temporary measure,
  92. * I just copy an arbitrary guess.
  93. *
  94. * In an email to servereng dated 2 Jan 1997, Mike Patnode (mikep)
  95. * suggests that this only needs to be done for HP-UX 9.
  96. */
  97. #ifdef HPUX9
  98. #define PIDOOMA_STACK_SIZE 524288
  99. #define BACKTRACE_SIZE 8192
  100. {
  101. jmp_buf jb;
  102. char *newstack;
  103. char *oldstack;
  104. if(!setjmp(jb)) {
  105. newstack = (char *) PR_MALLOC(PIDOOMA_STACK_SIZE);
  106. oldstack = (char *) (*(((int *) jb) + 1) - BACKTRACE_SIZE);
  107. memcpy(newstack, oldstack, BACKTRACE_SIZE);
  108. *(((int *) jb) + 1) = (int) (newstack + BACKTRACE_SIZE);
  109. longjmp(jb, 1);
  110. }
  111. }
  112. #endif /* HPUX9 */
  113. #endif /* !_PR_PTHREADS */
  114. }
  115. PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
  116. {
  117. #ifndef _PR_PTHREADS
  118. if (isCurrent) {
  119. (void) setjmp(CONTEXT(t));
  120. }
  121. *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
  122. return (PRWord *) CONTEXT(t);
  123. #else
  124. *np = 0;
  125. return NULL;
  126. #endif
  127. }
  128. #ifndef _PR_PTHREADS
  129. void
  130. _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
  131. {
  132. return;
  133. }
  134. PRStatus
  135. _MD_InitializeThread(PRThread *thread)
  136. {
  137. return PR_SUCCESS;
  138. }
  139. PRStatus
  140. _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
  141. {
  142. PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  143. _PR_MD_SWITCH_CONTEXT(thread);
  144. return PR_SUCCESS;
  145. }
  146. PRStatus
  147. _MD_WAKEUP_WAITER(PRThread *thread)
  148. {
  149. if (thread) {
  150. PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  151. }
  152. return PR_SUCCESS;
  153. }
  154. /* These functions should not be called for HP-UX */
  155. void
  156. _MD_YIELD(void)
  157. {
  158. PR_NOT_REACHED("_MD_YIELD should not be called for HP-UX.");
  159. }
  160. PRStatus
  161. _MD_CREATE_THREAD(
  162. PRThread *thread,
  163. void (*start) (void *),
  164. PRThreadPriority priority,
  165. PRThreadScope scope,
  166. PRThreadState state,
  167. PRUint32 stackSize)
  168. {
  169. PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for HP-UX.");
  170. }
  171. #endif /* _PR_PTHREADS */
  172. void
  173. _MD_suspend_thread(PRThread *thread)
  174. {
  175. #ifdef _PR_PTHREADS
  176. #endif
  177. }
  178. void
  179. _MD_resume_thread(PRThread *thread)
  180. {
  181. #ifdef _PR_PTHREADS
  182. #endif
  183. }
  184. #endif /* PTHREADS_USER */
  185. /*
  186. * The HP version of strchr is buggy. It looks past the end of the
  187. * string and causes a segmentation fault when our (NSPR) version
  188. * of malloc is used.
  189. *
  190. * A better solution might be to put a cushion in our malloc just in
  191. * case HP's version of strchr somehow gets used instead of this one.
  192. */
  193. char *
  194. strchr(const char *s, int c)
  195. {
  196. char ch;
  197. if (!s) {
  198. return NULL;
  199. }
  200. ch = (char) c;
  201. while ((*s) && ((*s) != ch)) {
  202. s++;
  203. }
  204. if ((*s) == ch) {
  205. return (char *) s;
  206. }
  207. return NULL;
  208. }
  209. /*
  210. * Implemementation of memcmp in HP-UX (verified on releases A.09.03,
  211. * A.09.07, and B.10.10) dumps core if called with:
  212. * 1. First operand with address = 1(mod 4).
  213. * 2. Size = 1(mod 4)
  214. * 3. Last byte of the second operand is the last byte of the page and
  215. * next page is not accessible(not mapped or protected)
  216. * Thus, using the following naive version (tons of optimizations are
  217. * possible;^)
  218. */
  219. int memcmp(const void *s1, const void *s2, size_t n)
  220. {
  221. register unsigned char *p1 = (unsigned char *) s1,
  222. *p2 = (unsigned char *) s2;
  223. while (n-- > 0) {
  224. register int r = ((int) ((unsigned int) *p1))
  225. - ((int) ((unsigned int) *p2));
  226. if (r) return r;
  227. p1++; p2++;
  228. }
  229. return 0;
  230. }