select.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:8k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* ==== select.c ============================================================
  2.  * Copyright (c) 1994 by Chris Provenzano, proven@mit.edu
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *  This product includes software developed by Chris Provenzano.
  16.  * 4. The name of Chris Provenzano may not be used to endorse or promote 
  17.  *   products derived from this software without specific prior written
  18.  *   permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY 
  24.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  26.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  30.  * SUCH DAMAGE.
  31.  *
  32.  * This code based on code contributed by 
  33.  * Peter Hofmann <peterh@prz.tu-berlin.d400.de>
  34.  *
  35.  * Description : Select.
  36.  *
  37.  *  1.23 94/04/26 proven
  38.  *      -Started coding this file.
  39.  */
  40. #ifndef lint
  41. static const char rcsid[] = "$Id$";
  42. #endif
  43. #include <string.h>
  44. #include <errno.h>
  45. #include <pthread.h>
  46. #include <sys/types.h>
  47. #include <sys/time.h>
  48. extern struct pthread_queue fd_wait_select;
  49. static struct timeval zero_timeout = { 0, 0 }; /* Moved by monty */
  50. /* ==========================================================================
  51.  * select() 
  52.  */
  53. int select(int numfds, fd_set *readfds, fd_set *writefds,
  54.            fd_set *exceptfds, struct timeval *timeout)
  55. {
  56.   fd_set real_exceptfds, real_readfds, real_writefds; /* mapped fd_sets */
  57.   fd_set * real_readfds_p, * real_writefds_p, * real_exceptfds_p;
  58.   fd_set read_locks, write_locks, rdwr_locks;
  59.   struct timespec timeout_time, current_time;
  60.   int i, j, ret = 0, got_all_locks = 1;
  61.   struct pthread_select_data data;
  62.   if (numfds > dtablesize) {
  63.     numfds = dtablesize;
  64.   }
  65.   data.nfds = 0;
  66.   FD_ZERO(&data.readfds);
  67.   FD_ZERO(&data.writefds);
  68.   FD_ZERO(&data.exceptfds);
  69.   /* Do this first */
  70.   if (timeout) {
  71.     machdep_gettimeofday(&current_time);
  72.     timeout_time.tv_sec = current_time.tv_sec + timeout->tv_sec;
  73.     if ((timeout_time.tv_nsec = current_time.tv_nsec + 
  74.  (timeout->tv_usec * 1000)) > 1000000000) {
  75.       timeout_time.tv_nsec -= 1000000000;
  76.       timeout_time.tv_sec++;
  77.     }
  78.   }
  79.   FD_ZERO(&read_locks);
  80.   FD_ZERO(&write_locks);
  81.   FD_ZERO(&rdwr_locks);
  82.   FD_ZERO(&real_readfds);
  83.   FD_ZERO(&real_writefds);
  84.   FD_ZERO(&real_exceptfds);
  85.   /* lock readfds */
  86.   if (readfds || writefds || exceptfds) {
  87.     for (i = 0; i < numfds; i++) {
  88.       if ((readfds && (FD_ISSET(i, readfds))) || 
  89.   (exceptfds && FD_ISSET(i, exceptfds))) {
  90. if (writefds && FD_ISSET(i ,writefds)) {
  91.   if ((ret = fd_lock(i, FD_RDWR, NULL)) != OK) {
  92.     got_all_locks = 0;
  93.     break;
  94.   }
  95.   FD_SET(i, &rdwr_locks);
  96.   FD_SET(fd_table[i]->fd.i,&real_writefds);
  97. } else {
  98.   if ((ret = fd_lock(i, FD_READ, NULL)) != OK) {
  99.     got_all_locks = 0;
  100.     break;
  101.   }
  102.   FD_SET(i, &read_locks);
  103. }
  104. if (readfds && FD_ISSET(i,readfds)) {
  105.   FD_SET(fd_table[i]->fd.i, &real_readfds);
  106. }
  107. if (exceptfds && FD_ISSET(i,exceptfds)) {
  108.   FD_SET(fd_table[i]->fd.i, &real_exceptfds);
  109. }
  110. if (fd_table[i]->fd.i >= data.nfds) {
  111.   data.nfds = fd_table[i]->fd.i + 1;
  112. }
  113.       } else {
  114. if (writefds && FD_ISSET(i, writefds)) {
  115.   if ((ret = fd_lock(i, FD_WRITE, NULL)) != OK) {
  116.     got_all_locks = 0;
  117.     break;
  118.   }
  119.   FD_SET(i, &write_locks);
  120.   FD_SET(fd_table[i]->fd.i,&real_writefds);
  121.   if (fd_table[i]->fd.i >= data.nfds) {
  122.     data.nfds = fd_table[i]->fd.i + 1;
  123.   }
  124. }
  125.       }
  126.     }
  127.   }
  128.   if (got_all_locks)
  129.   {
  130.     memcpy(&data.readfds,&real_readfds,sizeof(fd_set));
  131.     memcpy(&data.writefds,&real_writefds,sizeof(fd_set));
  132.     memcpy(&data.exceptfds,&real_exceptfds,sizeof(fd_set));
  133.     real_readfds_p = (readfds == NULL) ? NULL : &real_readfds;
  134.     real_writefds_p = (writefds == NULL) ? NULL : &real_writefds;
  135.     real_exceptfds_p = (exceptfds == NULL) ? NULL : &real_exceptfds;
  136.     pthread_run->sighandled=0;
  137.     if ((ret = machdep_sys_select(data.nfds, real_readfds_p,
  138.   real_writefds_p, real_exceptfds_p,
  139.   &zero_timeout)) == OK) {
  140.       pthread_sched_prevent();
  141.       real_exceptfds_p = (exceptfds == NULL) ? NULL : &data.exceptfds;
  142.       real_writefds_p = (writefds == NULL) ? NULL : &data.writefds;
  143.       real_readfds_p = (readfds == NULL) ? NULL : &data.readfds;
  144.       pthread_queue_enq(&fd_wait_select, pthread_run);
  145.       pthread_run->data.select_data = &data;
  146.       SET_PF_WAIT_EVENT(pthread_run);
  147.       if (timeout) {
  148. machdep_gettimeofday(&current_time);
  149. sleep_schedule(&current_time, &timeout_time);
  150. SET_PF_AT_CANCEL_POINT(pthread_run);
  151. pthread_resched_resume(PS_SELECT_WAIT);
  152. CLEAR_PF_AT_CANCEL_POINT(pthread_run);
  153. /* We're awake */
  154. if (sleep_cancel(pthread_run) == NOTOK) {
  155.   ret = OK;
  156. }
  157. else
  158. {
  159.   int count = 0;
  160.   for (i = 0; i < numfds; i++)
  161.   {
  162.     if (real_readfds_p && (FD_ISSET(i, real_readfds_p)))
  163.       count++;
  164.   if (real_writefds_p && (FD_ISSET(i, real_writefds_p)))
  165.     count++;
  166.   if (real_exceptfds_p && (FD_ISSET(i, real_exceptfds_p)))
  167.     count++;
  168.   }
  169.   ret = count;
  170. }
  171.       /* Moving this after the sleep_cancel() seemed
  172.        * to fix intermittent crashes during heavy
  173.        * socket use. (mevans)
  174.        */
  175.       CLEAR_PF_DONE_EVENT(pthread_run);
  176.     } else {
  177.       int count = 0;
  178.       SET_PF_AT_CANCEL_POINT(pthread_run);
  179.       pthread_resched_resume(PS_SELECT_WAIT);
  180.       CLEAR_PF_AT_CANCEL_POINT(pthread_run);
  181.       CLEAR_PF_DONE_EVENT(pthread_run);
  182.       for (i = 0; i < numfds; i++)
  183.       {
  184. if (real_readfds_p && (FD_ISSET(i, real_readfds_p)))
  185.   count++;
  186. if (real_writefds_p && (FD_ISSET(i, real_writefds_p)))
  187.   count++;
  188. if (real_exceptfds_p && (FD_ISSET(i, real_exceptfds_p)))
  189.   count++;
  190.       }
  191.       ret = count;
  192.     }
  193.     if (pthread_run->sighandled) /* Added by monty */
  194.     { /* We where aborted */
  195.       ret= NOTOK;
  196.       SET_ERRNO(EINTR);
  197.     }
  198.   } else if (ret < 0) {
  199.       SET_ERRNO(-ret);
  200.       ret = NOTOK;
  201.     }
  202.   }
  203.   /* clean up the locks */
  204.   for (i = 0; i < numfds; i++)
  205.   { /* Changed by monty */
  206.     if (FD_ISSET(i,&read_locks)) fd_unlock(i,FD_READ);
  207.     if (FD_ISSET(i,&rdwr_locks)) fd_unlock(i,FD_RDWR);
  208.     if (FD_ISSET(i,&write_locks)) fd_unlock(i,FD_WRITE);
  209.   }
  210.   if (ret > 0) {
  211.     if (readfds != NULL) {
  212.       for (i = 0; i < numfds; i++) {
  213. if (! (FD_ISSET(i,readfds) &&
  214.        FD_ISSET(fd_table[i]->fd.i,real_readfds_p)))
  215.   FD_CLR(i,readfds);
  216.       }
  217.     }
  218.     if (writefds != NULL) {
  219.       for (i = 0; i < numfds; i++)
  220. if (! (FD_ISSET(i,writefds) &&
  221.        FD_ISSET(fd_table[i]->fd.i,real_writefds_p)))
  222.   FD_CLR(i,writefds);
  223.     }
  224.     if (exceptfds != NULL) {
  225.       for (i = 0; i < numfds; i++)
  226. if (! (FD_ISSET(i,exceptfds) &&
  227.        FD_ISSET(fd_table[i]->fd.i,real_exceptfds_p)))
  228.   FD_CLR(i,exceptfds);
  229.     }
  230.   } else {
  231.     if (exceptfds != NULL) FD_ZERO(exceptfds);
  232.     if (writefds != NULL) FD_ZERO(writefds);
  233.     if (readfds != NULL) FD_ZERO(readfds);
  234.   }
  235.   return(ret);
  236. }