hotspot/test/native/runtime/test_semaphore.cpp
author hseigel
Wed, 01 Mar 2017 08:00:02 -0500
changeset 46194 5596e6f63072
parent 42627 0e7a86ac37c2
child 46610 d3e75e23b534
permissions -rw-r--r--
8172307: Remove ununsed JVM API JVM_GetModuleByPackageName() Summary: Remove get_module_by_package_name() etc., and unneeded test. Reviewed-by: sspitsyn, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31610
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     1
/*
42613
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
31610
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     4
 *
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     8
 *
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    13
 * accompanied this code).
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    14
 *
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    18
 *
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    21
 * questions.
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    22
 */
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    23
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    24
#include "precompiled.hpp"
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    25
#include "runtime/semaphore.hpp"
42613
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    26
#include "unittest.hpp"
31610
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    27
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    28
static void test_semaphore_single_separate(uint count) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    29
  Semaphore sem(0);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    30
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    31
  for (uint i = 0; i < count; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    32
    sem.signal();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    33
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    34
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    35
  for (uint i = 0; i < count; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    36
    sem.wait();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    37
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    38
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    39
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    40
static void test_semaphore_single_combined(uint count) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    41
  Semaphore sem(0);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    42
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    43
  for (uint i = 0; i < count; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    44
    sem.signal();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    45
    sem.wait();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    46
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    47
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    48
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    49
static void test_semaphore_many(uint value, uint max, uint increments) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    50
  Semaphore sem(value);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    51
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    52
  uint total = value;
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    53
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    54
  for (uint i = value; i + increments <= max; i += increments) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    55
    sem.signal(increments);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    56
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    57
    total += increments;
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    58
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    59
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    60
  for (uint i = 0; i < total; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    61
    sem.wait();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    62
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    63
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    64
42613
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    65
TEST(Semaphore, single_separate) {
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    66
  for (uint i = 1; i < 10; i++) {
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    67
    test_semaphore_single_separate(i);
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    68
  }
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    69
}
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    70
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    71
TEST(Semaphore, single_combined) {
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    72
  for (uint i = 1; i < 10; i++) {
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    73
    test_semaphore_single_combined(i);
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    74
  }
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    75
}
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    76
29c73ac33ad3 8166156: Convert test_semaphore to GTest
kzhaldyb
parents: 31610
diff changeset
    77
TEST(Semaphore, many) {
31610
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    78
  for (uint max = 0; max < 10; max++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    79
    for (uint value = 0; value < max; value++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    80
      for (uint inc = 1; inc <= max - value; inc++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    81
        test_semaphore_many(value, max, inc);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    82
      }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    83
    }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    84
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    85
}