hotspot/test/native/runtime/test_semaphore.cpp
changeset 42613 29c73ac33ad3
parent 31610 b05ea6f92971
child 42627 0e7a86ac37c2
equal deleted inserted replaced
42612:87ca7feb68bf 42613:29c73ac33ad3
       
     1 /*
       
     2  * Copyright (c) 2015, 2016, 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 #include "precompiled.hpp"
       
    25 #include "runtime/semaphore.hpp"
       
    26 #include "unittest.hpp"
       
    27 
       
    28 static void test_semaphore_single_separate(uint count) {
       
    29   Semaphore sem(0);
       
    30 
       
    31   for (uint i = 0; i < count; i++) {
       
    32     sem.signal();
       
    33   }
       
    34 
       
    35   for (uint i = 0; i < count; i++) {
       
    36     sem.wait();
       
    37   }
       
    38 }
       
    39 
       
    40 static void test_semaphore_single_combined(uint count) {
       
    41   Semaphore sem(0);
       
    42 
       
    43   for (uint i = 0; i < count; i++) {
       
    44     sem.signal();
       
    45     sem.wait();
       
    46   }
       
    47 }
       
    48 
       
    49 static void test_semaphore_many(uint value, uint max, uint increments) {
       
    50   Semaphore sem(value);
       
    51 
       
    52   uint total = value;
       
    53 
       
    54   for (uint i = value; i + increments <= max; i += increments) {
       
    55     sem.signal(increments);
       
    56 
       
    57     total += increments;
       
    58   }
       
    59 
       
    60   for (uint i = 0; i < total; i++) {
       
    61     sem.wait();
       
    62   }
       
    63 }
       
    64 
       
    65 TEST(Semaphore, single_separate) {
       
    66   for (uint i = 1; i < 10; i++) {
       
    67     test_semaphore_single_separate(i);
       
    68   }
       
    69 }
       
    70 
       
    71 TEST(Semaphore, single_combined) {
       
    72   for (uint i = 1; i < 10; i++) {
       
    73     test_semaphore_single_combined(i);
       
    74   }
       
    75 }
       
    76 
       
    77 TEST(Semaphore, many) {
       
    78   for (uint max = 0; max < 10; max++) {
       
    79     for (uint value = 0; value < max; value++) {
       
    80       for (uint inc = 1; inc <= max - value; inc++) {
       
    81         test_semaphore_many(value, max, inc);
       
    82       }
       
    83     }
       
    84   }
       
    85 }