MySQL判断是否有数据可读源码

/**

  Indicate whether there is data to read on a given socket.

 

  @note An exceptional condition event and/or errors are

        interpreted as if there is data to read.

 

  @param sd       A connected socket.

  @param timeout  Maximum time in seconds to poll.

 

  @retval FALSE   There is data to read.

  @retval TRUE    There is no data to read.

*/

 

static my_bool socket_poll_read(my_socket sd, uint timeout)

{

#ifdef __WIN__

  int res;

  my_socket fd= sd;

  fd_set readfds, errorfds;

  struct timeval tm;

  DBUG_ENTER("socket_poll_read");

  tm.tv_sec= timeout;

  tm.tv_usec= 0;

  FD_ZERO(&readfds);

  FD_ZERO(&errorfds);

  FD_SET(fd, &readfds);

  FD_SET(fd, &errorfds);

  /* The first argument is ignored on Windows, so a conversion to int is OK */

  if ((res= select((int) fd, &readfds, NULL, &errorfds, &tm) <= 0))//等待可读或错误事件

  {

    DBUG_RETURN(res < 0 ? 0 : 1);

  }

  res= FD_ISSET(fd, &readfds) || FD_ISSET(fd, &errorfds);//是否有可读或错误事件

  DBUG_RETURN(!res);

#elif defined(HAVE_POLL)

  struct pollfd fds;

  int res;

  DBUG_ENTER("socket_poll_read");

  fds.fd=sd;

  fds.events=POLLIN;//可读事件

  fds.revents=0;

  if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)//等待可读事件

  {

    DBUG_RETURN(res < 0 ? 0 : 1); /* Don't return 1 on errors */

  }

  DBUG_RETURN(fds.revents & (POLLIN | POLLERR | POLLHUP) ? 0 : 1);

#else

  return 0;

#endif

}

 

都说使用select具有可移植性,但是在该函数中应该并没有使用其可移植的功能,而是对WINDOWS和LINUX分别做了处理的,其原因应该就是在LINUX下,select函数的效率是没有poll效率高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值