hotspot/src/share/vm/runtime/semaphore.cpp
author rprotacio
Wed, 02 Mar 2016 15:10:38 -0500
changeset 36396 26a241a959de
parent 31610 b05ea6f92971
permissions -rw-r--r--
8145098: JNI GetVersion should return JNI_VERSION_9 Summary: Updated JNI_VERSION for current version to be JNI_VERSION_9 Reviewed-by: hseigel, gtriantafill, dholmes, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31610
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     1
/*
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    25
#include "precompiled.hpp"
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    26
#include "utilities/debug.hpp"
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    27
#include "runtime/semaphore.hpp"
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    28
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    29
/////////////// Unit tests ///////////////
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    30
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    31
#ifndef PRODUCT
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    32
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    33
static void test_semaphore_single_separate(uint count) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    34
  Semaphore sem(0);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    35
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    36
  for (uint i = 0; i < count; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    37
    sem.signal();
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
  for (uint i = 0; i < count; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    41
    sem.wait();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    42
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    43
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    44
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    45
static void test_semaphore_single_combined(uint count) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    46
  Semaphore sem(0);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    47
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    48
  for (uint i = 0; i < count; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    49
    sem.signal();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    50
    sem.wait();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    51
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    52
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    53
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    54
static void test_semaphore_many(uint value, uint max, uint increments) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    55
  Semaphore sem(value);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    56
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    57
  uint total = value;
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    58
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    59
  for (uint i = value; i + increments <= max; i += increments) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    60
    sem.signal(increments);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    61
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    62
    total += increments;
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    63
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    64
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    65
  for (uint i = 0; i < total; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    66
    sem.wait();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    67
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    68
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    69
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    70
static void test_semaphore_many() {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    71
  for (uint max = 0; max < 10; max++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    72
    for (uint value = 0; value < max; value++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    73
      for (uint inc = 1; inc <= max - value; inc++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    74
        test_semaphore_many(value, max, inc);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    75
      }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    76
    }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    77
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    78
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    79
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    80
void test_semaphore() {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    81
  for (uint i = 1; i < 10; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    82
    test_semaphore_single_separate(i);
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
  for (uint i = 0; i < 10; i++) {
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    86
    test_semaphore_single_combined(i);
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    87
  }
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    88
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    89
  test_semaphore_many();
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    90
}
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    91
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    92
#endif // PRODUCT
b05ea6f92971 8087322: Implement a Semaphore utility class
stefank
parents:
diff changeset
    93