~ubuntu-branches/ubuntu/gutsy/virtualbox-ose/gutsy

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/nsprpub/pr/src/md/os2/os2poll.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-09-08 16:44:58 UTC
  • Revision ID: james.westby@ubuntu.com-20070908164458-wao29470vqtr8ksy
Tags: upstream-1.5.0-dfsg2
ImportĀ upstreamĀ versionĀ 1.5.0-dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998-2000
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/*
 
39
 * This file implements _PR_MD_PR_POLL for OS/2.
 
40
 */
 
41
 
 
42
#ifdef XP_OS2_EMX
 
43
 #include <sys/time.h> /* For timeval. */
 
44
#endif
 
45
 
 
46
#include "primpl.h"
 
47
 
 
48
#ifndef BSD_SELECT
 
49
/* Utility functions called when using OS/2 select */
 
50
 
 
51
PRBool IsSocketSet( PRInt32 osfd, int* socks, int start, int count )
 
52
{
 
53
  int i;
 
54
  PRBool isSet = PR_FALSE;
 
55
 
 
56
  for( i = start; i < start+count; i++ )
 
57
  {
 
58
    if( socks[i] == osfd )
 
59
      isSet = PR_TRUE;
 
60
  }
 
61
  
 
62
  return isSet; 
 
63
}
 
64
#endif
 
65
 
 
66
PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
 
