src/hotspot/share/utilities/singleWriterSynchronizer.hpp
changeset 51511 eb8d5aeabab3
child 52332 d2a3503c72f7
equal deleted inserted replaced
51510:6b0012622443 51511:eb8d5aeabab3
       
     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_SINGLEWRITERSYNCHRONIZER_HPP
       
    26 #define SHARE_UTILITIES_SINGLEWRITERSYNCHRONIZER_HPP
       
    27 
       
    28 #include "memory/allocation.hpp"
       
    29 #include "runtime/atomic.hpp"
       
    30 #include "runtime/semaphore.hpp"
       
    31 #include "utilities/globalDefinitions.hpp"
       
    32 #include "utilities/macros.hpp"
       
    33 
       
    34 // Synchronization primitive inspired by RCU.
       
    35 //
       
    36 // Any number of threads may enter critical sections associated with a
       
    37 // synchronizer object.  One (at a time) other thread may wait for the
       
    38 // completion of all critical sections for the synchronizer object
       
    39 // that were extent when the wait was initiated.  Usage is that there
       
    40 // is some state that can be accessed either before or after some
       
    41 // change.  An accessing thread performs the access within a critical
       
    42 // section.  A writer thread performs the state change, and then waits
       
    43 // for critical sections to complete, thereby ensuring there are no
       
    44 // threads in a critical section that might have seen the old state.
       
    45 //
       
    46 // Generally, GlobalCounter should be used instead of this class, as
       
    47 // GlobalCounter has measurably better performance and doesn't have
       
    48 // the single writer at a time restriction.  Use this only in
       
    49 // situations where GlobalCounter won't work for some reason, such as
       
    50 // nesting.  But note that nesting often indicates other problems, and
       
    51 // may risk deadlock.
       
    52 class SingleWriterSynchronizer {
       
    53   volatile uint _enter;
       
    54   volatile uint _exit[2];
       
    55   volatile uint _waiting_for;
       
    56   Semaphore _wakeup;
       
    57 
       
    58   DEBUG_ONLY(volatile uint _writers;)
       
    59 
       
    60   // Noncopyable.
       
    61   SingleWriterSynchronizer(const SingleWriterSynchronizer&);
       
    62   SingleWriterSynchronizer& operator=(const SingleWriterSynchronizer&);
       
    63 
       
    64 public:
       
    65   SingleWriterSynchronizer();
       
    66 
       
    67   // Enter a critical section for this synchronizer.  Entering a
       
    68   // critical section never blocks.  While in a critical section, a
       
    69   // thread should avoid blocking, or even take a long time.  In
       
    70   // particular, a thread must never safepoint while in a critical
       
    71   // section.
       
    72   // Precondition: The current thread must not already be in a
       
    73   // critical section for this synchronizer.
       
    74   inline uint enter();
       
    75 
       
    76   // Exit a critical section for this synchronizer.
       
    77   // Precondition: enter_value must be the result of the corresponding
       
    78   // enter() for the critical section.
       
    79   inline void exit(uint enter_value);
       
    80 
       
    81   // Wait until all threads currently in a critical section for this
       
    82   // synchronizer have exited their critical section.  Threads that
       
    83   // enter a critical section after the synchronization has started
       
    84   // are not considered in the wait.
       
    85   // Precondition: No other thread may be synchronizing on this
       
    86   // synchronizer.
       
    87   void synchronize();
       
    88 
       
    89   // RAII class for managing enter/exit pairs.
       
    90   class CriticalSection;
       
    91 };
       
    92 
       
    93 inline uint SingleWriterSynchronizer::enter() {
       
    94   return Atomic::add(2u, &_enter);
       
    95 }
       
    96 
       
    97 inline void SingleWriterSynchronizer::exit(uint enter_value) {
       
    98   uint exit_value = Atomic::add(2u, &_exit[enter_value & 1]);
       
    99   // If this exit completes a synchronize request, wakeup possibly
       
   100   // waiting synchronizer.  Read of _waiting_for must follow the _exit
       
   101   // update.
       
   102   if (exit_value == _waiting_for) {
       
   103     _wakeup.signal();
       
   104   }
       
   105 }
       
   106 
       
   107 class SingleWriterSynchronizer::CriticalSection : public StackObj {
       
   108   SingleWriterSynchronizer* _synchronizer;
       
   109   uint _enter_value;
       
   110 
       
   111 public:
       
   112   // Enter synchronizer's critical section.
       
   113   explicit CriticalSection(SingleWriterSynchronizer* synchronizer) :
       
   114     _synchronizer(synchronizer),
       
   115     _enter_value(synchronizer->enter())
       
   116   {}
       
   117 
       
   118   // Exit synchronizer's critical section.
       
   119   ~CriticalSection() {
       
   120     _synchronizer->exit(_enter_value);
       
   121   }
       
   122 };
       
   123 
       
   124 #endif // SHARE_UTILITIES_SINGLEWRITERSYNCHRONIZER_HPP