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_WAITBARRIER_HPP 26 #define SHARE_UTILITIES_WAITBARRIER_HPP 27 28 #include "memory/allocation.hpp" 29 #include "utilities/waitBarrier_generic.hpp" 30 31 #if defined(LINUX) 32 #include "waitBarrier_linux.hpp" 33 typedef LinuxWaitBarrier WaitBarrierDefault; 34 #else 35 typedef GenericWaitBarrier WaitBarrierDefault; 36 #endif 37 38 // Platform independent WaitBarrier API. 39 // The WaitBarrier primary objective is to wake threads waiting in wait() fast. 40 // It can be arm with any int, the tag, that is not 0. 41 // - After arm() returns any thread calling wait(tag) with the correct tag will be blocked. 42 // - After disarm() is called any thread calling wait(...) will never block. 43 // - When wake() returns no threads are blocked any more, if it was disarmed before. 44 // - After calling disarm() and wake() it my be re-armed immediately with a different tag. 45 // - Re-arming with the same tag before all threads returned from previously wait 46 // is implementation defined. They may or may not return from the previously wait(). 47 // Wake thread: 48 // - arm(tag) 49 // - *work* 50 // - disarm() 51 // - wake() 52 // Wait thread: 53 // - wait(tag) 54 template <typename WaitBarrierImpl> 55 class WaitBarrierType : public CHeapObj<mtInternal> { 56 WaitBarrierImpl _impl; 57 58 // Prevent copying and assignment of WaitBarrier instances. 59 WaitBarrierType(const WaitBarrierDefault&); 60 WaitBarrierType& operator=(const WaitBarrierDefault&); 61 62 public: 63 WaitBarrierType() : _impl() {} 64 ~WaitBarrierType() {} 65 66 // Returns implementation type. 67 const char* description() { return _impl.description(); } 68 69 // Guarantees any thread calling wait() with same tag will be blocked. 70 // Provides a trailing fence. 71 void arm(int barrier_tag) { _impl.arm(barrier_tag); } 72 73 // Guarantees any thread calling wait() with any tag will not be blocked. 74 // Provides a trailing fence. 75 void disarm() { _impl.disarm(); } 76 77 // Guarantees any thread called wait() will be awake when it returns. 78 // Provides a trailing fence. 79 void wake() { _impl.wake(); } 80 81 // Guarantees to return if disarm() and wake() is called. 82 // Provides a trailing fence. 83 void wait(int barrier_tag) { _impl.wait(barrier_tag); } 84 }; 85 86 typedef WaitBarrierType<WaitBarrierDefault> WaitBarrier; 87 88 #endif // SHARE_UTILITIES_WAITBARRIER_HPP