Imagine the situation of multiple tasks needing to access a shared resource. To control access to the resource, you can use a global variable: if the global variable is 0, this indicates that the resource is free, and a task can set the variable to 1 and proceed to use the resource. When the task is done with the resource, it sets the variable back to 0. If the variable is 1 when a task wants to access the resource, then it busy-waits until it becomes set to 0. In fact, some CPUs provide a single instruction to test and modify a memory location precisely for this type of use.


Using global variables has many downsides, however, especially under a multitasking environment. A MUTEX (MUTually EXclusive access to shared resources) is a more sophisticated form of resource-sharing mechanism. A task calls REXIS_MutexLock to lock a MUTEX, and later calls REXIS_MutexUnlock to release the MUTEX when the task is done with the resource. If a MUTEX is unavailable when a task tries to lock it, then the task is blocked until the MUTEX becomes available or until the timeout value expires. Under REXIS, if there are multiple tasks waiting for a MUTEX, they are ordered by task priority so that a high priority task will get the MUTEX before a lower priority task.