ILock interface
The ILock interface provides concurrency control for objects operating in a multithreaded environment (for example, in applications that use distributed state).
AppLogics use locks to protect objects during concurrent operations. For example, state and session nodes implement this interface. Applications that access state or session data concurrently must synchronize using the methods in this interface.
A lock has the following attributes:
The ILock interface defines methods for locking and unlocking objects. It also defines a method for changing the lock mode.
Package
com.kivasoft
Methods
changeMode( )
Changes the lock on an object.
Note. This method is not supported for locks on state and session nodes. State and
session support only one lock mode, GXLOCK_EXCL, which cannot be
changed.
Syntax
public int changeMode(
int dwOldMode,
int dwNewMode,
byte[] pID,
int nSize);
dwOldMode.
Current lock mode applied to an object. The mode is one of GXLOCK_EXCL (exclusive lock) or GXLOCK_SHARE (shared lock).
dwNewMode.
New locking mode, one of GXLOCK_EXCL (exclusive lock) or GXLOCK_SHARE (shared lock). Optionally, the mode may also include GXLOCK_NOBLOCK if the operation should be allowed to continue if the desired locking mode is not available. If GXLOCK_NOBLOCK is not specified, then a thread is blocked if the desired locking mode is not available.
pID.
ID of the caller requesting the change to the lock. This value is read only.
nSize.
Size of the identifier.
Usage
Use changeMode( ) to change a lock on an object.
Return Value
An integer indicating success or failure of the lock mode change operation.
lock( )
Locks an object.
Syntax
public int lock(
int dwFlags,
byte[] pID,
int nSize);
dwFlags.
Locking mode, one of GXLOCK_EXCL (exclusive lock) or GXLOCK_SHARE (shared lock). Optionally, the mode may also include GXLOCK_NOBLOCK if the operation should be allowed to continue if the desired locking mode is not available. If GXLOCK_NOBLOCK is not specified, then a thread is blocked if the desired locking mode is not available.
GXLOCK_EXCL is the only mode currently supported for locking a state or session node. You cannot specify GXLOCK_NOBLOCK for state and session nodes.
pID.
ID of the caller requesting the lock. This value is a byte array. For state and session objects that implement the locking interface, you can pass in a null value for pID because these implementations automatically use the ID of the calling thread for pID.
nSize.
Size of the identifier.
Usage
Use lock( ) to lock an object.
Rules
When you lock certain kinds of nodes, the following rules apply:
Return Value
An integer indicating success or failure of the lock operation.
Example
The following code shows how to lock and unlock a state node:
IState2 marketnews = cacheroot.getStateChild("mktnews");
if (marketnews == null)
return;
// we expect marketnews state node to be accessed concurrently
// let's lock it
ILock l = null;
int ret;
if (marketnews instanceof ILock)
{
l = (ILock)marketnews;
ret = l.lock(GXLOCK.GXLOCK_EXCL, null, 0);
if (ret != GXE.SUCCESS)
{
log("lock error");
return;
}
}
else
{
log("IState2 object doesn't implement the ILock interface");
return;
}
if (l == null)
return;
// we now have the node locked in exclusive mode
// ..... do work ..........
// and unlock the node
ret = l.unlock(GXLOCK.GXLOCK_EXCL, null, 0);
if (ret != GXE.SUCCESS)
log("unlock error");
Related Topics
AppLogic or ISession2 classes
unlock( )
Unlocks a previously locked object.
Syntax
public int unlock(
int dwFlags,
byte[] pID,
int nSize);
dwFlags.
The locking mode previously used to lock the object, either GXLOCK_EXCL (exclusive lock), or GXLOCK_SHARE (shared lock).
GXLOCK_EXCL is the only mode currently supported for unlocking a state or session node.
pID.
The ID of the caller that requests lock removal. This value is a byte array. The ID must match the ID with which you set the lock.
Usually you pass in the ID of the executing thread that requests the lock. For state and session objects that implement the locking interface, you can pass in a null value for pID because these implementations automatically use the ID of the calling thread for pID.
nSize.
Size of the identifier.
Usage
Use unlock( ) to remove a lock on an object.
Return Value
An integer indicating success or failure of the unlock operation.
Example
The following code shows how to lock and unlock a state node:
IState2 marketnews = cacheroot.getStateChild("mktnews");
if (marketnews == null)
return;
// we expect marketnews state node to be accessed concurrently
// let's lock it
ILock l = null;
int ret;
if (marketnews instanceof ILock)
{
l = (ILock)marketnews;
ret = l.lock(GXLOCK.GXLOCK_EXCL, null, 0);
if (ret != GXE.SUCCESS)
{
log("lock error");
return;
}
}
else
{
log("IState2 doesn't implement the ILock interface");
return;
}
if (l == null)
return;
// we now have the node locked in exclusive mode
// ..... do work ..........
// and unlock the node
ret = l.unlock(GXLOCK.GXLOCK_EXCL, null, 0);
if (ret != GXE.SUCCESS)
log("unlock error");
Related Topics
AppLogic or ISession2 classes
|