Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: marauroa/RWLock.java


1   /* $Id: RWLock.java,v 1.2 2003/12/08 01:06:29 arianne_rpg Exp $ */
2   /***************************************************************************
3    *                      (C) Copyright 2003 - Marauroa                      *
4    ***************************************************************************
5    ***************************************************************************
6    *                                                                         *
7    *   This program is free software; you can redistribute it and/or modify  *
8    *   it under the terms of the GNU General Public License as published by  *
9    *   the Free Software Foundation; either version 2 of the License, or     *
10   *   (at your option) any later version.                                   *
11   *                                                                         *
12   ***************************************************************************/
13  package marauroa;
14  
15  /** This class is a Reader/Writters lock 
16   *  A Reader Writer Lock is a synchronization mechanism allowing access to data.
17   *  It allows multiple threads to read the data simultaneously, but only one 
18   *  thread at a time to update it. While a thread is updating, no other thread 
19   *  can read the data. The name is misleading. It may cause you to think there
20   *  are two locks; in reality there is a single lock that restricts both reading
21   *  and writing.*/
22  public class RWLock
23    {
24    private int givenLocks;
25    private int waitingWriters;
26      
27    private Object mutex;
28  
29    public RWLock()
30    {
31    mutex = new Object();
32    givenLocks = 0;
33    waitingWriters = 0;
34    }
35  
36    public void requestReadLock()
37    {
38      synchronized(mutex)
39        {
40        try
41       {
42       while((givenLocks == -1) || (waitingWriters != 0))
43        {
44          mutex.wait();
45        }
46      }
47      catch(java.lang.InterruptedException e)
48       {
49      System.out.println(e);
50      }
51        
52      givenLocks++;
53        }
54    }
55    
56    public void requestWriteLock()
57      {
58      synchronized(mutex)
59        {
60      waitingWriters++;
61      try
62        {
63        while(givenLocks != 0)
64        {
65        mutex.wait();
66        }
67        }
68      catch(java.lang.InterruptedException e)
69        {
70        System.out.println(e);
71        }
72      
73      waitingWriters--;
74      givenLocks = -1;
75      }
76      }
77    
78    
79    public void releaseLock()
80    {  
81     synchronized(mutex)
82      {
83        if(givenLocks == 0)
84          {
85          return;
86          }
87        
88        if(givenLocks == -1)
89          {
90      givenLocks = 0;
91      }
92        else
93          {
94      givenLocks--;
95      }
96        
97        mutex.notifyAll(); 
98      }
99    }
100   }
101