17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <pthread.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 #include <sys/uio.h>
35 #include <unistd.h>
36 #include <errno.h>
37
38 #include <sys/poll.h>
39
40 /*
41 * Stack allocated by thread when doing blocking operation
42 */
43 typedef struct threadEntry {
44 pthread_t thr; /* this thread */
45 struct threadEntry *next; /* next thread */
46 int intr; /* interrupted */
47 } threadEntry_t;
48
49 /*
50 * Heap allocated during initialized - one entry per fd
51 */
52 typedef struct {
53 pthread_mutex_t lock; /* fd lock */
54 threadEntry_t *threads; /* threads blocked on fd */
55 } fdEntry_t;
56
57 /*
58 * Signal to unblock thread
59 */
304 BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
305 }
306
307 #ifndef USE_SELECT
308 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
309 BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
310 }
311 #else
312 int NET_Select(int s, fd_set *readfds, fd_set *writefds,
313 fd_set *exceptfds, struct timeval *timeout) {
314 BLOCKING_IO_RETURN_INT( s-1,
315 select(s, readfds, writefds, exceptfds, timeout) );
316 }
317 #endif
318
319 /*
320 * Wrapper for poll(s, timeout).
321 * Auto restarts with adjusted timeout if interrupted by
322 * signal other than our wakeup signal.
323 */
324 int NET_Timeout(int s, long timeout) {
325 long prevtime = 0, newtime;
326 struct timeval t;
327 fdEntry_t *fdEntry = getFdEntry(s);
328
329 /*
330 * Check that fd hasn't been closed.
331 */
332 if (fdEntry == NULL) {
333 errno = EBADF;
334 return -1;
335 }
336
337 /*
338 * Pick up current time as may need to adjust timeout
339 */
340 if (timeout > 0) {
341 gettimeofday(&t, NULL);
342 prevtime = t.tv_sec * 1000 + t.tv_usec / 1000;
343 }
344
|
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <pthread.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 #include <sys/uio.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/poll.h>
38
39 #include "jni.h"
40
41 /*
42 * Stack allocated by thread when doing blocking operation
43 */
44 typedef struct threadEntry {
45 pthread_t thr; /* this thread */
46 struct threadEntry *next; /* next thread */
47 int intr; /* interrupted */
48 } threadEntry_t;
49
50 /*
51 * Heap allocated during initialized - one entry per fd
52 */
53 typedef struct {
54 pthread_mutex_t lock; /* fd lock */
55 threadEntry_t *threads; /* threads blocked on fd */
56 } fdEntry_t;
57
58 /*
59 * Signal to unblock thread
60 */
305 BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
306 }
307
308 #ifndef USE_SELECT
309 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
310 BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
311 }
312 #else
313 int NET_Select(int s, fd_set *readfds, fd_set *writefds,
314 fd_set *exceptfds, struct timeval *timeout) {
315 BLOCKING_IO_RETURN_INT( s-1,
316 select(s, readfds, writefds, exceptfds, timeout) );
317 }
318 #endif
319
320 /*
321 * Wrapper for poll(s, timeout).
322 * Auto restarts with adjusted timeout if interrupted by
323 * signal other than our wakeup signal.
324 */
325 int NET_Timeout(JNIEnv *unused, int s, long timeout) {
326 long prevtime = 0, newtime;
327 struct timeval t;
328 fdEntry_t *fdEntry = getFdEntry(s);
329
330 /*
331 * Check that fd hasn't been closed.
332 */
333 if (fdEntry == NULL) {
334 errno = EBADF;
335 return -1;
336 }
337
338 /*
339 * Pick up current time as may need to adjust timeout
340 */
341 if (timeout > 0) {
342 gettimeofday(&t, NULL);
343 prevtime = t.tv_sec * 1000 + t.tv_usec / 1000;
344 }
345
|