4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
Padlock.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: Padlock.java
5  * Description: Class represents the document protection, and provides methods to lock
6  */
7 
8 /**
9  * @file Padlock.java
10  *
11  * @brief Class represents the document protection, and provides methods to lock
12  */
13 
14 package cz.vutbr.fit.knot.annotations.comet.documentLock;
15 
16 /**
17  * Class represents the document protection, and provides methods to lock.
18  *
19  * @brief Class represents the document protection, and provides methods to lock.
20  * @author Martin Petr (xpetrm05)
21  */
22 public class Padlock {
23  /** id of document that lock belongs */
24  Integer documentId;
25 
26  /** paramether indicate locked document */
27  boolean isLocked;
28 
29  /** paramether indicate that some thread waits for change */
30  boolean waitForUpdate;
31 
32  /** count of readers of document */
34 
35  /**
36  * Constructor
37  *
38  * @param documentId id of document that lock belongs
39  */
40  public Padlock(Integer documentId) {
41  this.documentId = documentId;
42  isLocked = false;
43  waitForUpdate = false;
44  readersCount = 0;
45  }
46 
47  /**
48  * Gets id of document that is locked.
49  *
50  * @return id of document that is locked
51  */
52  public Integer getDocumentId() {
53  return documentId;
54  }
55 
56  /**
57  * Method try to lock document for read, if there some other thread that locks
58  * document before, current thread will be suspended.
59  *
60  * @return returns true if current thread have lock or false if not
61  */
62  public synchronized boolean lockForRead(){
63  if(isLocked || waitForUpdate){
64  try{
65  wait();
66  }catch(InterruptedException ex){
67  //handle exception
68  return false;
69  }
70  return false;
71  }else{
72  readersCount++;
73  return true;
74  }
75  }
76 
77  /**
78  * Method unlocks document for read operations, all suspended threads will be
79  * make runable.
80  */
81  public synchronized void unlockForRead(){
82  readersCount--;
83  if(readersCount == 0 && waitForUpdate){
84  notifyAll();
85  }
86  }
87 
88  /**
89  * Method try to lock document for change, if there some other thread that locks
90  * document before, current thread will be suspended.
91  *
92  * @return returns true if current thread have lock or false if not
93  */
94  public synchronized boolean lockForChange(){
95  if(isLocked){
96  if(!waitForUpdate){
97  waitForUpdate = true;
98  }
99  }else{
100  if(readersCount > 0){
101  waitForUpdate = true;
102  }else{
103  // finnaly run
104  isLocked = true;
105  waitForUpdate = false;
106  return true;
107  }
108  }
109 
110  try{
111  wait();
112  }catch(InterruptedException ex){
113  //handle exception
114  }
115 
116  return false;
117  }
118 
119  /**
120  * Method unlocks document for read operations, all suspended threads will be
121  * make runable.
122  */
123  public synchronized void unlockForChange(){
124  isLocked = false;
125  notifyAll();
126  }
127 } // public class Padlock
Class represents the document protection, and provides methods to lock.
Definition: Padlock.java:22