test/hotspot/gtest/utilities/test_globalCounter_nested.cpp
author stefank
Mon, 25 Nov 2019 12:22:13 +0100
changeset 59247 56bf71d64d51
parent 52332 d2a3503c72f7
permissions -rw-r--r--
8234562: Move OrderAccess::release_store*/load_acquire to Atomic Reviewed-by: rehn, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     1
/*
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     4
 *
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     8
 *
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    14
 *
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    18
 *
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    21
 * questions.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    22
 */
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    23
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    24
#include "precompiled.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    25
#include "metaprogramming/isRegisteredEnum.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    26
#include "runtime/atomic.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    27
#include "runtime/os.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    28
#include "utilities/globalCounter.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    29
#include "utilities/globalCounter.inline.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    30
#include "utilities/spinYield.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    31
#include "threadHelper.inline.hpp"
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    32
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    33
enum NestedTestState {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    34
  START,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    35
  START_WAIT,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    36
  OUTER_ENTERED,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    37
  INNER_ENTERED,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    38
  INNER_EXITED,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    39
  OUTER_EXITED,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    40
  SYNCHRONIZING,
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    41
  SYNCHRONIZED
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    42
};
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    43
template<> struct IsRegisteredEnum<NestedTestState> : public TrueType {};
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    44
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    45
class RCUNestedThread : public JavaTestThread {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    46
  volatile NestedTestState _state;
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    47
  volatile bool _proceed;
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    48
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    49
protected:
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    50
  RCUNestedThread(Semaphore* post) :
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    51
    JavaTestThread(post),
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    52
    _state(START),
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    53
    _proceed(false)
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    54
  {}
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    55
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    56
  ~RCUNestedThread() {}
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    57
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    58
  void set_state(NestedTestState new_state) {
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 52332
diff changeset
    59
    Atomic::release_store(&_state, new_state);
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    60
  }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    61
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    62
  void wait_with_state(NestedTestState new_state) {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    63
    SpinYield spinner;
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 52332
diff changeset
    64
    Atomic::release_store(&_state, new_state);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 52332
diff changeset
    65
    while (!Atomic::load_acquire(&_proceed)) {
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    66
      spinner.wait();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    67
    }
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 52332
diff changeset
    68
    Atomic::release_store(&_proceed, false);
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    69
  }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    70
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    71
public:
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    72
  NestedTestState state() const {
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 52332
diff changeset
    73
    return Atomic::load_acquire(&_state);
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    74
  }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    75
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    76
  void wait_for_state(NestedTestState goal) {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    77
    SpinYield spinner;
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    78
    while (state() != goal) {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    79
      spinner.wait();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    80
    }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    81
  }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    82
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    83
  void proceed() {
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 52332
diff changeset
    84
    Atomic::release_store(&_proceed, true);
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    85
  }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    86
};
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    87
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    88
class RCUNestedReaderThread : public RCUNestedThread {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    89
public:
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    90
  RCUNestedReaderThread(Semaphore* post) :
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    91
    RCUNestedThread(post)
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    92
  {}
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    93
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    94
  virtual void main_run();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    95
};
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    96
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    97
void RCUNestedReaderThread::main_run() {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    98
  wait_with_state(START_WAIT);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
    99
  {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   100
    GlobalCounter::CriticalSection outer(Thread::current());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   101
    wait_with_state(OUTER_ENTERED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   102
    {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   103
      GlobalCounter::CriticalSection inner(Thread::current());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   104
      wait_with_state(INNER_ENTERED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   105
    }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   106
    wait_with_state(INNER_EXITED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   107
  }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   108
  wait_with_state(OUTER_EXITED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   109
}
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   110
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   111
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   112
class RCUNestedWriterThread : public RCUNestedThread {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   113
public:
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   114
  RCUNestedWriterThread(Semaphore* post) :
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   115
    RCUNestedThread(post)
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   116
  {}
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   117
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   118
  virtual void main_run();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   119
};
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   120
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   121
void RCUNestedWriterThread::main_run() {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   122
  wait_with_state(START_WAIT);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   123
  set_state(SYNCHRONIZING);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   124
  GlobalCounter::write_synchronize();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   125
  wait_with_state(SYNCHRONIZED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   126
}
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   127
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   128
TEST_VM(GlobalCounter, nested_critical_section) {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   129
  Semaphore post;
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   130
  RCUNestedReaderThread* reader = new RCUNestedReaderThread(&post);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   131
  RCUNestedWriterThread* outer = new RCUNestedWriterThread(&post);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   132
  RCUNestedWriterThread* inner = new RCUNestedWriterThread(&post);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   133
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   134
  reader->doit();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   135
  outer->doit();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   136
  inner->doit();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   137
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   138
  reader->wait_for_state(START_WAIT);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   139
  outer->wait_for_state(START_WAIT);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   140
  inner->wait_for_state(START_WAIT);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   141
  EXPECT_EQ(START_WAIT, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   142
  EXPECT_EQ(START_WAIT, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   143
  EXPECT_EQ(START_WAIT, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   144
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   145
  reader->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   146
  reader->wait_for_state(OUTER_ENTERED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   147
  EXPECT_EQ(OUTER_ENTERED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   148
  EXPECT_EQ(START_WAIT, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   149
  EXPECT_EQ(START_WAIT, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   150
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   151
  outer->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   152
  outer->wait_for_state(SYNCHRONIZING);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   153
  EXPECT_EQ(OUTER_ENTERED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   154
  EXPECT_EQ(SYNCHRONIZING, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   155
  EXPECT_EQ(START_WAIT, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   156
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   157
  os::naked_short_sleep(100);   // Give outer time in synchronization.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   158
  EXPECT_EQ(OUTER_ENTERED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   159
  EXPECT_EQ(SYNCHRONIZING, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   160
  EXPECT_EQ(START_WAIT, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   161
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   162
  reader->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   163
  reader->wait_for_state(INNER_ENTERED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   164
  EXPECT_EQ(INNER_ENTERED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   165
  EXPECT_EQ(SYNCHRONIZING, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   166
  EXPECT_EQ(START_WAIT, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   167
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   168
  inner->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   169
  inner->wait_for_state(SYNCHRONIZING);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   170
  EXPECT_EQ(INNER_ENTERED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   171
  EXPECT_EQ(SYNCHRONIZING, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   172
  EXPECT_EQ(SYNCHRONIZING, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   173
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   174
  os::naked_short_sleep(100); // Give writers time in synchronization.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   175
  EXPECT_EQ(INNER_ENTERED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   176
  EXPECT_EQ(SYNCHRONIZING, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   177
  EXPECT_EQ(SYNCHRONIZING, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   178
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   179
  reader->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   180
  reader->wait_for_state(INNER_EXITED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   181
  // inner does *not* complete synchronization here.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   182
  EXPECT_EQ(INNER_EXITED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   183
  EXPECT_EQ(SYNCHRONIZING, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   184
  EXPECT_EQ(SYNCHRONIZING, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   185
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   186
  os::naked_short_sleep(100); // Give writers more time in synchronization.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   187
  EXPECT_EQ(INNER_EXITED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   188
  EXPECT_EQ(SYNCHRONIZING, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   189
  EXPECT_EQ(SYNCHRONIZING, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   190
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   191
  reader->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   192
  reader->wait_for_state(OUTER_EXITED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   193
  // Both inner and outer can synchronize now.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   194
  outer->wait_for_state(SYNCHRONIZED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   195
  inner->wait_for_state(SYNCHRONIZED);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   196
  EXPECT_EQ(OUTER_EXITED, reader->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   197
  EXPECT_EQ(SYNCHRONIZED, outer->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   198
  EXPECT_EQ(SYNCHRONIZED, inner->state());
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   199
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   200
  // Wait for reader, outer, and inner to complete.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   201
  reader->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   202
  outer->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   203
  inner->proceed();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   204
  for (uint i = 0; i < 3; ++i) {
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   205
    post.wait();
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   206
  }
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents:
diff changeset
   207
}