[PATCH 4/6] move the aio_ring defintion and empty check into a header

From: Christoph Hellwig
Date: Thu Jan 04 2018 - 03:04:54 EST


Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
src/aio_ring.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
src/io_getevents.c | 28 +++-------------------------
2 files changed, 52 insertions(+), 25 deletions(-)
create mode 100644 src/aio_ring.h

diff --git a/src/aio_ring.h b/src/aio_ring.h
new file mode 100644
index 0000000..3842c4b
--- /dev/null
+++ b/src/aio_ring.h
@@ -0,0 +1,49 @@
+/*
+ libaio Linux async I/O interface
+ Copyright 2002 Red Hat, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#ifndef _AIO_RING_H
+#define _AIO_RING_H
+
+#define AIO_RING_MAGIC 0xa10a10a1
+
+struct aio_ring {
+ unsigned id; /* kernel internal index number */
+ unsigned nr; /* number of io_events */
+ unsigned head;
+ unsigned tail;
+
+ unsigned magic;
+ unsigned compat_features;
+ unsigned incompat_features;
+ unsigned header_length; /* size of aio_ring */
+};
+
+static inline int aio_ring_is_empty(io_context_t ctx, struct timespec *timeout)
+{
+ struct aio_ring *ring = (struct aio_ring *)ctx;
+
+ if (!ring || ring->magic != AIO_RING_MAGIC)
+ return 0;
+ if (!timeout || timeout->tv_sec || timeout->tv_nsec)
+ return 0;
+ if (ring->head != ring->tail)
+ return 0;
+ return 1;
+}
+
+#endif /* _AIO_RING_H */
diff --git a/src/io_getevents.c b/src/io_getevents.c
index 5a05174..90d6081 100644
--- a/src/io_getevents.c
+++ b/src/io_getevents.c
@@ -21,36 +21,14 @@
#include <stdlib.h>
#include <time.h>
#include "syscall.h"
+#include "aio_ring.h"

io_syscall5(int, __io_getevents_0_4, io_getevents, io_context_t, ctx, long, min_nr, long, nr, struct io_event *, events, struct timespec *, timeout)

-#define AIO_RING_MAGIC 0xa10a10a1
-
-/* Ben will hate me for this */
-struct aio_ring {
- unsigned id; /* kernel internal index number */
- unsigned nr; /* number of io_events */
- unsigned head;
- unsigned tail;
-
- unsigned magic;
- unsigned compat_features;
- unsigned incompat_features;
- unsigned header_length; /* size of aio_ring */
-};
-
int io_getevents_0_4(io_context_t ctx, long min_nr, long nr, struct io_event * events, struct timespec * timeout)
{
- struct aio_ring *ring;
- ring = (struct aio_ring*)ctx;
- if (ring==NULL || ring->magic != AIO_RING_MAGIC)
- goto do_syscall;
- if (timeout!=NULL && timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
- if (ring->head == ring->tail)
- return 0;
- }
-
-do_syscall:
+ if (aio_ring_is_empty(ctx, timeout))
+ return 0;
return __io_getevents_0_4(ctx, min_nr, nr, events, timeout);
}

--
2.14.2