summaryrefslogtreecommitdiff
path: root/java/JACE/tests/Concurrency/RWMutexTest.java
blob: ff2bbc1f3960bea21f49eeb458dff230ee7f3d1d (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*************************************************
 *
 * = PACKAGE
 *    JACE.tests.Concurrency
 *
 * = FILENAME
 *    RWMutexTest.java
 *
 *@author Ross Dargahi (rossd@krinfo.com)
 *
 *************************************************/
package JACE.tests.Concurrency;

import JACE.OS.*;
import JACE.Concurrency.*;

public class RWMutexTest
{
  static class TestThread extends Thread
  {
    TestThread(String  name,
	       boolean writer,
	       AbstractLock lock)
    {
      super (name);
      mWriter = writer;
      mLock = lock;
    }

    public void output (String msg)
    {
      synchronized (iosynch) {
	ACE.DEBUG (msg);
      }
    }

    public void run()
    {
      for (int i = 0; i < 10; i++)
	{
	  try
	    {
	      if (!mWriter)
		{
		  mLock.acquireRead();
		  output (getName() + ": Acquired Read Lock");
		
		  int sleepTime = 10;
		  sleep (sleepTime);

		  mLock.release ();
		  output (getName () + ": Released Read Lock");
		}
	      else
		{
		  mLock.acquireWrite ();
		  output (getName () + ": Acquired Write Lock");
		
		  int sleepTime = 10;
		  sleep (sleepTime);

		  mLock.release ();
		  output (getName () + ": Released Write Lock");
		}
	    }
	  catch (InterruptedException ex)
	    {
	      ACE.ERROR ("InterruptedException");
	    }
	  catch (LockException ex) 
	    {
	      ACE.ERROR ("LockException: " + ex.getMessage ());
	    }
	}
    }

    AbstractLock  mLock;
    boolean mWriter;

    static Object iosynch = new Object ();
  }

  /**
   * Command line arguments:
   *
   * Optional class name to use for the tests (must implement the
   * AbstractLock interface).  Followed by an optional number of
   * iterations.
   */
  public static void main(String [] args)
    throws ClassNotFoundException,
	   IllegalAccessException,
	   InstantiationException,
	   InterruptedException,
	   NumberFormatException
  {
    AbstractLock lock;
    int iterations = 1;

    ACE.enableDebugging ();

    if (args.length > 0) {
      ACE.DEBUG("Using class " + args[0] + " as the Lock");
      
      lock = (AbstractLock)(Class.forName (args[0]).newInstance ());

      if (args.length > 1) 
	iterations = Integer.parseInt (args[1]);
	
    } else
      lock = new RWMutex ();
 
    for (int i = 0; i < iterations; i++) {
  
      ACE.DEBUG("Iteration " + (i + 1));

      TestThread t1 = new TestThread ("1", false, lock);
      TestThread t2 = new TestThread ("2", false, lock);
      TestThread t3 = new TestThread ("3", false, lock);
      TestThread t4 = new TestThread ("4", true, lock);
      TestThread t5 = new TestThread ("5", false, lock);
      TestThread t6 = new TestThread ("6", false, lock);
      TestThread t7 = new TestThread ("7", false, lock);
      TestThread t8 = new TestThread ("8", true, lock);

      t1.start ();
      t2.start ();
      t3.start ();
      t4.start ();
      t5.start ();
      t6.start ();
      t7.start ();
      t8.start ();
    }
  }
}