One optimization the programmer may want to do is tap into the power of the hardware and operating system interrupts. The MPTHD and MPTSK layers have been carefully designed so their functions can also be called from interrupt handlers with no modifications. For example, by adding this Turbo "C" code, the package becomes a pre-emptive multitasker:
void interrupt (* mpsig_oldclocktick)(); /* SAVE OLD CLOCK TICK INT */ void interrupt mpsig_clocktick() { mpsig_oldclocktick(); mptsk_yield(); } #define MPSIG_CLOCKTICK (0x08) /* INTERRUPT 0x08 ON IBM PC */ void mpres_setup() { .... mpsig_oldclocktick= getvect(MPSIG_CLOCKTICK); setvect(MPSIG_CLOCKTICK,mpsig_clocktick); .... }
Now 18.2 times a second (the frequency of the IBM PC clock), the current task is yielded if other tasks are waiting to run. A similar effect can be achieved in UNIX using the alarm signal.