corba/src/java.corba/share/classes/com/sun/corba/se/impl/orbutil/concurrent/Sync.java
author avstepan
Tue, 05 May 2015 15:17:13 +0400
changeset 30383 45960fdbe465
parent 25862 a5e25d68f971
child 30384 ff19c1d6f92a
permissions -rw-r--r--
8079075: some docs cleanup for CORBA - part 1 Summary: some fix for CORBA docs Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
  File: Sync.java
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
  Originally written by Doug Lea and released into the public domain.
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
  This may be used for any purposes whatsoever without acknowledgment.
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
  Thanks for the assistance and support of Sun Microsystems Labs,
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
  and everyone contributing, testing, and using this code.
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
  History:
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
  Date       Who                What
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
  11Jun1998  dl               Create public version
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
   5Aug1998  dl               Added some convenient time constants
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
*/
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
package com.sun.corba.se.impl.orbutil.concurrent;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
 * Main interface for locks, gates, and conditions.
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
 * Sync objects isolate waiting and notification for particular
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
 * logical states, resource availability, events, and the like that are
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
 * shared across multiple threads. Use of Syncs sometimes
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
 * (but by no means always) adds flexibility and efficiency
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
 * compared to the use of plain java monitor methods
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
 * and locking, and are sometimes (but by no means always)
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
 * simpler to program with.
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
 * Most Syncs are intended to be used primarily (although
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
 * not exclusively) in  before/after constructions such as:
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
 * class X {
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
 *   Sync gate;
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
 *   // ...
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
 *   public void m() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
 *     try {
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
 *       gate.acquire();  // block until condition holds
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
 *       try {
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
 *         // ... method body
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
 *       }
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
 *       finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
 *         gate.release()
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
 *       }
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
 *     catch (InterruptedException ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
 *       // ... evasive action
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
 *   public void m2(Sync cond) { // use supplied condition
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
 *     try {
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
 *       if (cond.attempt(10)) {         // try the condition for 10 ms
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
 *         try {
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
 *           // ... method body
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
 *         }
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
 *         finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
 *           cond.release()
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
 *         }
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
 *       }
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
 *     catch (InterruptedException ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
 *       // ... evasive action
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
 * }
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
 * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
 * Syncs may be used in somewhat tedious but more flexible replacements
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
 * for built-in Java synchronized blocks. For example:
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
 * class HandSynched {
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
 *   private double state_ = 0.0;
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
 *   private final Sync lock;  // use lock type supplied in constructor
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
 *   public HandSynched(Sync l) { lock = l; }
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
 *   public void changeState(double d) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
 *     try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
 *       lock.acquire();
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
 *       try     { state_ = updateFunction(d); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
 *       finally { lock.release(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
 *     catch(InterruptedException ex) { }
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
 *   public double getState() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
 *     double d = 0.0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
 *     try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
 *       lock.acquire();
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
 *       try     { d = accessFunction(state_); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
 *       finally { lock.release(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
 *     catch(InterruptedException ex){}
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
 *     return d;
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
 *   private double updateFunction(double d) { ... }
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
 *   private double accessFunction(double d) { ... }
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
 * }
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
 * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
 * If you have a lot of such methods, and they take a common
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
 * form, you can standardize this using wrappers. Some of these
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
 * wrappers are standardized in LockedExecutor, but you can make others.
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
 * For example:
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
 * class HandSynchedV2 {
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
 *   private double state_ = 0.0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
 *   private final Sync lock;  // use lock type supplied in constructor
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
 *   public HandSynchedV2(Sync l) { lock = l; }
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
 *   protected void runSafely(Runnable r) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
 *     try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
 *       lock.acquire();
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
 *       try { r.run(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
 *       finally { lock.release(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
 *     catch (InterruptedException ex) { // propagate without throwing
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
 *       Thread.currentThread().interrupt();
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
 *   public void changeState(double d) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
 *     runSafely(new Runnable() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
 *       public void run() { state_ = updateFunction(d); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
 *     });
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
 *   // ...
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
 * }
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
 * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
 * One reason to bother with such constructions is to use deadlock-
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
 * avoiding back-offs when dealing with locks involving multiple objects.
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
 * For example, here is a Cell class that uses attempt to back-off
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
 * and retry if two Cells are trying to swap values with each other
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
 * at the same time.
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
 * class Cell {
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
 *   long value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
 *   Sync lock = ... // some sync implementation class
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
 *   void swapValue(Cell other) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
 *     for (;;) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
 *       try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
 *         lock.acquire();
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
 *         try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
 *           if (other.lock.attempt(100)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
 *             try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
 *               long t = value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
 *               value = other.value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
 *               other.value = t;
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
 *               return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
 *             }
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
 *             finally { other.lock.release(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
 *           }
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
 *         }
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
 *         finally { lock.release(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
 *       }
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
 *       catch (InterruptedException ex) { return; }
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
 * }
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
 *</pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
 * Here is an even fancier version, that uses lock re-ordering
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
 * upon conflict:
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
 * class Cell {
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
 *   long value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
 *   Sync lock = ...;
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
 *   private static boolean trySwap(Cell a, Cell b) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
 *     a.lock.acquire();
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
 *     try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
 *       if (!b.lock.attempt(0))
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
 *         return false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
 *       try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
 *         long t = a.value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
 *         a.value = b.value;
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
 *         b.value = t;
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
 *         return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
 *       }
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
 *       finally { other.lock.release(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
 *     finally { lock.release(); }
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
 *     return false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
 *  void swapValue(Cell other) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
 *    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
 *      while (!trySwap(this, other) &&
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
 *            !tryswap(other, this))
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
 *        Thread.sleep(1);
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
 *    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
 *    catch (InterruptedException ex) { return; }
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
 *  }
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
 *}
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
 *</pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
 * Interruptions are in general handled as early as possible.
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
 * Normally, InterruptionExceptions are thrown
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
 * in acquire and attempt(msec) if interruption
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
 * is detected upon entry to the method, as well as in any
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
 * later context surrounding waits.
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
 * However, interruption status is ignored in release();
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
 * Timed versions of attempt report failure via return value.
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
 * If so desired, you can transform such constructions to use exception
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
 * throws via
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
 *   if (!c.attempt(timeval)) throw new TimeoutException(timeval);
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
 * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
 * The TimoutSync wrapper class can be used to automate such usages.
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
 * All time values are expressed in milliseconds as longs, which have a maximum
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
 * value of Long.MAX_VALUE, or almost 300,000 centuries. It is not
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
 * known whether JVMs actually deal correctly with such extreme values.
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
 * For convenience, some useful time values are defined as static constants.
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
 * All implementations of the three Sync methods guarantee to
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
 * somehow employ Java <code>synchronized</code> methods or blocks,
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
 * and so entail the memory operations described in JLS
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
 * chapter 17 which ensure that variables are loaded and flushed
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
 * within before/after constructions.
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
 * Syncs may also be used in spinlock constructions. Although
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
 * it is normally best to just use acquire(), various forms
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
 * of busy waits can be implemented. For a simple example
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
 * (but one that would probably never be preferable to using acquire()):
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
 * class X {
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
 *   Sync lock = ...
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
 *   void spinUntilAcquired() throws InterruptedException {
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
 *     // Two phase.
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
 *     // First spin without pausing.
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
 *     int purespins = 10;
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
 *     for (int i = 0; i < purespins; ++i) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
 *       if (lock.attempt(0))
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
 *         return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
 *     // Second phase - use timed waits
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
 *     long waitTime = 1; // 1 millisecond
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
 *     for (;;) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
 *       if (lock.attempt(waitTime))
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
 *         return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
 *       else
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
 *         waitTime = waitTime * 3 / 2 + 1; // increase 50%
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
 *     }
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
 *   }
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
 * }
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
 * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
 * In addition pure synchronization control, Syncs
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
 * may be useful in any context requiring before/after methods.
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
 * For example, you can use an ObservableSync
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
 * (perhaps as part of a LayeredSync) in order to obtain callbacks
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
 * before and after each method invocation for a given class.
30383
45960fdbe465 8079075: some docs cleanup for CORBA - part 1
avstepan
parents: 25862
diff changeset
   279
 *
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
30383
45960fdbe465 8079075: some docs cleanup for CORBA - part 1
avstepan
parents: 25862
diff changeset
   281
 **/
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
public interface Sync {
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
   *  Wait (possibly forever) until successful passage.
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
   *  Fail only upon interuption. Interruptions always result in
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
   *  `clean' failures. On failure,  you can be sure that it has not
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
   *  been acquired, and that no
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
   *  corresponding release should be performed. Conversely,
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
   *  a normal return guarantees that the acquire was successful.
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
  **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
  public void acquire() throws InterruptedException;
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
   * Wait at most msecs to pass; report whether passed.
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
   * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
   * The method has best-effort semantics:
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
   * The msecs bound cannot
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
   * be guaranteed to be a precise upper bound on wait time in Java.
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
   * Implementations generally can only attempt to return as soon as possible
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
   * after the specified bound. Also, timers in Java do not stop during garbage
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
   * collection, so timeouts can occur just because a GC intervened.
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
   * So, msecs arguments should be used in
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
   * a coarse-grained manner. Further,
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
   * implementations cannot always guarantee that this method
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
   * will return at all without blocking indefinitely when used in
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
   * unintended ways. For example, deadlocks may be encountered
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
   * when called in an unintended context.
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
   * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
   * @param msecs the number of milleseconds to wait.
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
   * An argument less than or equal to zero means not to wait at all.
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
   * However, this may still require
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
   * access to a synchronization lock, which can impose unbounded
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
   * delay if there is a lot of contention among threads.
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
   * @return true if acquired
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
  **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
  public boolean attempt(long msecs) throws InterruptedException;
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
  /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
   * Potentially enable others to pass.
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
   * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
   * Because release does not raise exceptions,
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
   * it can be used in `finally' clauses without requiring extra
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
   * embedded try/catch blocks. But keep in mind that
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
   * as with any java method, implementations may
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
   * still throw unchecked exceptions such as Error or NullPointerException
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
   * when faced with uncontinuable errors. However, these should normally
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
   * only be caught by higher-level error handlers.
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
  **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
  public void release();
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
  /**  One second, in milliseconds; convenient as a time-out value **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
  public static final long ONE_SECOND = 1000;
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
  /**  One minute, in milliseconds; convenient as a time-out value **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
  public static final long ONE_MINUTE = 60 * ONE_SECOND;
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
  /**  One hour, in milliseconds; convenient as a time-out value **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
  public static final long ONE_HOUR = 60 * ONE_MINUTE;
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
  /**  One day, in milliseconds; convenient as a time-out value **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
  public static final long ONE_DAY = 24 * ONE_HOUR;
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
  /**  One week, in milliseconds; convenient as a time-out value **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
  public static final long ONE_WEEK = 7 * ONE_DAY;
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
  /**  One year in milliseconds; convenient as a time-out value  **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
  // Not that it matters, but there is some variation across
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
  // standard sources about value at msec precision.
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
  // The value used is the same as in java.util.GregorianCalendar
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
  public static final long ONE_YEAR = (long)(365.2425 * ONE_DAY);
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
  /**  One century in milliseconds; convenient as a time-out value **/
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
  public static final long ONE_CENTURY = 100 * ONE_YEAR;
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
}