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. 45 // Wake thread: 46 // - arm(tag) 47 // - *work* 48 // - disarm() 49 // - wake() 50 // Wait thread: 51 // - wait(tag) 52 template <typename WaitBarrierImpl> 53 class WaitBarrierType : public CHeapObj<mtInternal> { 54 WaitBarrierImpl _impl; 55 56 // Prevent copying and assignment of WaitBarrier instances. 57 WaitBarrierType(const WaitBarrierDefault&); 58 WaitBarrierType& operator=(const WaitBarrierDefault&); 59 60 public: 61 WaitBarrierType() : _impl() {} 62 ~WaitBarrierType() {} 63 64 // Returns implementation type. 65 const char* description() { return _impl.description(); } 66 67 // Guarantees any thread calling wait() with same tag will be blocked. 68 // Provides a trailing fence. 69 void arm(int barrier_tag) { _impl.arm(barrier_tag); } 70 71 // Guarantees any thread calling wait() with any tag will not be blocked. 72 // Provides a trailing fence. 73 void disarm() { _impl.disarm(); } 74 75 // Guarantees any thread called wait() will be awake when it returns. 76 // Provides a trailing fence. 77 void wake() { _impl.wake(); } 78 79 // Guarantees to return if disarm() and wake() is called. 80 // Provides a trailing fence. 81 void wait(int barrier_tag) { _impl.wait(barrier_tag); } 82 }; 83 84 typedef WaitBarrierType<WaitBarrierDefault> WaitBarrier; 85 86 #endif // SHARE_UTILITIES_WAITBARRIER_HPP