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