< prev index next >

src/share/vm/utilities/semaphore.cpp

Print this page




  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 


  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_separate(uint count) {
  34   Semaphore sem(0);
  35 
  36   for (uint i = 0; i < count; i++) {











  37     sem.signal();
  38   }
  39 
  40   for (uint i = 0; i < count; i++) {
  41     sem.wait();
  42   }
  43 }
  44 
  45 static void test_semaphore_single_combined(uint count) {
  46   Semaphore sem(0);


  47 
  48   for (uint i = 0; i < count; i++) {






















  49     sem.signal();
  50     sem.wait();
  51   }
  52 }
  53 
  54 static void test_semaphore_many(uint value, uint max, uint increments) {
  55   Semaphore sem(value);
  56 
  57   uint total = value;
  58 
  59   for (uint i = value; i + increments <= max; i += increments) {
  60     sem.signal(increments);
  61 
  62     total = i;
  63   }
  64 
  65   for (uint i = 0; i < total; i++) {
  66     sem.wait();
  67   }
  68 }
  69 
  70 static void test_semaphore_many() {
  71   for (uint max = 0; max < 10; max++) {
  72     for (uint value = 0; value < max; value++) {
  73       for (uint inc = 1; inc <= max - value; inc++) {
  74         test_semaphore_many(value, max, inc);
  75       }
  76     }
  77   }
  78 }
  79 
  80 void test_semaphore() {
  81   for (uint i = 1; i < 10; i++) {
  82     test_semaphore_single_separate(i);


  83   }
  84 
  85   for (uint i = 0; i < 10; i++) {
  86     test_semaphore_single_combined(i);
  87   }


  88 
  89   test_semaphore_many();
  90 }
  91 
  92 #endif // PRODUCT
  93 
< prev index next >