PageRenderTime 90ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/mozjs/nsprpub/pr/src/md/beos/beos.c

http://github.com/zpao/spidernode
C | 264 lines | 159 code | 35 blank | 70 comment | 17 complexity | 910e385644726013660287a5b7eb0602 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, JSON, BSD-2-Clause, BSD-3-Clause
  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 <signal.h>
  39. #include <unistd.h>
  40. #include <memory.h>
  41. #include <fcntl.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/time.h>
  45. #include <sys/ioctl.h>
  46. #include <errno.h>
  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. ** Global lock variable used to bracket calls into rusty libraries that
  54. ** aren't thread safe (like libc, libX, etc).
  55. */
  56. static PRLock *_pr_rename_lock = NULL;
  57. static PRMonitor *_pr_Xfe_mon = NULL;
  58. /*
  59. * Variables used by the GC code, initialized in _MD_InitSegs().
  60. * _pr_zero_fd should be a static variable. Unfortunately, there is
  61. * still some Unix-specific code left in function PR_GrowSegment()
  62. * in file memory/prseg.c that references it, so it needs
  63. * to be a global variable for now.
  64. */
  65. PRInt32 _pr_zero_fd = -1;
  66. static PRLock *_pr_md_lock = NULL;
  67. sigset_t timer_set;
  68. void _PR_UnixInit()
  69. {
  70. struct sigaction sigact;
  71. int rv;
  72. sigemptyset(&timer_set);
  73. sigact.sa_handler = SIG_IGN;
  74. sigemptyset(&sigact.sa_mask);
  75. sigact.sa_flags = 0;
  76. rv = sigaction(SIGPIPE, &sigact, 0);
  77. PR_ASSERT(0 == rv);
  78. _pr_rename_lock = PR_NewLock();
  79. PR_ASSERT(NULL != _pr_rename_lock);
  80. _pr_Xfe_mon = PR_NewMonitor();
  81. PR_ASSERT(NULL != _pr_Xfe_mon);
  82. }
  83. /*
  84. *-----------------------------------------------------------------------
  85. *
  86. * PR_Now --
  87. *
  88. * Returns the current time in microseconds since the epoch.
  89. * The epoch is midnight January 1, 1970 GMT.
  90. * The implementation is machine dependent. This is the Unix
  91. * implementation.
  92. * Cf. time_t time(time_t *tp)
  93. *
  94. *-----------------------------------------------------------------------
  95. */
  96. PR_IMPLEMENT(PRTime)
  97. PR_Now(void)
  98. {
  99. struct timeval tv;
  100. PRInt64 s, us, s2us;
  101. GETTIMEOFDAY(&tv);
  102. LL_I2L(s2us, PR_USEC_PER_SEC);
  103. LL_I2L(s, tv.tv_sec);
  104. LL_I2L(us, tv.tv_usec);
  105. LL_MUL(s, s, s2us);
  106. LL_ADD(s, s, us);
  107. return s;
  108. }
  109. PRIntervalTime
  110. _PR_UNIX_GetInterval()
  111. {
  112. struct timeval time;
  113. PRIntervalTime ticks;
  114. (void)GETTIMEOFDAY(&time); /* fallicy of course */
  115. ticks = (PRUint32)time.tv_sec * PR_MSEC_PER_SEC; /* that's in milliseconds */
  116. ticks += (PRUint32)time.tv_usec / PR_USEC_PER_MSEC; /* so's that */
  117. return ticks;
  118. } /* _PR_SUNOS_GetInterval */
  119. PRIntervalTime _PR_UNIX_TicksPerSecond()
  120. {
  121. return 1000; /* this needs some work :) */
  122. }
  123. /************************************************************************/
  124. /*
  125. ** Special hacks for xlib. Xlib/Xt/Xm is not re-entrant nor is it thread
  126. ** safe. Unfortunately, neither is mozilla. To make these programs work
  127. ** in a pre-emptive threaded environment, we need to use a lock.
  128. */
  129. void PR_XLock()
  130. {
  131. PR_EnterMonitor(_pr_Xfe_mon);
  132. }
  133. void PR_XUnlock()
  134. {
  135. PR_ExitMonitor(_pr_Xfe_mon);
  136. }
  137. PRBool PR_XIsLocked()
  138. {
  139. return (PR_InMonitor(_pr_Xfe_mon)) ? PR_TRUE : PR_FALSE;
  140. }
  141. void PR_XWait(int ms)
  142. {
  143. PR_Wait(_pr_Xfe_mon, PR_MillisecondsToInterval(ms));
  144. }
  145. void PR_XNotify(void)
  146. {
  147. PR_Notify(_pr_Xfe_mon);
  148. }
  149. void PR_XNotifyAll(void)
  150. {
  151. PR_NotifyAll(_pr_Xfe_mon);
  152. }
  153. #if !defined(BEOS)
  154. #ifdef HAVE_BSD_FLOCK
  155. #include <sys/file.h>
  156. PR_IMPLEMENT(PRStatus)
  157. _MD_LOCKFILE (PRInt32 f)
  158. {
  159. PRInt32 rv;
  160. rv = flock(f, LOCK_EX);
  161. if (rv == 0)
  162. return PR_SUCCESS;
  163. _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
  164. return PR_FAILURE;
  165. }
  166. PR_IMPLEMENT(PRStatus)
  167. _MD_TLOCKFILE (PRInt32 f)
  168. {
  169. PRInt32 rv;
  170. rv = flock(f, LOCK_EX|LOCK_NB);
  171. if (rv == 0)
  172. return PR_SUCCESS;
  173. _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
  174. return PR_FAILURE;
  175. }
  176. PR_IMPLEMENT(PRStatus)
  177. _MD_UNLOCKFILE (PRInt32 f)
  178. {
  179. PRInt32 rv;
  180. rv = flock(f, LOCK_UN);
  181. if (rv == 0)
  182. return PR_SUCCESS;
  183. _PR_MD_MAP_FLOCK_ERROR(_MD_ERRNO());
  184. return PR_FAILURE;
  185. }
  186. #else
  187. PR_IMPLEMENT(PRStatus)
  188. _MD_LOCKFILE (PRInt32 f)
  189. {
  190. PRInt32 rv;
  191. rv = lockf(f, F_LOCK, 0);
  192. if (rv == 0)
  193. return PR_SUCCESS;
  194. _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
  195. return PR_FAILURE;
  196. }
  197. PR_IMPLEMENT(PRStatus)
  198. _MD_TLOCKFILE (PRInt32 f)
  199. {
  200. PRInt32 rv;
  201. rv = lockf(f, F_TLOCK, 0);
  202. if (rv == 0)
  203. return PR_SUCCESS;
  204. _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
  205. return PR_FAILURE;
  206. }
  207. PR_IMPLEMENT(PRStatus)
  208. _MD_UNLOCKFILE (PRInt32 f)
  209. {
  210. PRInt32 rv;
  211. rv = lockf(f, F_ULOCK, 0);
  212. if (rv == 0)
  213. return PR_SUCCESS;
  214. _PR_MD_MAP_LOCKF_ERROR(_MD_ERRNO());
  215. return PR_FAILURE;
  216. }
  217. #endif
  218. PR_IMPLEMENT(PRStatus)
  219. _MD_GETHOSTNAME (char *name, PRUint32 namelen)
  220. {
  221. PRIntn rv;
  222. rv = gethostname(name, namelen);
  223. if (0 == rv) {
  224. return PR_SUCCESS;
  225. }
  226. _PR_MD_MAP_GETHOSTNAME_ERROR(_MD_ERRNO());
  227. return PR_FAILURE;
  228. }
  229. #endif