home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / md / unix / linux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.2 KB  |  231 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. void _MD_EarlyInit(void)
  22. {
  23. }
  24.  
  25. PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
  26. {
  27. #ifndef _PR_PTHREADS
  28.     if (isCurrent) {
  29.     (void) setjmp(CONTEXT(t));
  30.     }
  31.     *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
  32.     return (PRWord *) CONTEXT(t);
  33. #else
  34.     *np = 0;
  35.     return NULL;
  36. #endif
  37. }
  38.  
  39. #ifdef _PR_PTHREADS
  40.  
  41. extern void _MD_unix_terminate_waitpid_daemon(void);
  42.  
  43. void _MD_CleanupBeforeExit(void)
  44. {
  45.     _MD_unix_terminate_waitpid_daemon();
  46. }
  47.  
  48. #else /* ! _PR_PTHREADS */
  49.  
  50. void
  51. _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
  52. {
  53.     return;
  54. }
  55.  
  56. PRStatus
  57. _MD_InitializeThread(PRThread *thread)
  58. {
  59.     return PR_SUCCESS;
  60. }
  61.  
  62. PRStatus
  63. _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
  64. {
  65.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  66.     _PR_MD_SWITCH_CONTEXT(thread);
  67.     return PR_SUCCESS;
  68. }
  69.  
  70. PRStatus
  71. _MD_WAKEUP_WAITER(PRThread *thread)
  72. {
  73.     if (thread) {
  74.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  75.     }
  76.     return PR_SUCCESS;
  77. }
  78.  
  79. /* These functions should not be called for OSF1 */
  80. void
  81. _MD_YIELD(void)
  82. {
  83.     PR_NOT_REACHED("_MD_YIELD should not be called for OSF1.");
  84. }
  85.  
  86. PRStatus
  87. _MD_CREATE_THREAD(
  88.     PRThread *thread,
  89.     void (*start) (void *),
  90.     PRThreadPriority priority,
  91.     PRThreadScope scope,
  92.     PRThreadState state,
  93.     PRUint32 stackSize)
  94. {
  95.     PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for OSF1.");
  96.     return PR_FAILURE;
  97. }
  98. #endif /* ! _PR_PTHREADS */
  99.  
  100. #if defined(_PR_NEED_FAKE_POLL)
  101.  
  102. #include <fcntl.h>
  103.  
  104. int poll(struct pollfd *filedes, unsigned long nfds, int timeout)
  105. {
  106.     int i;
  107.     int rv;
  108.     int maxfd;
  109.     fd_set rd, wr, ex;
  110.     struct timeval tv, *tvp;
  111.  
  112.     if (timeout < 0 && timeout != -1) {
  113.     errno = EINVAL;
  114.     return -1;
  115.     }
  116.  
  117.     if (timeout == -1) {
  118.         tvp = NULL;
  119.     } else {
  120.     tv.tv_sec = timeout / 1000;
  121.     tv.tv_usec = (timeout % 1000) * 1000;
  122.         tvp = &tv;
  123.     }
  124.  
  125.     maxfd = -1;
  126.     FD_ZERO(&rd);
  127.     FD_ZERO(&wr);
  128.     FD_ZERO(&ex);
  129.  
  130.     for (i = 0; i < nfds; i++) {
  131.     int osfd = filedes[i].fd;
  132.     int events = filedes[i].events;
  133.     PRBool fdHasEvent = PR_FALSE;
  134.  
  135.     if (osfd < 0) {
  136.             continue;  /* Skip this osfd. */
  137.     }
  138.  
  139.     /*
  140.      * Map the native poll flags to nspr poll flags.
  141.      *     POLLIN, POLLRDNORM  ===> PR_POLL_READ
  142.      *     POLLOUT, POLLWRNORM ===> PR_POLL_WRITE
  143.      *     POLLPRI, POLLRDBAND ===> PR_POLL_EXCEPTION
  144.      *     POLLNORM, POLLWRBAND (and POLLMSG on some platforms)
  145.      *     are ignored.
  146.      *
  147.      * The output events POLLERR and POLLHUP are never turned on.
  148.      * POLLNVAL may be turned on.
  149.      */
  150.  
  151.     if (events & (POLLIN | POLLRDNORM)) {
  152.         FD_SET(osfd, &rd);
  153.         fdHasEvent = PR_TRUE;
  154.     }
  155.     if (events & (POLLOUT | POLLWRNORM)) {
  156.         FD_SET(osfd, &wr);
  157.         fdHasEvent = PR_TRUE;
  158.     }
  159.     if (events & (POLLPRI | POLLRDBAND)) {
  160.         FD_SET(osfd, &ex);
  161.         fdHasEvent = PR_TRUE;
  162.     }
  163.     if (fdHasEvent && osfd > maxfd) {
  164.         maxfd = osfd;
  165.     }
  166.     }
  167.  
  168.     rv = select(maxfd + 1, &rd, &wr, &ex, tvp);
  169.  
  170.     /* Compute poll results */
  171.     if (rv > 0) {
  172.     rv = 0;
  173.         for (i = 0; i < nfds; i++) {
  174.         PRBool fdHasEvent = PR_FALSE;
  175.  
  176.         filedes[i].revents = 0;
  177.             if (filedes[i].fd < 0) {
  178.                 continue;
  179.             }
  180.         if (FD_ISSET(filedes[i].fd, &rd)) {
  181.         if (filedes[i].events & POLLIN) {
  182.             filedes[i].revents |= POLLIN;
  183.         }
  184.         if (filedes[i].events & POLLRDNORM) {
  185.             filedes[i].revents |= POLLRDNORM;
  186.         }
  187.         fdHasEvent = PR_TRUE;
  188.         }
  189.         if (FD_ISSET(filedes[i].fd, &wr)) {
  190.         if (filedes[i].events & POLLOUT) {
  191.             filedes[i].revents |= POLLOUT;
  192.         }
  193.         if (filedes[i].events & POLLWRNORM) {
  194.             filedes[i].revents |= POLLWRNORM;
  195.         }
  196.         fdHasEvent = PR_TRUE;
  197.         }
  198.         if (FD_ISSET(filedes[i].fd, &ex)) {
  199.         if (filedes[i].events & POLLPRI) {
  200.             filedes[i].revents |= POLLPRI;
  201.         }
  202.         if (filedes[i].events & POLLRDBAND) {
  203.             filedes[i].revents |= POLLRDBAND;
  204.         }
  205.         fdHasEvent = PR_TRUE;
  206.         }
  207.         if (fdHasEvent) {
  208.         rv++;
  209.             }
  210.         }
  211.     PR_ASSERT(rv > 0);
  212.     } else if (rv == -1 && errno == EBADF) {
  213.     rv = 0;
  214.         for (i = 0; i < nfds; i++) {
  215.         filedes[i].revents = 0;
  216.             if (filedes[i].fd < 0) {
  217.                 continue;
  218.             }
  219.         if (fcntl(filedes[i].fd, F_GETFL, 0) == -1) {
  220.         filedes[i].revents = POLLNVAL;
  221.         rv++;
  222.         }
  223.         }
  224.     PR_ASSERT(rv > 0);
  225.     }
  226.     PR_ASSERT(-1 != timeout || rv != 0);
  227.  
  228.     return rv;
  229. }
  230. #endif /* _PR_NEED_FAKE_POLL */
  231.