Shared Resources

Accessing a Shared Resource from two separate modules

A shared resource is identified by an ATN topic that can be requested by multiple modules, but is only available to be used by one module at a time. Each module registers a request bit with the resource, and when that bit goes high the resource is considered locked out by that module. Both modules in turn cyclically check for the availability of the resource.

Module A

PROGRAM _INIT
	
	registerToResource('sharedRail', 'Module A', task.internal.localUserId, task.internal.requestRail);
	 
END_PROGRAM

PROGRAM _CYCLIC
	
	task.internal.railAvailable := resourceIsAvailable('sharedRail', task.internal.localUserId);

    // Process a command that requires use of the shared resource.
    IF ((task.cmd.moveToRail) AND (task.internal.railAvailable)) THEN

        task.internal.requestRail := TRUE;

        // Perform other logic with the shared resource, knowing that other modules are 'locked out'. 
        // ...

    END_IF
	 
END_PROGRAM

The registerToResource call links the local task.internal.requestRail BOOL to the shared resource. When that BOOL is set to TRUE in the cyclic portion of the task, the rail resource is assigned to Module A. In that case the resource will continue to show up as available to the module that has requested it, but will not be available to any other modules. The resourceIsAvailable call is used to determine resource availability at runtime.

Module B

PROGRAM _INIT
	
	registerToResource('sharedRail', 'Module B', task.internal.localUserId, task.internal.requestRail);
	 
END_PROGRAM

PROGRAM _CYCLIC
	
	task.internal.railAvailable := resourceIsAvailable('sharedRail', task.internal.localUserId);

    // Process a command that requires use of the shared resource.
    IF ((task.cmd.liftRail) AND (task.internal.railAvailable)) THEN

        task.internal.requestRail := TRUE;

        // Perform other logic with the shared resource, knowing that other modules are 'locked out'. 
        // ...

    END_IF
	 
END_PROGRAM

The registration and checking portions are identical to Module A, but the command that Module B is processing is different (task.cmd.liftRail). So long as the rail resource is actively being requested by Module A, Module B will see that task.internal.railAvailable is FALSE, and will not be able to proceed with its logic.