~ubuntu-branches/ubuntu/lucid/jack-audio-connection-kit/lucid

« back to all changes in this revision

Viewing changes to config/os/macosx/poll.h

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-12-06 11:05:15 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081206110515-xa9v9pajr9jqvfvg
Tags: 0.115.6-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - Redirect stderr in bash completion (Debian #504488).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// fakepoll.h
 
3
// poll using select
 
4
// Warning: a call to this poll() takes about 4K of stack space.
 
5
 
 
6
// Greg Parker     gparker-web@sealiesoftware.com     December 2000
 
7
// This code is in the public domain and may be copied or modified without 
 
8
// permission. 
 
9
 
 
10
// Updated May 2002: 
 
11
// * fix crash when an fd is less than 0
 
12
// * set errno=EINVAL if an fd is greater or equal to FD_SETSIZE
 
13
// * don't set POLLIN or POLLOUT in revents if it wasn't requested 
 
14
//   in events (only happens when an fd is in the poll set twice)
 
15
 
 
16
#ifndef _FAKE_POLL_H
 
17
#define _FAKE_POLL_H
 
18
 
 
19
#include <limits.h>
 
20
#include <sys/types.h>
 
21
#include <sys/time.h>
 
22
#include <unistd.h>
 
23
#include <stdlib.h>
 
24
#include <errno.h>
 
25
 
 
26
typedef struct pollfd {
 
27
    int fd;                         /* file desc to poll */
 
28
    short events;                   /* events of interest on fd */
 
29
    short revents;                  /* events that occurred on fd */
 
30
} pollfd_t;
 
31
 
 
32
 
 
33
// poll flags
 
34
#define POLLIN  0x0001
 
35
#define POLLOUT 0x0004
 
36
#define POLLERR 0x0008
 
37
 
 
38
// synonyms
 
39
#define POLLNORM POLLIN
 
40
#define POLLPRI POLLIN
 
41
#define POLLRDNORM POLLIN
 
42
#define POLLRDBAND POLLIN
 
43
#define POLLWRNORM POLLOUT
 
44
#define POLLWRBAND POLLOUT
 
45
 
 
46
// ignored
 
47
#define POLLHUP 0x0010
 
48
#define POLLNVAL 0x0020
 
49
 
 
50
static inline int 
 
51
poll(struct pollfd *pollSet, int pollCount, int pollTimeout)
 
52
{
 
53
    struct timeval tv;
 
54
    struct timeval *tvp;
 
55
    fd_set readFDs, writeFDs, exceptFDs;
 
56
    fd_set *readp, *writep, *exceptp;
 
57
    struct pollfd *pollEnd, *p;
 
58
    int selected;
 
59
    int result;
 
60
    int maxFD;
 
61
 
 
62
    if (!pollSet) {
 
63
        pollEnd = NULL;
 
64
        readp = NULL;
 
65
        writep = NULL;
 
66
        exceptp = NULL;
 
67
        maxFD = 0;
 
68
    } 
 
69
    else {
 
70
        pollEnd = pollSet + pollCount;
 
71
        readp = &readFDs;
 
72
        writep = &writeFDs;
 
73
        exceptp = &exceptFDs;
 
74
 
 
75
        FD_ZERO(readp);
 
76
        FD_ZERO(writep);
 
77
        FD_ZERO(exceptp);
 
78
        
 
79
        // Find the biggest fd in the poll set
 
80
        maxFD = 0;
 
81
        for (p = pollSet; p < pollEnd; p++) {
 
82
            if (p->fd > maxFD) maxFD = p->fd;
 
83
        }
 
84
 
 
85
        if (maxFD >= FD_SETSIZE) {
 
86
            // At least one fd is too big
 
87
            errno = EINVAL;
 
88
            return -1;
 
89
        }
 
90
        
 
91
        // Transcribe flags from the poll set to the fd sets
 
92
        for (p = pollSet; p < pollEnd; p++) {
 
93
            if (p->fd < 0) {
 
94
                // Negative fd checks nothing and always reports zero
 
95
            } else {
 
96
                if (p->events & POLLIN)  FD_SET(p->fd, readp);
 
97
                if (p->events & POLLOUT) FD_SET(p->fd, writep);
 
98
                if (p->events != 0)     {
 
99
                    FD_SET(p->fd, exceptp);
 
100
                }
 
101
                // POLLERR is never set coming in; poll() always reports errors
 
102
                // But don't report if we're not listening to anything at all.
 
103
            }
 
104
        }
 
105
    }
 
106
        
 
107
    // poll timeout is in milliseconds. Convert to struct timeval.
 
108
    // poll timeout == -1 : wait forever : select timeout of NULL
 
109
    // poll timeout == 0  : return immediately : select timeout of zero
 
110
    if (pollTimeout >= 0) {
 
111
        tv.tv_sec = pollTimeout / 1000;
 
112
        tv.tv_usec = (pollTimeout % 1000) * 1000;
 
113
        tvp = &tv;
 
114
    } else {
 
115
        tvp = NULL;
 
116
    }
 
117
    
 
118
    selected = select(maxFD+1, readp, writep, exceptp, tvp);
 
119
 
 
120
    if (selected < 0) {
 
121
        // Error during select
 
122
        result = -1;
 
123
    } 
 
124
    else if (selected > 0) {
 
125
        // Select found something
 
126
        // Transcribe result from fd sets to poll set.
 
127
        // Also count the number of selected fds. poll returns the 
 
128
        // number of ready fds; select returns the number of bits set.
 
129
        int polled = 0;
 
130
        for (p = pollSet; p < pollEnd; p++) {
 
131
            p->revents = 0;
 
132
            if (p->fd < 0) {
 
133
                // Negative fd always reports zero
 
134
            } else {
 
135
                if ((p->events & POLLIN)  &&  FD_ISSET(p->fd, readp)) {
 
136
                    p->revents |= POLLIN;
 
137
                }
 
138
                if ((p->events & POLLOUT)  &&  FD_ISSET(p->fd, writep)) {
 
139
                    p->revents |= POLLOUT;
 
140
                }
 
141
                if ((p->events != 0)  &&  FD_ISSET(p->fd, exceptp)) {
 
142
                     p->revents |= POLLERR;
 
143
                }
 
144
 
 
145
                if (p->revents) polled++;
 
146
            }
 
147
        }
 
148
        result = polled;
 
149
    }
 
150
    else {
 
151
        // selected == 0, select timed out before anything happened
 
152
        // Clear all result bits and return zero.
 
153
        for (p = pollSet; p < pollEnd; p++) {
 
154
            p->revents = 0;
 
155
        }
 
156
        result = 0;
 
157
    }
 
158
 
 
159
    return result;
 
160
}
 
161
 
 
162
#endif