Using Locks

Because SICL allows multiple sessions on the same device or interface, the action of opening does not mean you have exclusive use. In some cases this is not an issue, but it should be a consideration if you are concerned with program portability.

The subtopics are:

What are Locks?

The SICL ilock function is used to lock an interface or device. The SICL iunlock function is used to unlock an interface or device.

Locks are performed on a per-session (device, interface, or commander) basis. Also, locks can be nested. The device or interface only becomes unlocked when the same number of unlocks are done as the number of locks. Doing an unlock without a lock returns the error I_ERR_NOLOCK.

What does it mean to lock? Locking an interface (from an interface session) restricts other device and interface sessions from accessing this interface. Locking a device restricts other device sessions from accessing this device; however, other interface sessions may continue to access the interface for this device. Locking a commander (from a commander session) restricts other commander sessions from accessing this commander.

It is possible for an interface session to access a device locked from a device session. In such a case, data may be lost from the device session that was underway. For example, Keysight VEE applications use SICL interface sessions. Therefore, I/O operations from VEE applications can supersede any device session that has a lock on a particular device.

Not all SICL routines are affected by locks. Some routines that set or return session parameters never touch the interface hardware and therefore work without locks. For information on using locks in multi-threaded SICL applications over LAN, see Using SICL with a LAN.

Lock Actions

If a session tries to perform any SICL function that obeys locks on an interface or device currently locked by another session, the default action is to suspend the call until the lock is released, or, if a timeout is set, until the call times out.

This action can be changed with the isetlockwait function. If the isetlockwait function is called with the flag parameter set to 0 (zero), the default action is changed. Rather than causing SICL functions to suspend, an error will be returned immediately.

To return to the default action, to suspend and wait for an unlock, call the isetlockwait function with the flag set to any non-zero value.

Locking in a Multi-User Environment

In a multi-user/multi-process environment where devices are being shared, it is a good idea to use locking to ensure exclusive use of a particular device or set of devices. However, as explained in , an interface session can access a device locked from a device session.

In general, it is not good programming practice to lock a device at the beginning of an application and unlock it at the end. This can result in deadlocks or long waits by others who want to use the resource.

The recommended procedure is to use locking per transaction. Per transaction means that you lock before you set up the device, then unlock after all desired data has been acquired. When sharing a device, you cannot assume the state of the device, so the beginning of each transaction should have any setup needed to configure the device or devices to be used.

Sample: Device Locking (C)

/* locking.c
This example shows how device locking can be used to gain exclusive access to a device*/

#include <sicl.h>
#include <stdio.h>

main()
{
INST dvm;
char strres[20];
unsigned long actual;

/* Log message and terminate on error */
ionerror (I_ERROR_EXIT);

/* Open the multimeter session */
dvm = iopen (“gpib0,16”);
itimeout (dvm, 10000);

/* Lock the multimeter device to prevent
access from other applications*/
ilock(dvm);

/* Take a measurement */
iwrite (dvm, “MEAS:VOLT:DC?\n”, 14, 1, NULL);

/* Read the results */
iread (dvm, strres, 20, NULL, &actual);

/* Release the multimeter device for use by
others */
iunlock(dvm);

/* NULL terminate result string and print
results */
/* This technique assumes the last byte sent
was a line-feed */

if (actual) {
strres[actual - 1] = (char) 0;
printf(“Result is %s\n”, strres);
}

/* Close the multimeter session */
iclose(dvm)
;

return 0;}

Sample Device Locking (Visual Basic)

This section contains information specific to the Windows product.

Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''
' locking.bas
' This example shows how device locking can be
used to gain exclusive access to a device
'''''''''''''''''''''''''''''''''''''''''''''''

Sub Main()

Dim dvm As Integer
Dim strres As String * 20 'Fixed length String
Dim actual As Long

'Install an error handler
On Error GoTo ErrorHandler

'Open the multimeter session
dvm = iopen("gpib0,23")
Call itimeout(dvm, 10000)

'Lock the multimeter device to prevent access
‘from other applications
Call ilock(dvm)

'Take a measurement
Call iwrite(dvm, "MEAS:VOLT:DC?" + Chr$(10),
14, 1, 0&)

'Read the results
Call iread(dvm, strres, 20, 0&, actual)

'Release the multimeter for use by others
Call iunlock(dvm)

'Display the results
MsgBox "Result is " + Left$(strres, actual)

'Close the multimeter session
Call iclose(dvm
)

Exit Sub

ErrorHandler:

'Display the error message.
MsgBox "*** Error : " + Erro
r

End Sub