]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/select.c
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Portions copyright (c) 1999, 2000
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley, Intel Corporation, and its contributors.
31 * 4. Neither the name of University, Intel Corporation, or their respective
32 * contributors may be used to endorse or promote products derived from
33 * this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
36 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
37 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
38 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
39 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
48 * $Id: select.c,v 1.1.1.1 2003/11/19 01:50:30 kyu3 Exp $
50 #include <Library/UefiBootServicesTableLib.h>
52 #include <LibConfig.h>
58 #include <sys/param.h>
68 #ifdef EFI_NT_EMULATOR
69 #define _SELECT_DELAY_ 10000
71 #define _SELECT_DELAY_ 1000
74 #define MAX_SLEEP_DELAY 0xfffffffe
76 /** Sleep for the specified number of Microseconds.
78 Implements the usleep(3) function.
80 @param[in] Microseconds Number of microseconds to sleep.
82 @retval 0 Always returns zero.
85 usleep( useconds_t Microseconds
)
87 while ( MAX_SLEEP_DELAY
< Microseconds
) {
88 gBS
->Stall ( MAX_SLEEP_DELAY
);
89 Microseconds
-= MAX_SLEEP_DELAY
;
91 gBS
->Stall((UINTN
)Microseconds
);
96 sleep( unsigned int Seconds
)
98 return (usleep( (useconds_t
)(Seconds
* 1000000) ));
117 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
118 static int16_t flag
[3] = { POLLRDNORM
, POLLWRNORM
, POLLRDBAND
};
120 for (msk
= 0, n
= 0; msk
< 3; msk
++) {
121 if (ibits
[msk
] == NULL
)
123 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
124 bits
= ibits
[ msk
][ i
/ NFDBITS
];
125 while (( 0 != (j
= ffs(bits
))) && ((fd
= i
+ --j
) < nfd
)) {
129 pfd
.events
= flag
[msk
];
131 FdCount
= poll ( &pfd
, 1, 0 );
132 if ( -1 == FdCount
) {
135 if ( 0 != FdCount
) {
136 obits
[msk
][(fd
)/NFDBITS
] |=
137 (1 << ((fd
) % NFDBITS
));
157 fd_mask
*ibits
[3], *obits
[3], *selbits
, *sbp
;
158 int error
, forever
, nselected
;
159 u_int nbufbytes
, ncpbytes
, nfdbits
;
166 * Allocate just enough bits for the non-null fd_sets. Use the
167 * preallocated auto buffer if possible.
169 nfdbits
= roundup(nd
, NFDBITS
);
170 ncpbytes
= nfdbits
/ NBBY
;
173 nbufbytes
+= 2 * ncpbytes
;
175 nbufbytes
+= 2 * ncpbytes
;
177 nbufbytes
+= 2 * ncpbytes
;
178 selbits
= malloc(nbufbytes
);
181 * Assign pointers into the bit buffers and fetch the input bits.
182 * Put the output buffers together so that they can be bzeroed
186 #define getbits(name, x) \
191 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
193 sbp += ncpbytes / sizeof *sbp; \
194 bcopy(name, ibits[x], ncpbytes); \
202 memset(selbits
, 0, nbufbytes
/ 2);
205 timo
= tv
->tv_usec
+ (tv
->tv_sec
* 1000000);
213 * Poll for I/O events
218 * Scan for pending I/O
220 error
= selscan(ibits
, obits
, nd
, &nselected
);
221 if (error
|| nselected
)
225 * Adjust timeout is needed
231 usleep( _SELECT_DELAY_
);
232 timo
-= _SELECT_DELAY_
;
235 } while (timo
> 0 || forever
);
237 /* select is not restarted after signals... */
238 if (error
== ERESTART
)
240 else if (error
== EWOULDBLOCK
)
243 #define putbits(name, x) if (name) bcopy(obits[x], name, ncpbytes)
255 return ( nselected
);