home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / md / unix / freebsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.3 KB  |  233 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "primpl.h"
  20.  
  21. #include <signal.h>
  22.  
  23. void _MD_EarlyInit(void)
  24. {
  25.     /*
  26.      * Ignore FPE because coercion of a NaN to an int causes SIGFPE
  27.      * to be raised.
  28.      */
  29.     struct sigaction act;
  30.  
  31.     act.sa_handler = SIG_IGN;
  32.     sigemptyset(&act.sa_mask);
  33.     act.sa_flags = SA_RESTART;
  34.     sigaction(SIGFPE, &act, 0);
  35. }
  36.  
  37. PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
  38. {
  39. #ifndef _PR_PTHREADS
  40.     if (isCurrent) {
  41.     (void) sigsetjmp(CONTEXT(t), 1);
  42.     }
  43.     *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
  44.     return (PRWord *) CONTEXT(t);
  45. #else
  46.     *np = 0;
  47.     return NULL;
  48. #endif
  49. }
  50.  
  51. #ifndef _PR_PTHREADS
  52. void
  53. _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
  54. {
  55.     return;
  56. }
  57.  
  58. PRStatus
  59. _MD_InitializeThread(PRThread *thread)
  60. {
  61.     return PR_SUCCESS;
  62. }
  63.  
  64. PRStatus
  65. _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
  66. {
  67.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  68.     _PR_MD_SWITCH_CONTEXT(thread);
  69.     return PR_SUCCESS;
  70. }
  71.  
  72. PRStatus
  73. _MD_WAKEUP_WAITER(PRThread *thread)
  74. {
  75.     if (thread) {
  76.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  77.     }
  78.     return PR_SUCCESS;
  79. }
  80.  
  81. /* These functions should not be called for OSF1 */
  82. void
  83. _MD_YIELD(void)
  84. {
  85.     PR_NOT_REACHED("_MD_YIELD should not be called for OSF1.");
  86. }
  87.  
  88. PRStatus
  89. _MD_CREATE_THREAD(
  90.     PRThread *thread,
  91.     void (*start) (void *),
  92.     PRThreadPriority priority,
  93.     PRThreadScope scope,
  94.     PRThreadState state,
  95.     PRUint32 stackSize)
  96. {
  97.     PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for OSF1.");
  98.     return PR_FAILURE;
  99. }
  100. #endif /* ! _PR_PTHREADS */
  101.  
  102. #if defined(_PR_NEED_FAKE_POLL)
  103.  
  104. #include <fcntl.h>
  105.  
  106. int poll(struct pollfd *filedes, unsigned long nfds, int timeout)
  107. {
  108.     int i;
  109.     int rv;
  110.     int maxfd;
  111.     fd_set rd, wr, ex;
  112.     struct timeval tv, *tvp;
  113.  
  114.     if (timeout < 0 && timeout != -1) {
  115.     errno = EINVAL;
  116.     return -1;
  117.     }
  118.  
  119.     if (timeout == -1) {
  120.         tvp = NULL;
  121.     } else {
  122.     tv.tv_sec = timeout / 1000;
  123.     tv.tv_usec = (timeout % 1000) * 1000;
  124.         tvp = &tv;
  125.     }
  126.  
  127.     maxfd = -1;
  128.     FD_ZERO(&rd);
  129.     FD_ZERO(&wr);
  130.     FD_ZERO(&ex);
  131.  
  132.     for (i = 0; i < nfds; i++) {
  133.     int osfd = filedes[i].fd;
  134.     int events = filedes[i].events;
  135.     PRBool fdHasEvent = PR_FALSE;
  136.  
  137.     if (osfd < 0) {
  138.             continue;  /* Skip this osfd. */
  139.     }
  140.  
  141.     /*
  142.      * Map the native poll flags to nspr poll flags.
  143.      *     POLLIN, POLLRDNORM  ===> PR_POLL_READ
  144.      *     POLLOUT, POLLWRNORM ===> PR_POLL_WRITE
  145.      *     POLLPRI, POLLRDBAND ===> PR_POLL_EXCEPTION
  146.      *     POLLNORM, POLLWRBAND (and POLLMSG on some platforms)
  147.      *     are ignored.
  148.      *
  149.      * The output events POLLERR and POLLHUP are never turned on.
  150.      * POLLNVAL may be turned on.
  151.      */
  152.  
  153.     if (events & (POLLIN | POLLRDNORM)) {
  154.         FD_SET(osfd, &rd);
  155.         fdHasEvent = PR_TRUE;
  156.     }
  157.     if (events & (POLLOUT | POLLWRNORM)) {
  158.         FD_SET(osfd, &wr);
  159.         fdHasEvent = PR_TRUE;
  160.     }
  161.     if (events & (POLLPRI | POLLRDBAND)) {
  162.         FD_SET(osfd, &ex);
  163.         fdHasEvent = PR_TRUE;
  164.     }
  165.     if (fdHasEvent && osfd > maxfd) {
  166.         maxfd = osfd;
  167.     }
  168.     }
  169.  
  170.     rv = select(maxfd + 1, &rd, &wr, &ex, tvp);
  171.  
  172.     /* Compute poll results */
  173.     if (rv > 0) {
  174.     rv = 0;
  175.         for (i = 0; i < nfds; i++) {
  176.         PRBool fdHasEvent = PR_FALSE;
  177.  
  178.         filedes[i].revents = 0;
  179.             if (filedes[i].fd < 0) {
  180.                 continue;
  181.             }
  182.         if (FD_ISSET(filedes[i].fd, &rd)) {
  183.         if (filedes[i].events & POLLIN) {
  184.             filedes[i].revents |= POLLIN;
  185.         }
  186.         if (filedes[i].events & POLLRDNORM) {
  187.             filedes[i].revents |= POLLRDNORM;
  188.         }
  189.         fdHasEvent = PR_TRUE;
  190.         }
  191.         if (FD_ISSET(filedes[i].fd, &wr)) {
  192.         if (filedes[i].events & POLLOUT) {
  193.             filedes[i].revents |= POLLOUT;
  194.         }
  195.         if (filedes[i].events & POLLWRNORM) {
  196.             filedes[i].revents |= POLLWRNORM;
  197.         }
  198.         fdHasEvent = PR_TRUE;
  199.         }
  200.         if (FD_ISSET(filedes[i].fd, &ex)) {
  201.         if (filedes[i].events & POLLPRI) {
  202.             filedes[i].revents |= POLLPRI;
  203.         }
  204.         if (filedes[i].events & POLLRDBAND) {
  205.             filedes[i].revents |= POLLRDBAND;
  206.         }
  207.         fdHasEvent = PR_TRUE;
  208.         }
  209.         if (fdHasEvent) {
  210.         rv++;
  211.             }
  212.         }
  213.     PR_ASSERT(rv > 0);
  214.     } else if (rv == -1 && errno == EBADF) {
  215.     rv = 0;
  216.         for (i = 0; i < nfds; i++) {
  217.         filedes[i].revents = 0;
  218.             if (filedes[i].fd < 0) {
  219.                 continue;
  220.             }
  221.         if (fcntl(filedes[i].fd, F_GETFL, 0) == -1) {
  222.         filedes[i].revents = POLLNVAL;
  223.         rv++;
  224.         }
  225.         }
  226.     PR_ASSERT(rv > 0);
  227.     }
  228.     PR_ASSERT(-1 != timeout || rv != 0);
  229.  
  230.     return rv;
  231. }
  232. #endif /* _PR_NEED_FAKE_POLL */
  233.