67
{
 
68
#ifdef BSD_SELECT
 
69
    fd_set rd, wt, ex;
 
70
#else
 
71
    int rd, wt, ex;
 
72
    int* socks;
 
73
    unsigned long msecs;
 
74
    int i, j;
 
75
#endif
 
76
    PRFileDesc *bottom;
 
77
    PRPollDesc *pd, *epd;
 
78
    PRInt32 maxfd = -1, ready, err;
 
79
    PRIntervalTime remaining, elapsed, start;
 
80
 
 
81
#ifdef BSD_SELECT
 
82
    struct timeval tv, *tvp = NULL;
 
83
 
 
84
    FD_ZERO(&rd);
 
85
    FD_ZERO(&wt);
 
86
    FD_ZERO(&ex);
 
87
#else
 
88
    rd = 0;
 
89
    wt = 0;
 
90
    ex = 0;
 
91
    socks = (int) PR_MALLOC( npds * 3 * sizeof(int) );
 
92
    
 
93
    if (!socks)
 
94
    {
 
95
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
 
96
        return -1;
 
97
    }
 
98
#endif
 
99
 
 
100
    ready = 0;
 
101
    for (pd = pds, epd = pd + npds; pd < epd; pd++)
 
102
    {
 
103
        PRInt16 in_flags_read = 0, in_flags_write = 0;
 
104
        PRInt16 out_flags_read = 0, out_flags_write = 0;
 
105
 
 
106
        if ((NULL != pd->fd) && (0 != pd->in_flags))
 
107
        {
 
108
            if (pd->in_flags & PR_POLL_READ)
 
109
            {
 
110
                in_flags_read = (pd->fd->methods->poll)(
 
111
                    pd->fd, pd->in_flags & ~PR_POLL_WRITE, &out_flags_read);
 
112
            }
 
113
            if (pd->in_flags & PR_POLL_WRITE)
 
114
            {
 
115
                in_flags_write = (pd->fd->methods->poll)(
 
116
                    pd->fd, pd->in_flags & ~PR_POLL_READ, &out_flags_write);
 
117
            }
 
118
            if ((0 != (in_flags_read & out_flags_read)) ||
 
119
                (0 != (in_flags_write & out_flags_write)))
 
120
            {
 
121
                /* this one's ready right now */
 
122
                if (0 == ready)
 
123
                {
 
124
                    /*
 
125
                     * We will have to return without calling the
 
126
                     * system poll/select function.  So zero the
 
127
                     * out_flags fields of all the poll descriptors
 
128
                     * before this one.
 
129
                     */
 
130
                    PRPollDesc *prev;
 
131
                    for (prev = pds; prev < pd; prev++)
 
132
                    {
 
133
                        prev->out_flags = 0;
 
134
                    }
 
135
                }
 
136
                ready += 1;
 
137
                pd->out_flags = out_flags_read | out_flags_write;
 
138
            }
 
139
            else
 
140
            {
 
141
                pd->out_flags = 0;  /* pre-condition */
 
142
 
 
143
                /* make sure this is an NSPR supported stack */
 
144
                bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
 
145
                PR_ASSERT(NULL != bottom);  /* what to do about that? */
 
146
                if ((NULL != bottom) &&
 
147
                    (_PR_FILEDESC_OPEN == bottom->secret->state))
 
148
                {
 
149
                    if (0 == ready)
 
150
                    {
 
151
                        PRInt32 osfd = bottom->secret->md.osfd;
 
152
                        if (osfd > maxfd) 
 
153
                            maxfd = osfd;
 
154
                        if (in_flags_read & PR_POLL_READ)
 
155
                        {
 
156
                            pd->out_flags |= _PR_POLL_READ_SYS_READ;
 
157
#ifdef BSD_SELECT
 
158
                            FD_SET(osfd, &rd);
 
159
#else
 
160
                            socks[rd] = osfd;
 
161
                            rd++;              
 
162
#endif
 
163
                        }
 
164
                        if (in_flags_read & PR_POLL_WRITE)
 
165
                        {
 
166
                            pd->out_flags |= _PR_POLL_READ_SYS_WRITE;
 
167
#ifdef BSD_SELECT
 
168
                            FD_SET(osfd, &wt);
 
169
#else
 
170
                            socks[npds+wt] = osfd;
 
171
                            wt++;              
 
172
#endif
 
173
                        }
 
174
                        if (in_flags_write & PR_POLL_READ)
 
175
                        {
 
176
                            pd->out_flags |= _PR_POLL_WRITE_SYS_READ;
 
177
#ifdef BSD_SELECT
 
178
                            FD_SET(osfd, &rd);
 
179
#else
 
180
                            socks[rd] = osfd;
 
181
                            rd++;              
 
182
#endif
 
183
                        }
 
184
                        if (in_flags_write & PR_POLL_WRITE)
 
185
                        {
 
186
                            pd->out_flags |= _PR_POLL_WRITE_SYS_WRITE;
 
187
#ifdef BSD_SELECT
 
188
                            FD_SET(osfd, &wt);
 
189
#else
 
190
                            socks[npds+wt] = osfd;
 
191
                            wt++;              
 
192
#endif
 
193
                        }
 
194
                        if (pd->in_flags & PR_POLL_EXCEPT)
 
195
                        {
 
196
#ifdef BSD_SELECT
 
197
                            FD_SET(osfd, &ex);
 
198
#else
 
199
                            socks[npds*2+ex] = osfd;
 
200
                            ex++;
 
201
#endif
 
202
                        }
 
203
                    }
 
204
                }
 
205
                else
 
206
                {
 
207
                    if (0 == ready)
 
208
                    {
 
209
                        PRPollDesc *prev;
 
210
                        for (prev = pds; prev < pd; prev++)
 
211
                        {
 
212
                            prev->out_flags = 0;
 
213
                        }
 
214
                    }
 
215
                    ready += 1;  /* this will cause an abrupt return */
 
216
                    pd->out_flags = PR_POLL_NVAL;  /* bogii */
 
217
                }
 
218
            }
 
219
        }
 
220
        else
 
221
        {
 
222
            pd->out_flags = 0;
 
223
        }
 
224
    }
 
225
 
 
226
    if (0 != ready)
 
227
    {
 
228
#ifndef BSD_SELECT
 
229
        PR_Free(socks);
 
230
#endif
 
231
        return ready;  /* no need to block */
 
232
    }
 
233
 
 
234
    remaining = timeout;
 
235
    start = PR_IntervalNow();
 
236
 
 
237
retry:
 
238
#ifdef BSD_SELECT
 
239
    if (timeout != PR_INTERVAL_NO_TIMEOUT)
 
240
    {
 
241
        PRInt32 ticksPerSecond = PR_TicksPerSecond();
 
242
        tv.tv_sec = remaining / ticksPerSecond;
 
243
        tv.tv_usec = PR_IntervalToMicroseconds( remaining % ticksPerSecond );
 
244
        tvp = &tv;
 
245
    }
 
246
 
 
247
    ready = bsdselect(maxfd + 1, &rd, &wt, &ex, tvp);
 
248
#else
 
249
    switch (timeout)
 
250
    {
 
251
        case PR_INTERVAL_NO_WAIT:
 
252
            msecs = 0;
 
253
            break;
 
254
        case PR_INTERVAL_NO_TIMEOUT:
 
255
            msecs = -1;
 
256
            break;
 
257
        default:
 
258
            msecs = PR_IntervalToMilliseconds(remaining);
 
259
    }
 
260
 
 
261
     /* compact array */
 
262
    for( i = rd, j = npds; j < npds+wt; i++,j++ )
 
263
        socks[i] = socks[j];
 
264
    for( i = rd+wt, j = npds*2; j < npds*2+ex; i++,j++ )
 
265
        socks[i] = socks[j];
 
266
    
 
267
    ready = os2_select(socks, rd, wt, ex, msecs);
 
268
#endif
 
269
 
 
270
    if (ready == -1 && errno == EINTR)
 
271
    {
 
272
        if (timeout == PR_INTERVAL_NO_TIMEOUT)
 
273
            goto retry;
 
274
        else
 
275
        {
 
276
            elapsed = (PRIntervalTime) (PR_IntervalNow() - start);
 
277
            if (elapsed > timeout)
 
278
                ready = 0;  /* timed out */
 
279
            else
 
280
            {
 
281
                remaining = timeout - elapsed;
 
282
                goto retry;
 
283
            }
 
284
        }
 
285
    }
 
286
 
 
287
    /*
 
288
    ** Now to unravel the select sets back into the client's poll
 
289
    ** descriptor list. Is this possibly an area for pissing away
 
290
    ** a few cycles or what?
 
291
    */
 
292
    if (ready > 0)
 
293
    {
 
294
        ready = 0;
 
295
        for (pd = pds, epd = pd + npds; pd < epd; pd++)
 
296
        {
 
297
            PRInt16 out_flags = 0;
 
298
            if ((NULL != pd->fd) && (0 != pd->in_flags))
 
299
            {
 
300
                PRInt32 osfd;
 
301
                bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
 
302
                PR_ASSERT(NULL != bottom);
 
303
 
 
304
                osfd = bottom->secret->md.osfd;
 
305
 
 
306
#ifdef BSD_SELECT
 
307
                if (FD_ISSET(osfd, &rd))
 
308
#else
 
309
                if( IsSocketSet(osfd, socks, 0, rd) )        
 
310
#endif
 
311
                {
 
312
                    if (pd->out_flags & _PR_POLL_READ_SYS_READ)
 
313
                        out_flags |= PR_POLL_READ;
 
314
                    if (pd->out_flags & _PR_POLL_WRITE_SYS_READ)
 
315
                        out_flags |= PR_POLL_WRITE;
 
316
                } 
 
317
 
 
318
#ifdef BSD_SELECT
 
319
                if (FD_ISSET(osfd, &wt))
 
320
#else
 
321
                if( IsSocketSet(osfd, socks, rd, wt) )        
 
322
#endif
 
323
                {
 
324
                    if (pd->out_flags & _PR_POLL_READ_SYS_WRITE)
 
325
                        out_flags |= PR_POLL_READ;
 
326
                    if (pd->out_flags & _PR_POLL_WRITE_SYS_WRITE)
 
327
                        out_flags |= PR_POLL_WRITE;
 
328
                } 
 
329
 
 
330
#ifdef BSD_SELECT
 
331
                if (FD_ISSET(osfd, &ex))
 
332
#else
 
333
                if( IsSocketSet(osfd, socks, rd+wt, ex) )        
 
334
#endif
 
335
                {
 
336
                    out_flags |= PR_POLL_EXCEPT;
 
337
                }
 
338
            }
 
339
            pd->out_flags = out_flags;
 
340
            if (out_flags) ready++;
 
341
        }
 
342
        PR_ASSERT(ready > 0);
 
343
    }
 
344
    else if (ready < 0)
 
345
    {
 
346
        err = _MD_ERRNO();
 
347
        if (err == EBADF)
 
348
        {
 
349
            /* Find the bad fds */
 
350
            int optval;
 
351
            int optlen = sizeof(optval);
 
352
            ready = 0;
 
353
            for (pd = pds, epd = pd + npds; pd < epd; pd++)
 
354
            {
 
355
                pd->out_flags = 0;
 
356
                if ((NULL != pd->fd) && (0 != pd->in_flags))
 
357
                {
 
358
                    bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
 
359
                    if (getsockopt(bottom->secret->md.osfd, SOL_SOCKET,
 
360
                        SO_TYPE, (char *) &optval, &optlen) == -1)
 
361
                    {
 
362
                        PR_ASSERT(sock_errno() == ENOTSOCK);
 
363
                        if (sock_errno() == ENOTSOCK)
 
364
                        {
 
365
                            pd->out_flags = PR_POLL_NVAL;
 
366
                            ready++;
 
367
                        }
 
368
                    }
 
369
                }
 
370
            }
 
371
            PR_ASSERT(ready > 0);
 
372
        }
 
373
        else
 
374
            _PR_MD_MAP_SELECT_ERROR(err);
 
375
    }
 
376
 
 
377
#ifndef BSD_SELECT
 
378
    PR_Free(socks);
 
379
#endif
 
380
    return ready;
 
381
}
 
382