--- /dev/null 2015-05-29 12:41:18.476004839 +0200 +++ new/src/share/vm/utilities/semaphore.cpp 2015-06-12 14:46:30.948219399 +0200 @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#include "precompiled.hpp" +#include "utilities/debug.hpp" +#include "utilities/semaphore.hpp" + +/////////////// Unit tests /////////////// + +#ifndef PRODUCT + +#if IMPLEMENTS_SEMAPHORE_CLASS +static void test_semaphore_single_with_max_once(uint max) { + assert(max > 0, "precondition"); + + Semaphore sem(0, max); + + sem.signal(); + sem.wait(); +} + +static void test_semaphore_single_with_max_one_less(uint max) { + assert(max > 0, "precondition"); + + Semaphore sem(0, max); + + for (uint i = 0; i < max -1; i++) { + sem.signal(); + } + + for (uint i = 0; i < max -1; i++) { + sem.wait(); + } +} + +static void test_semaphore_single_with_max_all(uint max) { + assert(max > 0, "precondition"); + + Semaphore sem(0, max); + + for (uint i = 0; i < max; i++) { + sem.signal(); + } + + for (uint i = 0; i < max; i++) { + sem.wait(); + } +} + +static void test_semaphore_single_with_value(uint value, uint max) { + assert(max > 0, "precondition"); + + Semaphore sem(value, max); + + for (uint i = 0; i < value; i++) { + sem.wait(); + } +} + +static void test_semaphore_single_multiple() { + Semaphore sem(0, 1); + + for (uint i = 0; i < 100; i++) { + sem.signal(); + sem.wait(); + } +} + +static void test_semaphore_many(uint value, uint max, uint increments) { + Semaphore sem(value, max); + + uint total = value; + + for (uint i = value; i + increments <= max; i += increments) { + sem.signal(increments); + + total = i; + } + + for (uint i = 0; i < total; i++) { + sem.wait(); + } +} + +static void test_semaphore_many() { + for (uint max = 0; max < 10; max++) { + for (uint value = 0; value < max; value++) { + for (uint inc = 1; inc <= max - value; inc++) { + test_semaphore_many(value, max, inc); + } + } + } +} + +void test_semaphore() { + for (uint i = 1; i < 10; i++) { + test_semaphore_single_with_max_once(i); + test_semaphore_single_with_max_one_less(i); + test_semaphore_single_with_max_all(i); + } + + for (uint i = 0; i < 10; i++) { + test_semaphore_single_with_value(i, 9 /* arbitrary max */); + } + + test_semaphore_single_multiple(); + + test_semaphore_many(); +} + +#else // IMPLEMENTS_SEMAPHORE_CLASS +void test_semaphore() {} +#endif + +#endif // PRODUCT +