1 /*
   2  * Copyright (c) 2018, 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 #ifndef SHARE_UTILITIES_GLOBAL_COUNTER_INLINE_HPP
  26 #define SHARE_UTILITIES_GLOBAL_COUNTER_INLINE_HPP
  27 
  28 #include "runtime/orderAccess.inline.hpp"
  29 #include "runtime/thread.inline.hpp"
  30 #include "utilities/globalCounter.hpp"
  31 
  32 inline void GlobalCounter::critical_section_begin(Thread *thread) {
  33   assert(thread == Thread::current(), "must be current thread");
  34   assert(thread->is_VM_thread() || thread->is_Java_thread(), "must be VMThread or JavaThread");
  35   assert((*thread->get_rcu_counter() & COUNTER_ACTIVE) == 0x0, "nestled critical sections, not supported yet");
  36   uintx gbl_cnt = OrderAccess::load_acquire(&_global_counter._counter);
  37   OrderAccess::release_store_fence(thread->get_rcu_counter(), gbl_cnt | COUNTER_ACTIVE);
  38 }
  39 
  40 inline void GlobalCounter::critical_section_end(Thread *thread) {
  41   assert(thread == Thread::current(), "must be current thread");
  42   assert(thread->is_VM_thread() || thread->is_Java_thread(), "must be VMThread or JavaThread");
  43   assert((*thread->get_rcu_counter() & COUNTER_ACTIVE) == COUNTER_ACTIVE, "must be in ctitical section");
  44   // Mainly for debugging we set it to 'now'.
  45   uintx gbl_cnt = OrderAccess::load_acquire(&_global_counter._counter);
  46   OrderAccess::release_store(thread->get_rcu_counter(), gbl_cnt);
  47 }
  48 
  49 class GlobalCounter::CriticalSection {
  50  private:
  51   Thread* _thread;
  52  public:
  53   inline CriticalSection(Thread* thread) : _thread(thread) {
  54     GlobalCounter::critical_section_begin(_thread);
  55   }
  56   inline  ~CriticalSection() {
  57     GlobalCounter::critical_section_end(_thread);
  58   }
  59 };
  60 
  61 #endif // include guard