jdk/src/share/classes/sun/nio/ch/NativeThreadSet.java
changeset 2057 3acf8e5e2ca0
parent 715 f16baef3a20e
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
     1 /*
     1 /*
     2  * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2002-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
    31 
    31 
    32 class NativeThreadSet {
    32 class NativeThreadSet {
    33 
    33 
    34     private long[] elts;
    34     private long[] elts;
    35     private int used = 0;
    35     private int used = 0;
       
    36     private boolean waitingToEmpty;
    36 
    37 
    37     NativeThreadSet(int n) {
    38     NativeThreadSet(int n) {
    38         elts = new long[n];
    39         elts = new long[n];
    39     }
    40     }
    40 
    41 
    73         if (i < 0)
    74         if (i < 0)
    74             return;
    75             return;
    75         synchronized (this) {
    76         synchronized (this) {
    76             elts[i] = 0;
    77             elts[i] = 0;
    77             used--;
    78             used--;
       
    79             if (used == 0 && waitingToEmpty)
       
    80                 notifyAll();
    78         }
    81         }
    79     }
    82     }
    80 
    83 
    81     // Signals all threads in this set.
    84     // Signals all threads in this set.
    82     //
    85     //
    83     void signal() {
    86     void signalAndWait() {
    84         synchronized (this) {
    87         synchronized (this) {
    85             int u = used;
    88             int u = used;
    86             int n = elts.length;
    89             int n = elts.length;
    87             for (int i = 0; i < n; i++) {
    90             for (int i = 0; i < n; i++) {
    88                 long th = elts[i];
    91                 long th = elts[i];
    90                     continue;
    93                     continue;
    91                 NativeThread.signal(th);
    94                 NativeThread.signal(th);
    92                 if (--u == 0)
    95                 if (--u == 0)
    93                     break;
    96                     break;
    94             }
    97             }
       
    98             waitingToEmpty = true;
       
    99             while (used > 0) {
       
   100                 try {
       
   101                     wait();
       
   102                 } catch (InterruptedException ignore) { }
       
   103             }
    95         }
   104         }
    96     }
   105     }
    97 
       
    98 }
   106 }