improved prioritization

This commit is contained in:
Neil Webber 2023-09-18 21:15:20 -05:00
parent a74312451a
commit 18de748028

View file

@ -21,7 +21,6 @@
# SOFTWARE. # SOFTWARE.
from collections import namedtuple from collections import namedtuple
from functools import partial
import threading import threading
from pdptraps import PDPTrap from pdptraps import PDPTrap
@ -40,6 +39,16 @@ PendingInterrupt = namedtuple(
'PendingInterrupt', ('pri', 'vector', 'callback')) '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: # To cause an interrupt, a device creates a PendingInterrupt containing:
# pri -- priority # pri -- priority
# vector -- the interrupt vector # vector -- the interrupt vector
@ -95,8 +104,7 @@ class InterruptManager:
# by the bus signal protocol yet, you can't assert the # by the bus signal protocol yet, you can't assert the
# same interrupt line again ... it's already asserted) # same interrupt line again ... it's already asserted)
if irq not in self.requests: if irq not in self.requests:
self.requests = sorted( self.requests = sorted(self.requests + [irq], key=_qpri)
self.requests + [irq], key=lambda q: q.pri)
self.pri_pending = self.requests[-1].pri self.pri_pending = self.requests[-1].pri
self.condition.notify_all() self.condition.notify_all()
@ -133,6 +141,7 @@ class InterruptManager:
if __name__ == "__main__": if __name__ == "__main__":
import unittest import unittest
from functools import partial
class TestMethods(unittest.TestCase): class TestMethods(unittest.TestCase):
def test__init__(self): def test__init__(self):