Using Locks
This section contains information that applies to both Windows and Linux.
In VISA, applications can open multiple sessions to a VISA resource simultaneously. Applications can, therefore, access a VISA resource concurrently through different sessions. However, in certain cases, applications accessing a VISA resource may want to restrict other applications from accessing that resource.
Lock Functions
For example, when an application needs to perform successive write operations on a resource, the application may require that, during the sequence of writes, no other operation can be invoked through any other session to that resource. For such circumstances, VISA defines a locking mechanism that restricts access to resources.
The VISA locking mechanism enforces arbitration of accesses to VISA resources on a per-session basis. If a session locks a resource, operations invoked on the resource through other sessions either are serviced or are returned with an error, depending on the operation and the type of lock.
If a VISA resource is not locked by any of its sessions, all sessions have full privilege to invoke any operation and update any global attributes. Sessions are not required to have locks to invoke operations or update global attributes. However, if some other session has already locked the resource, attempts to update global attributes or invoke certain operations will fail.
viLock/viUnlock Functions
The VISA viLock function is used to acquire a lock on a resource.
viLock(vi, lockType, timeout, requestedKey, accessKey);
The VI_ATTR_RSRC_LOCK_STATE attribute specifies the current locking state of the resource on the given session, which can be either VI_NO_LOCK, VI_EXCLUSIVE_LOCK, or VI_SHARED_LOCK.
The VISA viUnlock function is then used to release the lock on a resource. If a resource is locked and the current session does not have the lock, the error VI_ERROR_RSRC_LOCKED is returned.
VISA Lock Types
VISA defines two different types of locks: Exclusive Lock and Shared Lock.
Exclusive Lock - A session can lock a VISA resource using the lock type VI_EXCLUSIVE_LOCK to get exclusive access privileges to the resource. This exclusive lock type excludes access to the resource from all other sessions.
If a session has an exclusive lock, other sessions cannot modify global attributes or invoke operations on the resource. However, the other sessions can still get attributes.
Shared Lock - A session can share a lock on a VISA resource with other sessions by using the lock type VI_SHARED_LOCK. Shared locks in VISA are similar to exclusive locks in terms of access privileges, but can still be shared between multiple sessions.
If a session has a shared lock, other sessions that share the lock can also modify global attributes and invoke operations on the resource (of course, unless some other session has a previous exclusive lock on that resource). A session that does not share the lock will lack these capabilities.
Locking a resource restricts access from other sessions, and in the case where an exclusive lock is acquired, ensures that operations do not fail because other sessions have acquired a lock on that resource. Thus, locking a resource prevents other, subsequent sessions from acquiring an exclusive lock on that resource. Yet, when multiple sessions have acquired a shared lock, VISA allows one of the sessions to acquire an exclusive lock along with the shared lock it is holding.
Also, VISA supports nested locking. That is, a session can lock the same VISA resource multiple times (for the same lock type) via multiple invocations of the viLock function. In such a case, unlocking the resource requires an equal number of invocations of the viUnlock function. Nested locking is explained in detail later in this topic.
Some VISA operations may be permitted even when there is an exclusive lock on a resource, or some global attributes may not be read when there is any kind of lock on the resource. These exceptions, when applicable, are mentioned in the descriptions of the individual VISA functions and attributes.
Example: Exclusive Lock
The lockexcl.c sample program shows a session gaining an exclusive lock to perform the viPrintf and viScanf VISA operations on a GPIB device. It then releases the lock via the viUnlock function.
Example: Shared Lock
The lockshr.c sample program shows a session gaining a shared lock with the accessKey called lockkey. Other sessions can now use this accessKey in the requestedKey parameter of the viLock function to share access on the locked resource. This example then shows the original session acquiring an exclusive lock while maintaining its shared lock.
When the session holding the exclusive lock unlocks the resource via the viUnlock function, all the sessions sharing the lock again have all the access privileges associated with the shared lock.