<< Prev        Next >>

ID : 5034

Mutex Object

Mutex object is the object used for exclusion control between the tasks.

Mutex object has "Locked" and "Unlocked" for its status. The default status is "Unlocked." Switching from "Unlocked" to "Locked" can be made by any task. However, switching from "Locked" to "Unlocked" can be made only by the task which made it "Locked."

"TakeMutex" statement makes the status of Mutex object from "Unlocked" to "Locked." When the designated Mutex object is "Locked" by another task, wait until it turns to "Unlocked." This function controls a task exclusively.

Flow of Exclusion Control

1 Create a Mutex object

To create a Mutex object, execute CreateMutex. Specify a name for the Mutex object. When a Mutex object with the same name has already been created by another task, the new Mutex object will not be created and return the existing object ID, then a task reference will be registered to the object.

Mutex object can be created more than one within a project and it is identified by name and ID. You can create up to 64 Mutex objects at the same time. Even if you create an object with the same name, different ID will be assigned every time.

User can give an arbitrary name for the object as an identifier, when the Mutex object is created (when CreateMutex is executed). Name should be within 255 Bytes of string type data. Identification will be made by the string comparison, so if an object's string has different characters in such as upper/lower case and even space, it will be recognized as a different one.

2 Exclusion control

Enclose the process (program code) on each task for exclusion control with "TakeMutex" and "GiveMutex." The task whose "TakeMutex" has been executed first can only be processed, but because Mutex object's status is "Locked", other tasks will wait until the status of Mutex object turns to "Unlocked" through the execution of "TakeMutex."

When the task that turned Mutex object's status to "Locked" first executes "GiveMutex", status of Mutex object turns to "Unlocked" and "TakeMutex" of other tasks who has been waiting will be executed.

3 Delete a Mutex object

When a task completes the exclusion control, execute "DeleteMutex" to request a deletion of the Mutex object to the system. If there is no reference to the Mutex object, it will be deleted. And, request a deletion of a Mutex object automatically when the task is completed.

Mutex object will be deleted when all the tasks whose references registered to the Mutex object are cleared.

Only the (reference is registered) task which executes CreateMutex to the Mutex object can request a deletion request (DeleteMutex) to the Mutex object.

Status of Mutex Object

Mutex object has a property which shows error status.

When a task set the status at "Locked" somehow freezes at "Locked" or is deleted unexpectedly, the Mutex object's status turns to error status, and at the same time, the tasks with "Unlocked" error will delete their references.

When another task tries to execute (TakeMutex) to turn Mutex object in error status to "Locked," the task becomes error status. Execute "ResetMutex" in Error Processing Routine to remove the error of the Mutex object. Refer to "Program Example 3 for Exclusion Control."

Program Example for Exclusion Control

Program example 1 for exclusion control
This is a general example of exclusion control between tasks. It will be executed in Pro1-> Pro2-> Pro3 order.
Program example 2 for exclusion control
This is an example of exclusion control between tasks aware of lifetime of the Mutex object. It will be executed in Pro1-> Pro2-> Pro3 order. Lifetime of Mutex object depends on Pro1.
Program example 3 for exclusion control
This is an example of handling the case that the exclusion control is aborted by an error. It will be executed in Pro1-> Pro2 order and controls exclusively to have I1, I2 and I3 in sequence number any time. When an error occurs, return processing will be executed to handle mismatched status.

ID : 5034

<< Prev        Next >>