Move iolooper.h to include/android/
+ Move iolooper-select.c to android/
Change-Id: I5154aa491132b43c3b80c248bb8dd968f4d41c2a
diff --git a/android/adb-server.c b/android/adb-server.c
index bc7baa1..117b7a8 100644
--- a/android/adb-server.c
+++ b/android/adb-server.c
@@ -16,7 +16,7 @@
#include "qemu-common.h"
#include "android/sockets.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
#include "android/async-utils.h"
#include "android/utils/debug.h"
#include "android/utils/list.h"
diff --git a/android/async-socket-connector.c b/android/async-socket-connector.c
index f4d51b5..71740e4 100644
--- a/android/async-socket-connector.c
+++ b/android/async-socket-connector.c
@@ -23,7 +23,7 @@
#include "android/utils/debug.h"
#include "android/async-socket-connector.h"
#include "utils/panic.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
#define E(...) derror(__VA_ARGS__)
#define W(...) dwarning(__VA_ARGS__)
diff --git a/android/async-socket.c b/android/async-socket.c
index ab4e4b6..92c2ef7 100644
--- a/android/async-socket.c
+++ b/android/async-socket.c
@@ -24,7 +24,7 @@
#include "android/async-socket-connector.h"
#include "android/async-socket.h"
#include "utils/panic.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
#define E(...) derror(__VA_ARGS__)
#define W(...) dwarning(__VA_ARGS__)
diff --git a/android/iolooper-select.c b/android/iolooper-select.c
new file mode 100644
index 0000000..91dfece
--- /dev/null
+++ b/android/iolooper-select.c
@@ -0,0 +1,242 @@
+#include "android/iolooper.h"
+#include "qemu-common.h"
+
+/* An implementation of iolooper.h based on Unix select() */
+#ifdef _WIN32
+# include <winsock2.h>
+#else
+# include <sys/types.h>
+# include <sys/select.h>
+#endif
+#include "android/sockets.h"
+
+struct IoLooper {
+ fd_set reads[1];
+ fd_set writes[1];
+ fd_set reads_result[1];
+ fd_set writes_result[1];
+ int max_fd;
+ int max_fd_valid;
+};
+
+IoLooper*
+iolooper_new(void)
+{
+ IoLooper* iol = malloc(sizeof(*iol));
+ iolooper_reset(iol);
+ return iol;
+}
+
+void
+iolooper_free( IoLooper* iol )
+{
+ free(iol);
+}
+
+void
+iolooper_reset( IoLooper* iol )
+{
+ FD_ZERO(iol->reads);
+ FD_ZERO(iol->writes);
+ iol->max_fd = -1;
+ iol->max_fd_valid = 1;
+}
+
+static void
+iolooper_add_fd( IoLooper* iol, int fd )
+{
+ if (iol->max_fd_valid && fd > iol->max_fd) {
+ iol->max_fd = fd;
+ }
+}
+
+static void
+iolooper_del_fd( IoLooper* iol, int fd )
+{
+ if (iol->max_fd_valid && fd == iol->max_fd)
+ iol->max_fd_valid = 0;
+}
+
+void
+iolooper_modify( IoLooper* iol, int fd, int oldflags, int newflags )
+{
+ if (fd < 0)
+ return;
+
+ int changed = oldflags ^ newflags;
+
+ if ((changed & IOLOOPER_READ) != 0) {
+ if ((newflags & IOLOOPER_READ) != 0)
+ iolooper_add_read(iol, fd);
+ else
+ iolooper_del_read(iol, fd);
+ }
+ if ((changed & IOLOOPER_WRITE) != 0) {
+ if ((newflags & IOLOOPER_WRITE) != 0)
+ iolooper_add_write(iol, fd);
+ else
+ iolooper_del_write(iol, fd);
+ }
+}
+
+
+static int
+iolooper_fd_count( IoLooper* iol )
+{
+ int max_fd = iol->max_fd;
+ int fd;
+
+ if (iol->max_fd_valid)
+ return max_fd + 1;
+
+ /* recompute max fd */
+ for (fd = 0; fd < FD_SETSIZE; fd++) {
+ if (!FD_ISSET(fd, iol->reads) && !FD_ISSET(fd, iol->writes))
+ continue;
+
+ max_fd = fd;
+ }
+ iol->max_fd = max_fd;
+ iol->max_fd_valid = 1;
+
+ return max_fd + 1;
+}
+
+void
+iolooper_add_read( IoLooper* iol, int fd )
+{
+ if (fd >= 0) {
+ iolooper_add_fd(iol, fd);
+ FD_SET(fd, iol->reads);
+ }
+}
+
+void
+iolooper_add_write( IoLooper* iol, int fd )
+{
+ if (fd >= 0) {
+ iolooper_add_fd(iol, fd);
+ FD_SET(fd, iol->writes);
+ }
+}
+
+void
+iolooper_del_read( IoLooper* iol, int fd )
+{
+ if (fd >= 0) {
+ iolooper_del_fd(iol, fd);
+ FD_CLR(fd, iol->reads);
+ }
+}
+
+void
+iolooper_del_write( IoLooper* iol, int fd )
+{
+ if (fd >= 0) {
+ iolooper_del_fd(iol, fd);
+ FD_CLR(fd, iol->writes);
+ }
+}
+
+int
+iolooper_poll( IoLooper* iol )
+{
+ int count = iolooper_fd_count(iol);
+ int ret;
+ fd_set errs;
+
+ if (count == 0)
+ return 0;
+
+ FD_ZERO(&errs);
+
+ do {
+ struct timeval tv;
+
+ tv.tv_sec = tv.tv_usec = 0;
+
+ iol->reads_result[0] = iol->reads[0];
+ iol->writes_result[0] = iol->writes[0];
+
+ ret = select( count, iol->reads_result, iol->writes_result, &errs, &tv);
+ } while (ret < 0 && errno == EINTR);
+
+ return ret;
+}
+
+int
+iolooper_wait( IoLooper* iol, int64_t duration )
+{
+ int count = iolooper_fd_count(iol);
+ int ret;
+ fd_set errs;
+ struct timeval tm0, *tm = NULL;
+
+ if (count == 0)
+ return 0;
+
+ CLAMP_MAC_TIMEOUT(duration);
+
+ if (duration < 0)
+ tm = NULL;
+ else {
+ tm = &tm0;
+ tm->tv_sec = duration / 1000;
+ tm->tv_usec = (duration - 1000*tm->tv_sec) * 1000;
+ }
+
+ FD_ZERO(&errs);
+
+ do {
+ iol->reads_result[0] = iol->reads[0];
+ iol->writes_result[0] = iol->writes[0];
+
+ ret = select( count, iol->reads_result, iol->writes_result, &errs, tm);
+ if (ret == 0) {
+ // Indicates timeout
+ errno = ETIMEDOUT;
+ }
+ } while (ret < 0 && errno == EINTR);
+
+ return ret;
+}
+
+
+int
+iolooper_is_read( IoLooper* iol, int fd )
+{
+ return FD_ISSET(fd, iol->reads_result);
+}
+
+int
+iolooper_is_write( IoLooper* iol, int fd )
+{
+ return FD_ISSET(fd, iol->writes_result);
+}
+
+int
+iolooper_has_operations( IoLooper* iol )
+{
+ return iolooper_fd_count(iol) > 0;
+}
+
+int64_t
+iolooper_now(void)
+{
+ struct timeval time_now;
+ return gettimeofday(&time_now, NULL) ? -1 : (int64_t)time_now.tv_sec * 1000LL +
+ time_now.tv_usec / 1000;
+}
+
+int
+iolooper_wait_absolute(IoLooper* iol, int64_t deadline)
+{
+ int64_t timeout = deadline - iolooper_now();
+
+ /* If the deadline has passed, set the timeout to 0, this allows us
+ * to poll the file descriptor nonetheless */
+ if (timeout < 0)
+ timeout = 0;
+
+ return iolooper_wait(iol, timeout);
+}
diff --git a/android/looper-generic.c b/android/looper-generic.c
index 9fd4f0b..c8df86d 100644
--- a/android/looper-generic.c
+++ b/android/looper-generic.c
@@ -15,7 +15,7 @@
#include "android/utils/refset.h"
#include "android/utils/system.h"
#include "android/looper.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
#include "android/sockets.h"
#include <inttypes.h>
#include <limits.h>
diff --git a/android/main.c b/android/main.c
index 757a05f..61f794a 100644
--- a/android/main.c
+++ b/android/main.c
@@ -55,7 +55,7 @@
#include "android/snapshot.h"
#include "android/framebuffer.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
AndroidRotation android_framebuffer_rotation;
diff --git a/android/protocol/core-connection.c b/android/protocol/core-connection.c
index 35f4d6a..68831d4 100644
--- a/android/protocol/core-connection.c
+++ b/android/protocol/core-connection.c
@@ -15,7 +15,7 @@
#include "android/sockets.h"
#include "qemu-common.h"
#include "errno.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
#include "android/android.h"
#include "android/utils/debug.h"
#include "android/globals.h"
diff --git a/android/sdk-controller-socket.c b/android/sdk-controller-socket.c
index 8b0d813..eb1f220 100644
--- a/android/sdk-controller-socket.c
+++ b/android/sdk-controller-socket.c
@@ -25,7 +25,7 @@
#include "android/async-socket.h"
#include "android/sdk-controller-socket.h"
#include "utils/panic.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
#define E(...) derror(__VA_ARGS__)
#define W(...) dwarning(__VA_ARGS__)
diff --git a/android/sync-utils.c b/android/sync-utils.c
index cab0fa6..4f2fd81 100644
--- a/android/sync-utils.c
+++ b/android/sync-utils.c
@@ -21,7 +21,7 @@
#include "qemu-common.h"
#include "errno.h"
-#include "iolooper.h"
+#include "android/iolooper.h"
#include "android/sockets.h"
#include "android/utils/debug.h"
#include "android/sync-utils.h"