hotspot/src/share/vm/runtime/semaphore.cpp
changeset 31610 b05ea6f92971
equal deleted inserted replaced
31609:a02d832f2880 31610:b05ea6f92971
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 
       
    25 #include "precompiled.hpp"
       
    26 #include "utilities/debug.hpp"
       
    27 #include "runtime/semaphore.hpp"
       
    28 
       
    29 /////////////// Unit tests ///////////////
       
    30 
       
    31 #ifndef PRODUCT
       
    32 
       
    33 static void test_semaphore_single_separate(uint count) {
       
    34   Semaphore sem(0);
       
    35 
       
    36   for (uint i = 0; i < count; i++) {
       
    37     sem.signal();
       
    38   }
       
    39 
       
    40   for (uint i = 0; i < count; i++) {
       
    41     sem.wait();
       
    42   }
       
    43 }
       
    44 
       
    45 static void test_semaphore_single_combined(uint count) {
       
    46   Semaphore sem(0);
       
    47 
       
    48   for (uint i = 0; i < count; i++) {
       
    49     sem.signal();
       
    50     sem.wait();
       
    51   }
       
    52 }
       
    53 
       
    54 static void test_semaphore_many(uint value, uint max, uint increments) {
       
    55   Semaphore sem(value);
       
    56 
       
    57   uint total = value;
       
    58 
       
    59   for (uint i = value; i + increments <= max; i += increments) {
       
    60     sem.signal(increments);
       
    61 
       
    62     total += increments;
       
    63   }
       
    64 
       
    65   for (uint i = 0; i < total; i++) {
       
    66     sem.wait();
       
    67   }
       
    68 }
       
    69 
       
    70 static void test_semaphore_many() {
       
    71   for (uint max = 0; max < 10; max++) {
       
    72     for (uint value = 0; value < max; value++) {
       
    73       for (uint inc = 1; inc <= max - value; inc++) {
       
    74         test_semaphore_many(value, max, inc);
       
    75       }
       
    76     }
       
    77   }
       
    78 }
       
    79 
       
    80 void test_semaphore() {
       
    81   for (uint i = 1; i < 10; i++) {
       
    82     test_semaphore_single_separate(i);
       
    83   }
       
    84 
       
    85   for (uint i = 0; i < 10; i++) {
       
    86     test_semaphore_single_combined(i);
       
    87   }
       
    88 
       
    89   test_semaphore_many();
       
    90 }
       
    91 
       
    92 #endif // PRODUCT
       
    93