gnu_java_nio_VMSelector.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* gnu_java_nio_VMSelector.c - Native methods for SelectorImpl class
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include "config.h"
  32. /* <sys/types.h> needs to be included on OSX before <sys/select.h> */
  33. #if defined(HAVE_SYS_TYPES_H)
  34. #include <sys/types.h>
  35. #endif
  36. #if defined(HAVE_SYS_SELECT_H)
  37. #include <sys/select.h>
  38. #endif
  39. #include <sys/time.h>
  40. #include <string.h>
  41. #include <errno.h>
  42. #include <jni.h>
  43. #include <jcl.h>
  44. #include "gnu_java_nio_VMSelector.h"
  45. /* Amount of characters in the error message buffer for strerror_r. */
  46. #define BUF_SIZE 250
  47. void helper_put_filedescriptors (JNIEnv *, jintArray, fd_set *, int *);
  48. void helper_get_filedescriptors (JNIEnv *, jintArray *, fd_set *);
  49. void helper_reset (JNIEnv *, jintArray *);
  50. int
  51. helper_select (JNIEnv *, jclass, jmethodID,
  52. int, fd_set *, fd_set *, fd_set *, struct timeval *);
  53. void
  54. helper_put_filedescriptors (JNIEnv * env, jintArray fdArray, fd_set * fds,
  55. int *max_fd)
  56. {
  57. jint *tmpFDArray = (*env)->GetIntArrayElements (env, fdArray, 0);
  58. int size = (*env)->GetArrayLength (env, fdArray);
  59. int index, fd;
  60. for (index = 0; index < size; index++)
  61. {
  62. fd = tmpFDArray[index];
  63. if (fd > 0)
  64. {
  65. FD_SET (tmpFDArray[index], fds);
  66. if (tmpFDArray[index] > (*max_fd))
  67. (*max_fd) = tmpFDArray[index];
  68. }
  69. }
  70. }
  71. void
  72. helper_get_filedescriptors (JNIEnv * env, jintArray * fdArray, fd_set * fds)
  73. {
  74. jint *tmpFDArray = (*env)->GetIntArrayElements (env, fdArray, 0);
  75. int size = (*env)->GetArrayLength (env, fdArray);
  76. int index, fd;
  77. for (index = 0; index < size; index++)
  78. {
  79. fd = tmpFDArray[index];
  80. if (fd < 0 || !FD_ISSET (fd, fds))
  81. tmpFDArray[index] = 0;
  82. }
  83. }
  84. void
  85. helper_reset (JNIEnv * env, jintArray * fdArray)
  86. {
  87. jint *tmpFDArray = (*env)->GetIntArrayElements (env, fdArray, 0);
  88. int size = (*env)->GetArrayLength (env, fdArray);
  89. int index;
  90. for (index = 0; index < size; index++)
  91. tmpFDArray[index] = 0;
  92. }
  93. /* A wrapper for select() which ignores EINTR.
  94. * Taken from gclib's posix.cc
  95. */
  96. int
  97. helper_select (JNIEnv * env, jclass thread_class,
  98. jmethodID thread_interrupted, int n, fd_set * readfds,
  99. fd_set * writefds, fd_set * exceptfds, struct timeval *timeout)
  100. {
  101. #ifdef HAVE_SYS_SELECT_H
  102. /* If we have a timeout, compute the absolute ending time. */
  103. struct timeval end, delay, after;
  104. int r;
  105. if (timeout)
  106. {
  107. gettimeofday (&end, NULL);
  108. end.tv_usec += timeout->tv_usec;
  109. if (end.tv_usec >= 1000000)
  110. {
  111. ++end.tv_sec;
  112. end.tv_usec -= 1000000;
  113. }
  114. end.tv_sec += timeout->tv_sec;
  115. delay = *timeout;
  116. }
  117. else
  118. {
  119. /* Placate compiler. */
  120. delay.tv_sec = delay.tv_usec = 0;
  121. }
  122. while (1)
  123. {
  124. r = select (n, readfds, writefds, exceptfds, timeout ? &delay : NULL);
  125. if (r < 0 && errno != EINTR)
  126. return -errno;
  127. else if (r >= 0)
  128. return r;
  129. /* Here we know we got EINTR. */
  130. if ((*env)->
  131. CallStaticBooleanMethod (env, thread_class, thread_interrupted))
  132. {
  133. return -EINTR;
  134. }
  135. if (timeout)
  136. {
  137. gettimeofday (&after, NULL);
  138. /* Now compute new timeout argument. */
  139. delay.tv_usec = end.tv_usec - after.tv_usec;
  140. delay.tv_sec = end.tv_sec - after.tv_sec;
  141. if (delay.tv_usec < 0)
  142. {
  143. --delay.tv_sec;
  144. delay.tv_usec += 1000000;
  145. }
  146. if (delay.tv_sec < 0)
  147. {
  148. /* We assume that the user wants a valid select() call
  149. * more than precise timing. So if we get a series of
  150. * EINTR we just keep trying with delay 0 until we get a
  151. * valid result.
  152. */
  153. delay.tv_sec = 0;
  154. }
  155. }
  156. }
  157. #else /* HAVE_SYS_SELECT_H */
  158. return 0;
  159. #endif
  160. }
  161. JNIEXPORT jint JNICALL
  162. Java_gnu_java_nio_VMSelector_select (JNIEnv * env,
  163. jclass obj __attribute__ ((__unused__)),
  164. jintArray read,
  165. jintArray write,
  166. jintArray except, jlong timeout)
  167. {
  168. jint result;
  169. jclass thread_class = (*env)->FindClass (env, "java/lang/Thread");
  170. jmethodID thread_current_thread =
  171. (*env)->GetStaticMethodID (env, thread_class, "currentThread",
  172. "()Ljava/lang/Thread;");
  173. jmethodID thread_interrupt =
  174. (*env)->GetMethodID (env, thread_class, "interrupt", "()V");
  175. jmethodID thread_interrupted =
  176. (*env)->GetStaticMethodID (env, thread_class, "interrupted", "()Z");
  177. jobject current_thread;
  178. int max_fd = 0;
  179. fd_set read_fds;
  180. fd_set write_fds;
  181. fd_set except_fds;
  182. struct timeval real_time_data;
  183. struct timeval *time_data = NULL;
  184. char *message;
  185. /* If a legal timeout value isn't given, use NULL.
  186. * This means an infinite timeout. The specification
  187. * also says that a zero timeout should be treated
  188. * as infinite. Otherwise (if the timeout value is legal),
  189. * fill our timeval struct and use it for the select.
  190. */
  191. if (timeout > 0)
  192. {
  193. real_time_data.tv_sec = timeout / 1000;
  194. real_time_data.tv_usec = (timeout % 1000) * 1000;
  195. time_data = &real_time_data;
  196. }
  197. /* Reset all fd_set structures */
  198. FD_ZERO (&read_fds);
  199. FD_ZERO (&write_fds);
  200. FD_ZERO (&except_fds);
  201. /* Fill the fd_set data structures for the _Jv_select() call. */
  202. helper_put_filedescriptors (env, read, &read_fds, &max_fd);
  203. helper_put_filedescriptors (env, write, &write_fds, &max_fd);
  204. helper_put_filedescriptors (env, except, &except_fds, &max_fd);
  205. /* Actually do the select */
  206. result =
  207. helper_select (env, thread_class, thread_interrupted, max_fd + 1,
  208. &read_fds, &write_fds, &except_fds, time_data);
  209. if (result == -EINTR)
  210. {
  211. /* The behavior of JRE 1.4.1 is that no exception is thrown
  212. * when the thread is interrupted, but the thread's interrupt
  213. * status is set. Clear all of our select sets and return 0,
  214. * indicating that nothing was selected.
  215. */
  216. current_thread =
  217. (*env)->CallStaticObjectMethod (env, thread_class,
  218. thread_current_thread);
  219. (*env)->CallVoidMethod (env, current_thread, thread_interrupt);
  220. helper_reset (env, read);
  221. helper_reset (env, write);
  222. helper_reset (env, except);
  223. return 0;
  224. }
  225. if (result < 0)
  226. {
  227. #if defined(HAVE_STRERROR_R)
  228. char message_buf[BUF_SIZE+1];
  229. int errorcode = -result;
  230. if (strerror_r (errorcode, message_buf, BUF_SIZE))
  231. {
  232. /* This would mean that message_buf was to small
  233. * to hold the error message.
  234. */
  235. JCL_ThrowException (env, "java/lang/InternalError",
  236. "Not enough space in message buffer.");
  237. return 0;
  238. }
  239. message = message_buf;
  240. #else
  241. message = strerror(errno);
  242. #endif
  243. JCL_ThrowException (env, "java/io/IOException", message);
  244. return 0;
  245. }
  246. /* Set the file descriptors according to the values returned from select(). */
  247. helper_get_filedescriptors (env, read, &read_fds);
  248. helper_get_filedescriptors (env, write, &write_fds);
  249. helper_get_filedescriptors (env, except, &except_fds);
  250. return result;
  251. }