change execution order of event handler and event finish functions

Sun, 02 Jun 2024 12:39:04 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 02 Jun 2024 12:39:04 +0200
changeset 529
cd606400f0ba
parent 528
8206bfafb7a6
child 530
1e117b5d6710

change execution order of event handler and event finish functions

src/server/daemon/event_bsd.c file | annotate | diff | comparison | revisions
src/server/daemon/event_linux.c file | annotate | diff | comparison | revisions
src/server/daemon/event_solaris.c file | annotate | diff | comparison | revisions
--- a/src/server/daemon/event_bsd.c	Sun Jun 02 10:22:12 2024 +0200
+++ b/src/server/daemon/event_bsd.c	Sun Jun 02 12:39:04 2024 +0200
@@ -62,24 +62,29 @@
 }
 
 
+#define EV_MAX_EVENTS 32
+
 void ev_handle_events(EventHandler *ev) {
     struct timespec timeout;
     timeout.tv_nsec = 0;
     timeout.tv_sec = 600;
     
-    struct kevent events[64];
-    struct kevent changes[128];
+    struct kevent events[EV_MAX_EVENTS];
+    struct kevent changes[EV_MAX_EVENTS*2];
+    Event *finished[EV_MAX_EVENTS];
     int numchanges = 0;
+    int numfinished;
     
     for(;;) {
         // wait for events
-        int nev = kevent(ev->kqueue, changes, numchanges, events, 64, &timeout);  
+        int nev = kevent(ev->kqueue, changes, numchanges, events, EV_MAX_EVENTS, &timeout);  
         if(nev == -1) {
             log_ereport(LOG_FAILURE, "kevent: %s", strerror(errno));
             continue;
         }
         
         numchanges = 0;
+        int numfinished = 0;
         for(int i=0;i<nev;i++) {
             Event *event = (Event*)events[i].udata;
             if(!event) {
@@ -101,7 +106,7 @@
                     // ret 0 => remove event
                     
                     if(event->finish) {
-                        event->finish(ev, event);
+                        finished[numfinished++] = event;
                     }
                     
                     event_events = 0;
@@ -134,6 +139,11 @@
                 }
             }
         }
+        // call event finish handlers
+        for(int i=0;i<numfinished;i++) {
+            Event *event = finished[i];
+            finished[i]->finish(ev, event);
+        }
     }
 }
 
--- a/src/server/daemon/event_linux.c	Sun Jun 02 10:22:12 2024 +0200
+++ b/src/server/daemon/event_linux.c	Sun Jun 02 12:39:04 2024 +0200
@@ -84,12 +84,13 @@
     return ev;
 }
 
+#define EV_MAX_EVENTS 32
 
 void ev_handle_events(EventHandler *ev) {
     int ep = ev->ep;
     
-    //port_event_t events[16];
-    struct epoll_event events[16];
+    struct epoll_event events[EV_MAX_EVENTS];
+    Event* finished[EV_MAX_EVENTS];
     
     for(;;) {
         /* wait for events */
@@ -99,6 +100,7 @@
             continue;
         }
         
+        int numfinished = 0;
         for(int i=0;i<ret;i++) {
             Event *event = events[i].data.ptr;
             if(!event) {
@@ -134,9 +136,11 @@
                                 strerror(errno));
                     }
                     
-                    // if set, execute event->finish
+                    // if set, remember this event and 
+                    // execute event->finish later
                     if(event->finish) {
-                        event->finish(ev, event);
+                        finished[numfinished++] = event;
+                        //event->finish(ev, event);
                     }
                 } else {
                     if(saved_ev != event->events) {
@@ -158,6 +162,11 @@
                 }
             }
         }
+        // call event finish handlers
+        for(int i=0;i<numfinished;i++) {
+            Event *event = finished[i];
+            finished[i]->finish(ev, event);
+        }
     }
 }
 
--- a/src/server/daemon/event_solaris.c	Sun Jun 02 10:22:12 2024 +0200
+++ b/src/server/daemon/event_solaris.c	Sun Jun 02 12:39:04 2024 +0200
@@ -61,8 +61,11 @@
     return ev;
 }
 
+#define EV_MAX_EVENTS 32
+
 void ev_handle_events(EventHandler *ev) {   
-    port_event_t events[64];
+    port_event_t events[EV_MAX_EVENTS];
+    Event *finished[EV_MAX_EVENTS];
     struct timespec timeout;
     timeout.tv_nsec = 0;
     timeout.tv_sec = 600;
@@ -70,13 +73,14 @@
     for(;;) {
         // wait for events
         uint_t nev = 1;
-        int ret = port_getn(ev->port, events, 64, &nev, &timeout);
+        int ret = port_getn(ev->port, events, EV_MAX_EVENTS, &nev, &timeout);
         if(ret == -1) {
             // TODO: check for error
             perror("port_getn");
             continue;
         }
         
+        int numfinished = 0;
         for(int i=0;i<nev;i++) {
             Event *event = events[i].portev_user;
             if(events[i].portev_source == PORT_SOURCE_AIO) {
@@ -87,7 +91,7 @@
                     aio->result_errno = aiocb->aio_resultp.aio_errno;
                     if(event->fn) {
                         if(!event->fn(ev, event) && event->finish) {
-                            event->finish(ev, event);
+                            finished[numfinished++] = event;
                         }
                     }
                 }
@@ -115,6 +119,11 @@
                 }
             }  
         }
+        // call event finish handlers
+        for(int i=0;i<numfinished;i++) {
+            Event *event = finished[i];
+            finished[i]->finish(ev, event);
+        }
     }
 }
 

mercurial