1 /*
2  * Copyright (c) 2018, 2019, 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(SingleWriterSynchronizer);
61 
62 public:
63   SingleWriterSynchronizer();
64 
65   // Enter a critical section for this synchronizer.  Entering a
66   // critical section never blocks.  While in a critical section, a
67   // thread should avoid blocking, or even take a long time.  In
68   // particular, a thread must never safepoint while in a critical
69   // section.
70   // Precondition: The current thread must not already be in a
71   // critical section for this synchronizer.
72   inline uint enter();
73 
74   // Exit a critical section for this synchronizer.
75   // Precondition: enter_value must be the result of the corresponding
76   // enter() for the critical section.
77   inline void exit(uint enter_value);
78 
79   // Wait until all threads currently in a critical section for this
80   // synchronizer have exited their critical section.  Threads that
81   // enter a critical section after the synchronization has started
82   // are not considered in the wait.
83   // Precondition: No other thread may be synchronizing on this
84   // synchronizer.
85   void synchronize();
86 
87   // RAII class for managing enter/exit pairs.
88   class CriticalSection;
89 };
90 
enter()91 inline uint SingleWriterSynchronizer::enter() {
92   return Atomic::add(2u, &_enter);
93 }
94 
exit(uint enter_value)95 inline void SingleWriterSynchronizer::exit(uint enter_value) {
96   uint exit_value = Atomic::add(2u, &_exit[enter_value & 1]);
97   // If this exit completes a synchronize request, wakeup possibly
98   // waiting synchronizer.  Read of _waiting_for must follow the _exit
99   // update.
100   if (exit_value == _waiting_for) {
101     _wakeup.signal();
102   }
103 }
104 
105 class SingleWriterSynchronizer::CriticalSection : public StackObj {
106   SingleWriterSynchronizer* _synchronizer;
107   uint _enter_value;
108 
109 public:
110   // Enter synchronizer's critical section.
CriticalSection(SingleWriterSynchronizer * synchronizer)111   explicit CriticalSection(SingleWriterSynchronizer* synchronizer) :
112     _synchronizer(synchronizer),
113     _enter_value(synchronizer->enter())
114   {}
115 
116   // Exit synchronizer's critical section.
~CriticalSection()117   ~CriticalSection() {
118     _synchronizer->exit(_enter_value);
119   }
120 };
121 
122 #endif // SHARE_UTILITIES_SINGLEWRITERSYNCHRONIZER_HPP
123