shithub: hexen

Download patch

ref: 421b13439f877ca1b3d9a13c07df10bf16928897
parent: 8bd16fed5b196ae0d0c05e2d8412b2e7b08901ba
author: Jacob Moody <[email protected]>
date: Tue Jan 24 21:48:00 EST 2023

lock when adding events, both mouse and key proc do it

--- a/h2_main.c
+++ b/h2_main.c
@@ -602,6 +602,8 @@
 	}
 }
 
+QLock	eventlock;
+
 //==========================================================================
 //
 // H2_ProcessEvents
@@ -639,11 +641,23 @@
 //
 //==========================================================================
 
-void H2_PostEvent(event_t *ev)
+void H2_PostEvent (event_t* ev)
 {
-	events[eventhead] = *ev;
-	eventhead++;
-	eventhead &= (MAXEVENTS - 1);
+    int next;
+
+retry:
+    qlock(&eventlock);
+    next = (eventhead+1)&(MAXEVENTS-1);
+    if(next == eventtail){
+        qunlock(&eventlock);
+        if(ev->type != ev_keydown && ev->type != ev_keyup)
+            return;
+        sleep(1);
+        goto retry;
+    }
+    events[eventhead] = *ev;
+    eventhead = next;
+    qunlock(&eventlock);
 }
 
 //==========================================================================