View Javadoc
1   //******************************************************************************
2   //
3   // File:    SharedObject.java
4   // Package: edu.rit.pj.reduction
5   // Unit:    Class edu.rit.pj.reduction.SharedObject
6   //
7   // This Java source file is copyright (C) 2007 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.pj.reduction;
41  
42  import java.util.concurrent.atomic.AtomicReference;
43  
44  /**
45   * Class SharedObject provides a reduction variable for a value of an object
46   * type.
47   * <P>
48   * Class SharedObject is multiple thread safe. The methods use lock-free atomic
49   * compare-and-set.
50   * <P>
51   * <I>Note:</I> Class SharedObject is implemented using class
52   * java.util.concurrent.atomic.AtomicReference.
53   *
54   * @param <T> Object data type.
55   * @author Alan Kaminsky
56   * @version 20-Jun-2007
57   */
58  public class SharedObject<T> {
59  
60  // Hidden data members.
61      private AtomicReference<T> myValue;
62  
63  // Exported constructors.
64      /**
65       * Construct a new object reduction variable with the initial value null.
66       */
67      public SharedObject() {
68          myValue = new AtomicReference<T>();
69      }
70  
71      /**
72       * Construct a new object reduction variable with the given initial value.
73       *
74       * @param initialValue Initial value.
75       */
76      public SharedObject(T initialValue) {
77          myValue = new AtomicReference<T>(initialValue);
78      }
79  
80  // Exported operations.
81      /**
82       * Returns this reduction variable's current value.
83       *
84       * @return Current value.
85       */
86      public T get() {
87          return myValue.get();
88      }
89  
90      /**
91       * Set this reduction variable to the given value.
92       *
93       * @param value New value.
94       */
95      public void set(T value) {
96          myValue.set(value);
97      }
98  
99      /**
100      * Set this reduction variable to the given value and return the previous
101      * value.
102      *
103      * @param value New value.
104      * @return Previous value.
105      */
106     public T getAndSet(T value) {
107         return myValue.getAndSet(value);
108     }
109 
110     /**
111      * Atomically set this reduction variable to the given updated value if the
112      * current value equals the expected value.
113      *
114      * @param expect Expected value.
115      * @param update Updated value.
116      * @return True if the update happened, false otherwise.
117      */
118     public boolean compareAndSet(T expect,
119             T update) {
120         return myValue.compareAndSet(expect, update);
121     }
122 
123     /**
124      * Atomically set this reduction variable to the given updated value if the
125      * current value equals the expected value. May fail spuriously.
126      *
127      * @param expect Expected value.
128      * @param update Updated value.
129      * @return True if the update happened, false otherwise.
130      */
131     @SuppressWarnings("deprecation")
132     public boolean weakCompareAndSet(T expect,
133             T update) {
134         return myValue.weakCompareAndSet(expect, update);
135     }
136 
137     /**
138      * Combine this reduction variable with the given value using the given
139      * operation. The result is stored back into this reduction variable and is
140      * returned.
141      *
142      * @param value Value.
143      * @param op Binary operation.
144      * @return (This variable) <I>op</I> (<code>value</code>).
145      */
146     public T reduce(T value,
147             ObjectOp<T> op) {
148         for (;;) {
149             T oldvalue = myValue.get();
150             T newvalue = op.op(oldvalue, value);
151             if (myValue.compareAndSet(oldvalue, newvalue)) {
152                 return newvalue;
153             }
154         }
155     }
156 
157     /**
158      * Returns a string version of this reduction variable.
159      *
160      * @return String version.
161      */
162     public String toString() {
163         return myValue.toString();
164     }
165 
166 }