1 /*
   2  * Copyright (c) 2015, 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 
  25 #include "precompiled.hpp"
  26 #include "utilities/debug.hpp"
  27 #include "utilities/semaphore.hpp"
  28 
  29 /////////////// Unit tests ///////////////
  30 
  31 #ifndef PRODUCT
  32 
  33 #if IMPLEMENTS_SEMAPHORE_CLASS
  34 static void test_semaphore_single_with_max_once(uint max) {
  35   assert(max > 0, "precondition");
  36 
  37   Semaphore sem(0, max);
  38 
  39   sem.signal();
  40   sem.wait();
  41 }
  42 
  43 static void test_semaphore_single_with_max_one_less(uint max) {
  44   assert(max > 0, "precondition");
  45 
  46   Semaphore sem(0, max);
  47 
  48   for (uint i = 0; i < max -1; i++) {
  49     sem.signal();
  50   }
  51 
  52   for (uint i = 0; i < max -1; i++) {
  53     sem.wait();
  54   }
  55 }
  56 
  57 static void test_semaphore_single_with_max_all(uint max) {
  58   assert(max > 0, "precondition");
  59 
  60   Semaphore sem(0, max);
  61 
  62   for (uint i = 0; i < max; i++) {
  63     sem.signal();
  64   }
  65 
  66   for (uint i = 0; i < max; i++) {
  67     sem.wait();
  68   }
  69 }
  70 
  71 static void test_semaphore_single_with_value(uint value, uint max) {
  72   assert(max > 0, "precondition");
  73 
  74   Semaphore sem(value, max);
  75 
  76   for (uint i = 0; i < value; i++) {
  77     sem.wait();
  78   }
  79 }
  80 
  81 static void test_semaphore_single_multiple() {
  82   Semaphore sem(0, 1);
  83 
  84   for (uint i = 0; i < 100; i++) {
  85     sem.signal();
  86     sem.wait();
  87   }
  88 }
  89 
  90 static void test_semaphore_many(uint value, uint max, uint increments) {
  91   Semaphore sem(value, max);
  92 
  93   uint total = value;
  94 
  95   for (uint i = value; i + increments <= max; i += increments) {
  96     sem.signal(increments);
  97 
  98     total = i;
  99   }
 100 
 101   for (uint i = 0; i < total; i++) {
 102     sem.wait();
 103   }
 104 }
 105 
 106 static void test_semaphore_many() {
 107   for (uint max = 0; max < 10; max++) {
 108     for (uint value = 0; value < max; value++) {
 109       for (uint inc = 1; inc <= max - value; inc++) {
 110         test_semaphore_many(value, max, inc);
 111       }
 112     }
 113   }
 114 }
 115 
 116 void test_semaphore() {
 117   for (uint i = 1; i < 10; i++) {
 118     test_semaphore_single_with_max_once(i);
 119     test_semaphore_single_with_max_one_less(i);
 120     test_semaphore_single_with_max_all(i);
 121   }
 122 
 123   for (uint i = 0; i < 10; i++) {
 124     test_semaphore_single_with_value(i, 9 /* arbitrary max */);
 125   }
 126 
 127   test_semaphore_single_multiple();
 128 
 129   test_semaphore_many();
 130 }
 131 
 132 #else // IMPLEMENTS_SEMAPHORE_CLASS
 133 void test_semaphore() {}
 134 #endif
 135 
 136 #endif // PRODUCT
 137