-

SEMA

Type Function block
Description The object-oriented SEMA function block is used to control the access to a limited number of resources. The function block handles the logic of a semaphore. It ist used as counting semaphore that allows more than one task to access a resource at the same time.

The counter of the semaphore counts how many tasks can access a resource at the same time. The initial value is the initial number of resources that can be accessed by the tasks. Counting starts at 0. The INT value at the FB input MaximumCount specifies the maximum number of times the resource can be accessed simultaneously (default value is 0). If this value is reached, the semaphore blocks the access to the resource until another task releases the semaphore. For example, a semaphore with a MaximumCount value of two can handle up to three concurrent accesses.

The function block provides the following methods and an initial value that are used for interacting with a semaphore:

  • ACQUIRE

    Tries to get access to the resource.

    • If the maximum number of permitted tasks that are allowed to access the semaphore is not reached, the resource is accessed and task execution is continued.
    • If the maximum number of permitted tasks that are allowed to access the semaphore is reached, the tasks waits (task is blocked) until a resource becomes available.

    Note
    In case of a semaphore acquire, a task watchdog can occur. It is therefore recommended to use a TRY_ACQUIRE method instead of an ACQUIRE method (see also the note above).

  • RELEASE

    Releases (frees) the previously accessed resource by one count. This allows other tasks to acquire it later.

  • TRY_ACQUIRE

    Tries to get access to the resource without waiting (blocking).

    • If the maximum number of permitted tasks that are allowed to access the semaphore is not reached, the resource is accessed and task execution is continued.
    • If the maximum number of permitted tasks that are allowed to access the semaphore is reached, the task continues with a return value FALSE. (The caller shall not access the locked code.)

Further Info
See the section "Protecting concurrent access to shared data with Mutex and Semaphore" in the topic "Global Function Blocks in PLCnext Engineer" for further details on semaphores.

Further Info
See the topic "Methods of FBs in PLCnext Engineer" how to use function block methods.

Parameters MaximumCount

Data type:INT
Description: Initial value (non-negative integer) of the semaphore counter. Defines the number of subsequent requests that can get (acquire) the semaphore. The counter is decremented whenever a task locks the semaphore and incremented when a task releases the semaphore.

Inital value: 0.