Path: blob/master/test/hotspot/gtest/runtime/test_semaphore.cpp
41144 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "runtime/semaphore.hpp"25#include "unittest.hpp"2627static void test_semaphore_single_separate(uint count) {28Semaphore sem(0);2930for (uint i = 0; i < count; i++) {31sem.signal();32}3334for (uint i = 0; i < count; i++) {35sem.wait();36}37}3839static void test_semaphore_single_combined(uint count) {40Semaphore sem(0);4142for (uint i = 0; i < count; i++) {43sem.signal();44sem.wait();45}46}4748static void test_semaphore_many(uint value, uint max, uint increments) {49Semaphore sem(value);5051uint total = value;5253for (uint i = value; i + increments <= max; i += increments) {54sem.signal(increments);5556total += increments;57}5859for (uint i = 0; i < total; i++) {60sem.wait();61}62}6364static void test_semaphore_trywait(uint value, uint max) {65Semaphore sem(value);6667for (uint i = 0; i < max; ++i) {68if (i < value) {69ASSERT_EQ(sem.trywait(), true);70} else {71ASSERT_EQ(sem.trywait(), false);72}73}74}7576TEST(Semaphore, single_separate) {77for (uint i = 1; i < 10; i++) {78test_semaphore_single_separate(i);79}80}8182TEST(Semaphore, single_combined) {83for (uint i = 1; i < 10; i++) {84test_semaphore_single_combined(i);85}86}8788TEST(Semaphore, many) {89for (uint max = 0; max < 10; max++) {90for (uint value = 0; value < max; value++) {91for (uint inc = 1; inc <= max - value; inc++) {92test_semaphore_many(value, max, inc);93}94}95}96}9798TEST(Semaphore, trywait) {99for (uint max = 0; max < 10; max++) {100for (uint value = 0; value < max; value++) {101test_semaphore_trywait(value, max);102}103}104}105106107