ARTEMIS-2800 Improving trapping race
diff --git a/.RELEASING.md.swp b/.RELEASING.md.swp
new file mode 100644
index 0000000..40d7ef4
--- /dev/null
+++ b/.RELEASING.md.swp
Binary files differ
diff --git a/.pom.xml.swp b/.pom.xml.swp
new file mode 100644
index 0000000..661be31
--- /dev/null
+++ b/.pom.xml.swp
Binary files differ
diff --git a/src/main/c/org_apache_activemq_artemis_nativo_jlibaio_LibaioContext.c b/src/main/c/org_apache_activemq_artemis_nativo_jlibaio_LibaioContext.c
index d495eb4..eaecdbf 100644
--- a/src/main/c/org_apache_activemq_artemis_nativo_jlibaio_LibaioContext.c
+++ b/src/main/c/org_apache_activemq_artemis_nativo_jlibaio_LibaioContext.c
@@ -151,6 +151,11 @@
             }
 
             if (available >= max) {
+               short retryTail = 0;
+               // we first wait for 20 iterations, to see if the tail moved
+               for (retryTail = 0; retryTail < 20 && ring->tail == tail; retryTail++) {
+                  mem_barrier();
+               }
                // This is to trap a possible bug from the kernel:
                //       https://bugzilla.redhat.com/show_bug.cgi?id=1845326
                //       https://issues.apache.org/jira/browse/ARTEMIS-2800
@@ -160,7 +165,10 @@
                // while (ring->tail == tail) mem_barrier();
                //
                // however eventually we could have available==max in a legal situation what could lead to infinite loop here
-               return io_getevents(aio_ctx, min_nr, max, events, timeout);
+               if (retryTail == 20) {
+                   // if the tail didn't move, we will then perform a regular syscall
+                   return io_getevents(aio_ctx, min_nr, max, events, timeout);
+               }
 
                // also: I could have called io_getevents to the one at the end of this method
                //       but I really hate goto, so I would rather have a duplicate code here
@@ -774,6 +782,12 @@
       return;
     }
     int max = theControl->queueSize;
+    if (max > 1) {
+       // We can't maximize the queue reading
+       // as we need to check eventually for the tail moving
+       // this is to minimize the number of syscalls we perform on trapping a previous race.
+       max = max / 2;
+    }
     pthread_mutex_lock(&(theControl->pollLock));
 
     short running = 1;