~brianaker/libdrizzle/autoreconf-zlib-fixes

« back to all changes in this revision

Viewing changes to libdrizzle/poll.cc

  • Committer: Continuous Integration
  • Date: 2013-01-05 16:12:13 UTC
  • mfrom: (80.1.1 trunk-5.01)
  • Revision ID: ci@drizzle.org-20130105161213-8r8p1yaq878a3gap
Tags: 5.1.1
Merge lp:~brianaker/libdrizzle/windows-support-via-mingw Build: jenkins-Libdrizzle-34

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibMemcached
 
2
 * Copyright (C) 2010 Brian Aker, Trond Norbye
 
3
 * All rights reserved.
 
4
 *
 
5
 * Use and distribution licensed under the BSD license.  See
 
6
 * the COPYING file in the parent directory for full text.
 
7
 *
 
8
 * Summary: Implementation of poll by using select
 
9
 *
 
10
 */
 
11
 
 
12
#include "config.h"
 
13
 
 
14
#include "libdrizzle/common.h"
 
15
 
 
16
#if defined(WIN32) || defined(__MINGW32__)
 
17
#include "libdrizzle/poll.h"
 
18
 
 
19
#include <sys/time.h>
 
20
#include <strings.h>
 
21
 
 
22
int poll(struct pollfd fds[], nfds_t nfds, int tmo)
 
23
{
 
24
  fd_set readfds, writefds, errorfds;
 
25
  FD_ZERO(&readfds);
 
26
  FD_ZERO(&writefds);
 
27
  FD_ZERO(&errorfds);
 
28
 
 
29
  int maxfd= 0;
 
30
 
 
31
  for (nfds_t x= 0; x < nfds; ++x)
 
32
  {
 
33
    if (fds[x].events & (POLLIN | POLLOUT))
 
34
    {
 
35
#ifndef WIN32
 
36
      if (fds[x].fd > maxfd)
 
37
      {
 
38
        maxfd= fds[x].fd;
 
39
      }
 
40
#endif
 
41
      if (fds[x].events & POLLIN)
 
42
      {
 
43
        FD_SET(fds[x].fd, &readfds);
 
44
      }
 
45
      if (fds[x].events & POLLOUT)
 
46
      {
 
47
        FD_SET(fds[x].fd, &writefds);
 
48
      }
 
49
    }
 
50
  }
 
51
 
 
52
  struct timeval timeout= { .tv_sec = tmo / 1000,
 
53
                            .tv_usec= (tmo % 1000) * 1000 };
 
54
  struct timeval *tp= &timeout;
 
55
  if (tmo == -1)
 
56
  {
 
57
    tp= NULL;
 
58
  }
 
59
  int ret= select(maxfd + 1, &readfds, &writefds, &errorfds, tp);
 
60
  if (ret <= 0)
 
61
  {
 
62
    return ret;
 
63
  }
 
64
 
 
65
  /* Iterate through all of them because I need to clear the revent map */
 
66
  for (nfds_t x= 0; x < nfds; ++x)
 
67
  {
 
68
    fds[x].revents= 0;
 
69
    if (FD_ISSET(fds[x].fd, &readfds))
 
70
    {
 
71
      fds[x].revents |= POLLIN;
 
72
    }
 
73
    if (FD_ISSET(fds[x].fd, &writefds))
 
74
    {
 
75
      fds[x].revents |= POLLOUT;
 
76
    }
 
77
    if (FD_ISSET(fds[x].fd, &errorfds))
 
78
    {
 
79
      fds[x].revents |= POLLERR;
 
80
    }
 
81
  }
 
82
 
 
83
   return ret;
 
84
}
 
85
 
 
86
#endif // defined(WIN32) || defined(__MINGW32__)