shithub: heretic

Download patch

ref: 5781718e4d025711c24f6db2eaea14de2d17fec9
parent: cb0b53e51979d276a22428c270c5f7eac004b7da
author: Jacob Moody <moody@posixcafe.org>
date: Tue Jan 24 13:12:52 EST 2023

Lock and filter events in D_PostEvent

copied from doom

--- a/d_main.c
+++ b/d_main.c
@@ -229,11 +229,30 @@
 //
 //---------------------------------------------------------------------------
 
-void D_PostEvent(event_t *ev)
+QLock		eventlock;
+
+
+//
+// D_PostEvent
+// Called by the I/O functions when input is detected
+//
+void D_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);
 }
 
 //---------------------------------------------------------------------------