-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLock.java
38 lines (32 loc) · 1.15 KB
/
Lock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
///////////////////
// Lock: Used as a mutex locking system that ensures only one table is accessed at a time when using transactions
///////////////////
public class Lock extends SQL {
String lockLocation; //the location of the lock
WriteFile writeFile = new WriteFile(); //the file writer, used to write files
ReadFile readFile = new ReadFile(); //the file reader, used to read files
//Lock - default constructor sets the lock location
Lock(String lockLocation) {
this.lockLocation = lockLocation; //set the token command
}
//acquire - acquires the lock if possible
public boolean acquire() {
if(!isLocked()) {
writeFile.createFile(lockLocation + "_lock");
return true;
}
return false;
}
//release - releases a lock if in posession of one
public boolean release() {
if(isLocked()) {
writeFile.deleteFile(lockLocation + "_lock");
return true;
}
return false;
}
//isLocked - returns whether or not a lock is acquired
public boolean isLocked() {
return readFile.fileExists(lockLocation + "_lock");
}
}