Mutex: Difference between revisions

Content added Content deleted
(→‎{{header|6502 Assembly}}: formatting for better readability)
Line 27: Line 27:
=Sample implementations / APIs=
=Sample implementations / APIs=
=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
There isn't any hardware support for mutexes, but a simple flag in memory will do. This implementation is more akin to a "starting pistol" for some time-critical process such as the Nintendo Entertainment System's vBlank NMI (Non-Maskable Interrupt) which is typically used to update video memory. The function's parameters are pre-loaded in global memory, but the function that uses them won't be called unless the lock is released. Once all the parameters are ready, the main procedure can wait for the interrupt, after which it releases the lock and waits for the interrupt again. This time, the interrupt routine that needs those parameters is run, and when it's finished, the flag is locked again. For simplicity, most of the hardware-specific routines are omitted (and in reality would require additional mutexes since it usually takes more than one frame to print a long string to the screen.)
There isn't any hardware support for mutexes, but a simple flag in memory will do. This implementation is more akin to a "starting pistol" for some time-critical process such as the Nintendo Entertainment System's vBlank NMI (Non-Maskable Interrupt) which is typically used to update video memory. The function's parameters are pre-loaded in global memory, but the function that uses them won't be called unless the lock is released. Once all the parameters are ready, the main procedure can wait for the interrupt, after which it releases the lock and waits for the interrupt again. This time, the interrupt routine that needs those parameters is run, and when it's finished, the flag is locked again. For simplicity, most of the hardware-specific routines are omitted (and in reality would require additional mutexes since it usually takes more than one frame to do something like print a long string to the screen.)


''Side note: During a 6502's NMI, other interrupts cannot occur, not even another NMI. Therefore there is no chance that an IRQ will happen while executing NMI code.''
''Side note: During a 6502's NMI, other interrupts cannot occur, not even another NMI. Therefore there is no chance that an IRQ will happen while executing NMI code.''