15 * accompanied this code).
16 *
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 "jni.h"
27 #include "jni_util.h"
28 #include "jvm.h"
29 #include "jlong.h"
30 #include "sun_nio_ch_PollArrayWrapper.h"
31 #include <poll.h>
32 #include <unistd.h>
33 #include <sys/time.h>
34
35 #define RESTARTABLE(_cmd, _result) do { \
36 do { \
37 _result = _cmd; \
38 } while((_result == -1) && (errno == EINTR)); \
39 } while(0)
40
41 static int
42 ipoll(struct pollfd fds[], unsigned int nfds, int timeout)
43 {
44 jlong start, now;
45 int remaining = timeout;
46 struct timeval t;
47 int diff;
48
49 gettimeofday(&t, NULL);
50 start = t.tv_sec * 1000 + t.tv_usec / 1000;
51
52 for (;;) {
53 int res = poll(fds, nfds, remaining);
54 if (res < 0 && errno == EINTR) {
61 return 0;
62 }
63 start = now;
64 }
65 } else {
66 return res;
67 }
68 }
69 }
70
71 JNIEXPORT jint JNICALL
72 Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv *env, jobject this,
73 jlong address, jint numfds,
74 jlong timeout)
75 {
76 struct pollfd *a;
77 int err = 0;
78
79 a = (struct pollfd *) jlong_to_ptr(address);
80
81 if (timeout <= 0) { /* Indefinite or no wait */
82 RESTARTABLE (poll(a, numfds, timeout), err);
83 } else { /* Bounded wait; bounded restarts */
84 err = ipoll(a, numfds, timeout);
85 }
86
87 if (err < 0) {
88 JNU_ThrowIOExceptionWithLastError(env, "Poll failed");
89 }
90 return (jint)err;
91 }
92
93 JNIEXPORT void JNICALL
94 Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
95 {
96 int fakebuf[1];
97 fakebuf[0] = 1;
98 if (write(fd, fakebuf, 1) < 0) {
99 JNU_ThrowIOExceptionWithLastError(env,
100 "Write to interrupt fd failed");
101 }
102 }
|
15 * accompanied this code).
16 *
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 "jni.h"
27 #include "jni_util.h"
28 #include "jvm.h"
29 #include "jlong.h"
30 #include "sun_nio_ch_PollArrayWrapper.h"
31 #include <poll.h>
32 #include <unistd.h>
33 #include <sys/time.h>
34
35 #ifdef _AIX
36 /* needed for javaToNativeEvents()/nativeToJavaEvents() */
37 #include "net_util_md.h"
38 #endif
39
40 #define RESTARTABLE(_cmd, _result) do { \
41 do { \
42 _result = _cmd; \
43 } while((_result == -1) && (errno == EINTR)); \
44 } while(0)
45
46 static int
47 ipoll(struct pollfd fds[], unsigned int nfds, int timeout)
48 {
49 jlong start, now;
50 int remaining = timeout;
51 struct timeval t;
52 int diff;
53
54 gettimeofday(&t, NULL);
55 start = t.tv_sec * 1000 + t.tv_usec / 1000;
56
57 for (;;) {
58 int res = poll(fds, nfds, remaining);
59 if (res < 0 && errno == EINTR) {
66 return 0;
67 }
68 start = now;
69 }
70 } else {
71 return res;
72 }
73 }
74 }
75
76 JNIEXPORT jint JNICALL
77 Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv *env, jobject this,
78 jlong address, jint numfds,
79 jlong timeout)
80 {
81 struct pollfd *a;
82 int err = 0;
83
84 a = (struct pollfd *) jlong_to_ptr(address);
85
86 #ifdef _AIX
87 /* The hard coded event constants from Java do not match the system values */
88 int i;
89 for (i=0; i<numfds; i++) {
90 a[i].events = javaToNativeEvents(a[i].events);
91 }
92 #endif
93
94 if (timeout <= 0) { /* Indefinite or no wait */
95 RESTARTABLE (poll(a, numfds, timeout), err);
96 } else { /* Bounded wait; bounded restarts */
97 err = ipoll(a, numfds, timeout);
98 }
99
100 #ifdef _AIX
101 /* Transfer the event flags back to the hardcoded java event flags. */
102 for (i=0; i<numfds; i++) {
103 a[i].events = nativeToJavaEvents(a[i].events);
104 a[i].revents = nativeToJavaEvents(a[i].revents);
105 }
106 #endif
107
108 if (err < 0) {
109 JNU_ThrowIOExceptionWithLastError(env, "Poll failed");
110 }
111 return (jint)err;
112 }
113
114 JNIEXPORT void JNICALL
115 Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
116 {
117 int fakebuf[1];
118 fakebuf[0] = 1;
119 if (write(fd, fakebuf, 1) < 0) {
120 JNU_ThrowIOExceptionWithLastError(env,
121 "Write to interrupt fd failed");
122 }
123 }
|