Package com.mckoi.database
Class LockingQueue
- java.lang.Object
-
- com.mckoi.database.LockingQueue
-
final class LockingQueue extends java.lang.Object
This class is used in the 'LockingMechanism' class. It maintains a queue of threads that have locked the table this queue refers to. A lock means the table is either pending to be accessed, or the data in the table is being used.A write lock in the queue stops any concurrently running threads from accessing the tables. A read lock can go ahead only if there is no write lock in the queue below it.
The rules are simple, and allow for reading of tables to happen concurrently and writing to happen sequentually. Once a table is pending being written to, it must be guarenteed that no thread can read the table while the write is happening.
-
-
Field Summary
Fields Modifier and Type Field Description private DataTable
parent_table
The DataTable this queue is 'protecting'private java.util.ArrayList
queue
This is the queue that stores the table locks.
-
Constructor Summary
Constructors Constructor Description LockingQueue(DataTable table)
The Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
addLock(Lock lock)
Adds a lock to the queue.(package private) void
checkAccess(Lock lock)
Looks at the queue and _blocks_ if the access to the table by the means specified in the lock is allowed or not.(package private) DataTable
getTable()
Returns the DataTable object the queue is 'attached' to.(package private) void
removeLock(Lock lock)
Removes a lock from the queue.java.lang.String
toString()
-
-
-
Field Detail
-
parent_table
private DataTable parent_table
The DataTable this queue is 'protecting'
-
queue
private java.util.ArrayList queue
This is the queue that stores the table locks.
-
-
Constructor Detail
-
LockingQueue
LockingQueue(DataTable table)
The Constructor.
-
-
Method Detail
-
getTable
DataTable getTable()
Returns the DataTable object the queue is 'attached' to.
-
addLock
void addLock(Lock lock)
Adds a lock to the queue. NOTE: This method is thread safe since it is only called from the LockingMechanism synchronized methods. SYNCHRONIZED: This has to be synchronized because we don't want new locks being added while a 'checkAccess' is happening.
-
removeLock
void removeLock(Lock lock)
Removes a lock from the queue. This also does a 'notifyAll()' to kick any threads that might be blocking in the 'checkAccess' method. SYNCHRONIZED: This has to be synchronized because we don't want locks to be removed while a 'checkAccess' is happening.
-
checkAccess
void checkAccess(Lock lock)
Looks at the queue and _blocks_ if the access to the table by the means specified in the lock is allowed or not. The rules for determining this are as follows:1) If the lock is a READ lock and there is a WRITE lock 'infront' of this lock on the queue then block. 2) If the lock is a WRITE lock and the lock isn't at the front of the queue then block. 3) Retry when a lock is released from the queue.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-