If You're coming from a coding background, a mouse click is something you might take for granted. To successfully code an application, we don't need to understand how a mouse click or keypress works, or we might assume we do know at a high level. But when you dive into computer architecture complexity spirals just as quickly as it does when learning code. Today, I want to simplify while giving a still accurate account of what happens when someone clicks their mouse because as long as we're writing thousands of lines of code that depend on user interaction, it would probably nice to know how we're even allowed to interact with those events in the first place.
So, what does happen when a user clicks their mouse? It might go to the event loop or to a handler, but for that to happen first the OS would have to be aware. I mean, how does your computer even know the mouse was clicked in the first place?
Well, how might a computer listen for something like a mouse click. The mouse, upon being clicked, could be wired to change a bit somewhere or file. And if we did that, we could have the computer (the processor) check that file every so often. If it's been flipped from a 0 to a 1, we'd know that the mouse was clicked. But how often would we want to check that file? In a game, we might want to know about clicks every microsecond, and checking a file that often is certainly going to waste energy and processing power. If we check it less often, to be more efficient, we risk a sluggish user interface, not noticing clicks until noticeably late. This is a system that was used in the past and used for other things still. It's called Polling.
Of course, its problems in our use case are apparent from what I listed above. So, we need something better. Well, in the case of polling, the mouse changes something and the processor reacts, but the processor doesn't react until it checks that bit on its schedule. The processor is the prime mover. What would a system where the mouse is the primary mover look like? I suppose the mouse could send a signal directly to the processor. And indeed, that is how our modern systems work.
It's called an interrupt. The mouse or hardware device will send a signal directly to the processor when an event occurs. The Processor must then handle the signal, immediately. This means the processor will pause in whatever function it is running, saving where it is at in the code, and handle the click right then. Afterward, it goes back to the state it saved and continues as if nothing had happened.
Because every hardware event is completely pausing the processor, it's important to handle them as quickly as possible. So, part of the operation then is to do only the minimum amount to document the click, before continuing with the workload. Essiantly, the processor saves the info about the click event and passes it to the Operating Systems scheduler. The OS scheduler will then decide what else needs to be done about it, based on priority and complexity.
Top comments (0)