From 18de748028ecf6bcf842ca81b46e8e09d8b226df Mon Sep 17 00:00:00 2001 From: Neil Webber Date: Mon, 18 Sep 2023 21:15:20 -0500 Subject: [PATCH] improved prioritization --- interrupts.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/interrupts.py b/interrupts.py index fd869d4..1f80521 100644 --- a/interrupts.py +++ b/interrupts.py @@ -21,7 +21,6 @@ # SOFTWARE. from collections import namedtuple -from functools import partial import threading from pdptraps import PDPTrap @@ -40,6 +39,16 @@ PendingInterrupt = namedtuple( 'PendingInterrupt', ('pri', 'vector', 'callback')) +# Interrupts are priority sorted by pri (duh), but (less obviously) +# two interrupts with the same pri are further priority sorted by +# vector, with lower vector being higher priority. This calculation +# encapsulates that into a single integer value, with the knowledge +# that no vectors can ever be anywhere near as high as 16384. + +def _qpri(pdi): + return (pdi.pri * 16384) + (16384 - pdi.vector) + + # To cause an interrupt, a device creates a PendingInterrupt containing: # pri -- priority # vector -- the interrupt vector @@ -95,8 +104,7 @@ class InterruptManager: # by the bus signal protocol yet, you can't assert the # same interrupt line again ... it's already asserted) if irq not in self.requests: - self.requests = sorted( - self.requests + [irq], key=lambda q: q.pri) + self.requests = sorted(self.requests + [irq], key=_qpri) self.pri_pending = self.requests[-1].pri self.condition.notify_all() @@ -133,6 +141,7 @@ class InterruptManager: if __name__ == "__main__": import unittest + from functools import partial class TestMethods(unittest.TestCase): def test__init__(self):