_irix.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #ifndef nspr_irix_defs_h___
  38. #define nspr_irix_defs_h___
  39. #define _PR_HAVE_ATOMIC_CAS
  40. /*
  41. * MipsPro assembler defines _LANGUAGE_ASSEMBLY
  42. */
  43. #ifndef _LANGUAGE_ASSEMBLY
  44. #include "prclist.h"
  45. #include "prthread.h"
  46. #include <sys/ucontext.h>
  47. /*
  48. * Internal configuration macros
  49. */
  50. #define PR_LINKER_ARCH "irix"
  51. #define _PR_SI_SYSNAME "IRIX"
  52. #define _PR_SI_ARCHITECTURE "mips"
  53. #define PR_DLL_SUFFIX ".so"
  54. #define _PR_VMBASE 0x30000000
  55. #define _PR_STACK_VMBASE 0x50000000
  56. #define _PR_NUM_GCREGS 9
  57. #define _MD_MMAP_FLAGS MAP_PRIVATE
  58. #define _MD_DEFAULT_STACK_SIZE 65536L
  59. #define _MD_MIN_STACK_SIZE 16384L
  60. #undef HAVE_STACK_GROWING_UP
  61. #define HAVE_WEAK_IO_SYMBOLS
  62. #define HAVE_WEAK_MALLOC_SYMBOLS
  63. #define HAVE_DLL
  64. #define USE_DLFCN
  65. #define _PR_HAVE_ATOMIC_OPS
  66. #define _PR_POLL_AVAILABLE
  67. #define _PR_USE_POLL
  68. #define _PR_STAT_HAS_ST_ATIM
  69. #define _PR_HAVE_OFF64_T
  70. #define HAVE_POINTER_LOCALTIME_R
  71. #define _PR_HAVE_POSIX_SEMAPHORES
  72. #define PR_HAVE_POSIX_NAMED_SHARED_MEMORY
  73. #define _PR_ACCEPT_INHERIT_NONBLOCK
  74. #ifdef _PR_INET6
  75. #define _PR_HAVE_INET_NTOP
  76. #define _PR_HAVE_GETIPNODEBYNAME
  77. #define _PR_HAVE_GETIPNODEBYADDR
  78. #define _PR_HAVE_GETADDRINFO
  79. #endif
  80. /* Initialization entry points */
  81. NSPR_API(void) _MD_EarlyInit(void);
  82. #define _MD_EARLY_INIT _MD_EarlyInit
  83. NSPR_API(void) _MD_IrixInit(void);
  84. #define _MD_FINAL_INIT _MD_IrixInit
  85. #define _MD_INIT_IO()
  86. /* Timer operations */
  87. NSPR_API(PRIntervalTime) _MD_IrixGetInterval(void);
  88. #define _MD_GET_INTERVAL _MD_IrixGetInterval
  89. NSPR_API(PRIntervalTime) _MD_IrixIntervalPerSec(void);
  90. #define _MD_INTERVAL_PER_SEC _MD_IrixIntervalPerSec
  91. /* GC operations */
  92. NSPR_API(void *) _MD_GetSP(PRThread *thread);
  93. #define _MD_GET_SP _MD_GetSP
  94. /* The atomic operations */
  95. #include <mutex.h>
  96. #define _MD_INIT_ATOMIC()
  97. #define _MD_ATOMIC_INCREMENT(val) add_then_test((unsigned long*)val, 1)
  98. #define _MD_ATOMIC_ADD(ptr, val) add_then_test((unsigned long*)ptr, (unsigned long)val)
  99. #define _MD_ATOMIC_DECREMENT(val) add_then_test((unsigned long*)val, 0xffffffff)
  100. #define _MD_ATOMIC_SET(val, newval) test_and_set((unsigned long*)val, newval)
  101. #if defined(_PR_PTHREADS)
  102. #else /* defined(_PR_PTHREADS) */
  103. /************************************************************************/
  104. #include <setjmp.h>
  105. #include <errno.h>
  106. #include <unistd.h>
  107. #include <bstring.h>
  108. #include <sys/time.h>
  109. #include <ulocks.h>
  110. #include <sys/prctl.h>
  111. /*
  112. * Data region private to each sproc. This region is setup by calling
  113. * mmap(...,MAP_LOCAL,...). The private data is mapped at the same
  114. * address in every sproc, but every sproc gets a private mapping.
  115. *
  116. * Just make sure that this structure fits in a page, as only one page
  117. * is allocated for the private region.
  118. */
  119. struct sproc_private_data {
  120. struct PRThread *me;
  121. struct _PRCPU *cpu;
  122. struct PRThread *last;
  123. PRUintn intsOff;
  124. int sproc_pid;
  125. };
  126. extern char *_nspr_sproc_private;
  127. #define _PR_PRDA() ((struct sproc_private_data *) _nspr_sproc_private)
  128. #define _MD_SET_CURRENT_THREAD(_thread) _PR_PRDA()->me = (_thread)
  129. #define _MD_THIS_THREAD() (_PR_PRDA()->me)
  130. #define _MD_LAST_THREAD() (_PR_PRDA()->last)
  131. #define _MD_SET_LAST_THREAD(_thread) _PR_PRDA()->last = (_thread)
  132. #define _MD_CURRENT_CPU() (_PR_PRDA()->cpu)
  133. #define _MD_SET_CURRENT_CPU(_cpu) _PR_PRDA()->cpu = (_cpu)
  134. #define _MD_SET_INTSOFF(_val) (_PR_PRDA()->intsOff = _val)
  135. #define _MD_GET_INTSOFF() (_PR_PRDA()->intsOff)
  136. #define _MD_SET_SPROC_PID(_val) (_PR_PRDA()->sproc_pid = _val)
  137. #define _MD_GET_SPROC_PID() (_PR_PRDA()->sproc_pid)
  138. NSPR_API(struct PRThread*) _MD_get_attached_thread(void);
  139. NSPR_API(struct PRThread*) _MD_get_current_thread(void);
  140. #define _MD_GET_ATTACHED_THREAD() _MD_get_attached_thread()
  141. #define _MD_CURRENT_THREAD() _MD_get_current_thread()
  142. #define _MD_CHECK_FOR_EXIT() { \
  143. if (_pr_irix_exit_now) { \
  144. _PR_POST_SEM(_pr_irix_exit_sem); \
  145. _MD_Wakeup_CPUs(); \
  146. _exit(0); \
  147. } \
  148. }
  149. #define _MD_ATTACH_THREAD(threadp)
  150. #define _MD_SAVE_ERRNO(_thread) (_thread)->md.errcode = errno;
  151. #define _MD_RESTORE_ERRNO(_thread) errno = (_thread)->md.errcode;
  152. extern struct _PRCPU *_pr_primordialCPU;
  153. extern usema_t *_pr_irix_exit_sem;
  154. extern PRInt32 _pr_irix_exit_now;
  155. extern int _pr_irix_primoridal_cpu_fd[];
  156. extern PRInt32 _pr_irix_process_exit;
  157. extern PRInt32 _pr_irix_process_exit_code;
  158. /* Thread operations */
  159. #define _PR_LOCK_HEAP() { \
  160. PRIntn _is; \
  161. if (_pr_primordialCPU) { \
  162. if (_MD_GET_ATTACHED_THREAD() && \
  163. !_PR_IS_NATIVE_THREAD( \
  164. _MD_GET_ATTACHED_THREAD())) \
  165. _PR_INTSOFF(_is); \
  166. _PR_LOCK(_pr_heapLock); \
  167. }
  168. #define _PR_UNLOCK_HEAP() if (_pr_primordialCPU) { \
  169. _PR_UNLOCK(_pr_heapLock); \
  170. if (_MD_GET_ATTACHED_THREAD() && \
  171. !_PR_IS_NATIVE_THREAD( \
  172. _MD_GET_ATTACHED_THREAD())) \
  173. _PR_INTSON(_is); \
  174. } \
  175. }
  176. #define _PR_OPEN_POLL_SEM(_sem) usopenpollsema(_sem, 0666)
  177. #define _PR_WAIT_SEM(_sem) uspsema(_sem)
  178. #define _PR_POST_SEM(_sem) usvsema(_sem)
  179. #define _MD_CVAR_POST_SEM(threadp) usvsema((threadp)->md.cvar_pollsem)
  180. #define _MD_IOQ_LOCK()
  181. #define _MD_IOQ_UNLOCK()
  182. struct _MDLock {
  183. ulock_t lock;
  184. usptr_t *arena;
  185. };
  186. /*
  187. * disable pre-emption for the LOCAL threads when calling the arena lock
  188. * routines
  189. */
  190. #define _PR_LOCK(lock) { \
  191. PRIntn _is; \
  192. PRThread *me = _MD_GET_ATTACHED_THREAD(); \
  193. if (me && !_PR_IS_NATIVE_THREAD(me)) \
  194. _PR_INTSOFF(_is); \
  195. ussetlock(lock); \
  196. if (me && !_PR_IS_NATIVE_THREAD(me)) \
  197. _PR_FAST_INTSON(_is); \
  198. }
  199. #define _PR_UNLOCK(lock) { \
  200. PRIntn _is; \
  201. PRThread *me = _MD_GET_ATTACHED_THREAD(); \
  202. if (me && !_PR_IS_NATIVE_THREAD(me)) \
  203. _PR_INTSOFF(_is); \
  204. usunsetlock(lock); \
  205. if (me && !_PR_IS_NATIVE_THREAD(me)) \
  206. _PR_FAST_INTSON(_is); \
  207. }
  208. NSPR_API(PRStatus) _MD_NEW_LOCK(struct _MDLock *md);
  209. NSPR_API(void) _MD_FREE_LOCK(struct _MDLock *lockp);
  210. #define _MD_LOCK(_lockp) _PR_LOCK((_lockp)->lock)
  211. #define _MD_UNLOCK(_lockp) _PR_UNLOCK((_lockp)->lock)
  212. #define _MD_TEST_AND_LOCK(_lockp) (uscsetlock((_lockp)->lock, 1) == 0)
  213. extern ulock_t _pr_heapLock;
  214. struct _MDThread {
  215. jmp_buf jb;
  216. usptr_t *pollsem_arena;
  217. usema_t *cvar_pollsem;
  218. PRInt32 cvar_pollsemfd;
  219. PRInt32 cvar_pollsem_select; /* acquire sem by calling select */
  220. PRInt32 cvar_wait; /* if 1, thread is waiting on cvar Q */
  221. PRInt32 id;
  222. PRInt32 suspending_id;
  223. int errcode;
  224. };
  225. struct _MDThreadStack {
  226. PRInt8 notused;
  227. };
  228. struct _MDSemaphore {
  229. usema_t *sem;
  230. };
  231. struct _MDCVar {
  232. ulock_t mdcvar_lock;
  233. };
  234. struct _MDSegment {
  235. PRInt8 notused;
  236. };
  237. /*
  238. * md-specific cpu structure field
  239. */
  240. #define _PR_MD_MAX_OSFD FD_SETSIZE
  241. struct _MDCPU_Unix {
  242. PRCList ioQ;
  243. PRUint32 ioq_timeout;
  244. PRInt32 ioq_max_osfd;
  245. PRInt32 ioq_osfd_cnt;
  246. #ifndef _PR_USE_POLL
  247. fd_set fd_read_set, fd_write_set, fd_exception_set;
  248. PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
  249. fd_exception_cnt[_PR_MD_MAX_OSFD];
  250. #else
  251. struct pollfd *ioq_pollfds;
  252. int ioq_pollfds_size;
  253. #endif /* _PR_USE_POLL */
  254. };
  255. #define _PR_IOQ(_cpu) ((_cpu)->md.md_unix.ioQ)
  256. #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
  257. #define _PR_FD_READ_SET(_cpu) ((_cpu)->md.md_unix.fd_read_set)
  258. #define _PR_FD_READ_CNT(_cpu) ((_cpu)->md.md_unix.fd_read_cnt)
  259. #define _PR_FD_WRITE_SET(_cpu) ((_cpu)->md.md_unix.fd_write_set)
  260. #define _PR_FD_WRITE_CNT(_cpu) ((_cpu)->md.md_unix.fd_write_cnt)
  261. #define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set)
  262. #define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt)
  263. #define _PR_IOQ_TIMEOUT(_cpu) ((_cpu)->md.md_unix.ioq_timeout)
  264. #define _PR_IOQ_MAX_OSFD(_cpu) ((_cpu)->md.md_unix.ioq_max_osfd)
  265. #define _PR_IOQ_OSFD_CNT(_cpu) ((_cpu)->md.md_unix.ioq_osfd_cnt)
  266. #define _PR_IOQ_POLLFDS(_cpu) ((_cpu)->md.md_unix.ioq_pollfds)
  267. #define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size)
  268. #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32
  269. struct _MDCPU {
  270. PRInt32 id;
  271. PRInt32 suspending_id;
  272. struct _MDCPU_Unix md_unix;
  273. };
  274. /*
  275. ** Initialize the thread context preparing it to execute _main.
  276. */
  277. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \
  278. PR_BEGIN_MACRO \
  279. int *jb = (_thread)->md.jb; \
  280. *status = PR_TRUE; \
  281. (void) setjmp(jb); \
  282. (_thread)->md.jb[JB_SP] = (int) ((_sp) - 64); \
  283. (_thread)->md.jb[JB_PC] = (int) _main; \
  284. _thread->no_sched = 0; \
  285. PR_END_MACRO
  286. /*
  287. ** Switch away from the current thread context by saving its state and
  288. ** calling the thread scheduler. Reload cpu when we come back from the
  289. ** context switch because it might have changed.
  290. *
  291. * XXX RUNQ lock needed before clearing _PR_NO_SCHED flag, because the
  292. * thread may be unr RUNQ?
  293. */
  294. #define _MD_SWITCH_CONTEXT(_thread) \
  295. PR_BEGIN_MACRO \
  296. PR_ASSERT(_thread->no_sched); \
  297. if (!setjmp(_thread->md.jb)) { \
  298. _MD_SAVE_ERRNO(_thread) \
  299. _MD_SET_LAST_THREAD(_thread); \
  300. _PR_Schedule(); \
  301. } else { \
  302. PR_ASSERT(_MD_LAST_THREAD() !=_MD_CURRENT_THREAD()); \
  303. _MD_LAST_THREAD()->no_sched = 0; \
  304. } \
  305. PR_END_MACRO
  306. /*
  307. ** Restore a thread context that was saved by _MD_SWITCH_CONTEXT or
  308. ** initialized by _MD_INIT_CONTEXT.
  309. */
  310. #define _MD_RESTORE_CONTEXT(_newThread) \
  311. PR_BEGIN_MACRO \
  312. int *jb = (_newThread)->md.jb; \
  313. _MD_RESTORE_ERRNO(_newThread) \
  314. _MD_SET_CURRENT_THREAD(_newThread); \
  315. _newThread->no_sched = 1; \
  316. longjmp(jb, 1); \
  317. PR_END_MACRO
  318. NSPR_API(PRStatus) _MD_InitThread(struct PRThread *thread,
  319. PRBool wakeup_parent);
  320. NSPR_API(PRStatus) _MD_InitAttachedThread(struct PRThread *thread,
  321. PRBool wakeup_parent);
  322. #define _MD_INIT_THREAD(thread) _MD_InitThread(thread, PR_TRUE)
  323. #define _MD_INIT_ATTACHED_THREAD(thread) \
  324. _MD_InitAttachedThread(thread, PR_FALSE)
  325. NSPR_API(void) _MD_ExitThread(struct PRThread *thread);
  326. #define _MD_EXIT_THREAD _MD_ExitThread
  327. NSPR_API(void) _MD_SuspendThread(struct PRThread *thread);
  328. #define _MD_SUSPEND_THREAD _MD_SuspendThread
  329. NSPR_API(void) _MD_ResumeThread(struct PRThread *thread);
  330. #define _MD_RESUME_THREAD _MD_ResumeThread
  331. NSPR_API(void) _MD_SuspendCPU(struct _PRCPU *thread);
  332. #define _MD_SUSPEND_CPU _MD_SuspendCPU
  333. NSPR_API(void) _MD_ResumeCPU(struct _PRCPU *thread);
  334. #define _MD_RESUME_CPU _MD_ResumeCPU
  335. #define _MD_BEGIN_SUSPEND_ALL()
  336. #define _MD_END_SUSPEND_ALL()
  337. #define _MD_BEGIN_RESUME_ALL()
  338. #define _MD_END_RESUME_ALL()
  339. NSPR_API(void) _MD_InitLocks(void);
  340. #define _MD_INIT_LOCKS _MD_InitLocks
  341. NSPR_API(void) _MD_CleanThread(struct PRThread *thread);
  342. #define _MD_CLEAN_THREAD _MD_CleanThread
  343. #define _MD_YIELD() sginap(0)
  344. /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
  345. * awaken a thread which is waiting on a lock or cvar.
  346. */
  347. NSPR_API(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout);
  348. #define _MD_WAIT _MD_wait
  349. NSPR_API(void) _PR_MD_primordial_cpu();
  350. NSPR_API(void) _PR_MD_WAKEUP_PRIMORDIAL_CPU();
  351. NSPR_API(PRStatus) _MD_WakeupWaiter(struct PRThread *);
  352. #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
  353. NSPR_API(void ) _MD_exit(PRIntn status);
  354. #define _MD_EXIT _MD_exit
  355. #include "prthread.h"
  356. NSPR_API(void) _MD_SetPriority(struct _MDThread *thread,
  357. PRThreadPriority newPri);
  358. #define _MD_SET_PRIORITY _MD_SetPriority
  359. NSPR_API(PRStatus) _MD_CreateThread(
  360. struct PRThread *thread,
  361. void (*start) (void *),
  362. PRThreadPriority priority,
  363. PRThreadScope scope,
  364. PRThreadState state,
  365. PRUint32 stackSize);
  366. #define _MD_CREATE_THREAD _MD_CreateThread
  367. extern void _MD_CleanupBeforeExit(void);
  368. #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
  369. NSPR_API(void) _PR_MD_PRE_CLEANUP(PRThread *me);
  370. /* The following defines the unwrapped versions of select() and poll(). */
  371. extern int _select(int nfds, fd_set *readfds, fd_set *writefds,
  372. fd_set *exceptfds, struct timeval *timeout);
  373. #define _MD_SELECT _select
  374. #include <stropts.h>
  375. #include <poll.h>
  376. #define _MD_POLL _poll
  377. extern int _poll(struct pollfd *fds, unsigned long nfds, int timeout);
  378. #define HAVE_THREAD_AFFINITY 1
  379. NSPR_API(PRInt32) _MD_GetThreadAffinityMask(PRThread *unused, PRUint32 *mask);
  380. #define _MD_GETTHREADAFFINITYMASK _MD_GetThreadAffinityMask
  381. NSPR_API(void) _MD_InitRunningCPU(struct _PRCPU *cpu);
  382. #define _MD_INIT_RUNNING_CPU _MD_InitRunningCPU
  383. #endif /* defined(_PR_PTHREADS) */
  384. #endif /* _LANGUAGE_ASSEMBLY */
  385. #endif /* nspr_irix_defs_h___ */