/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #ifndef SHARE_UTILITIES_WAITBARRIER_HPP #define SHARE_UTILITIES_WAITBARRIER_HPP #include "memory/allocation.hpp" #include "utilities/waitBarrier_generic.hpp" #if defined(LINUX) #include "waitBarrier_linux.hpp" typedef LinuxWaitBarrier WaitBarrierDefault; #else typedef GenericWaitBarrier WaitBarrierDefault; #endif // Platform independent WaitBarrier API. // The WaitBarrier primary objective is to wake threads waiting in wait() fast. // It can be arm with any int, the tag, that is not 0. // - After arm() returns any thread calling wait(tag) with the correct tag will be blocked. // - After disarm() is called any thread calling wait(...) will never block. // - When wake() returns no threads are blocked any more, if it was disarmed before. // - After calling disarm() and wake() it my be re-armed immediately. // Wake thread: // - arm(tag) // - *work* // - disarm() // - wake() // Wait thread: // - wait(tag) template class WaitBarrierType : public CHeapObj { WaitBarrierImpl _impl; // Prevent copying and assignment of WaitBarrier instances. WaitBarrierType(const WaitBarrierDefault&); WaitBarrierType& operator=(const WaitBarrierDefault&); public: WaitBarrierType() : _impl() {} ~WaitBarrierType() {} // Returns implementation type. const char* description() { return _impl.description(); } // Guarantees any thread calling wait() with same tag will be blocked. // Provides a trailing fence. void arm(int barrier_tag) { _impl.arm(barrier_tag); } // Guarantees any thread calling wait() with any tag will not be blocked. // Provides a trailing fence. void disarm() { _impl.disarm(); } // Guarantees any thread called wait() will be awake when it returns. // Provides a trailing fence. void wake() { _impl.wake(); } // Guarantees to return if disarm() and wake() is called. // Provides a trailing fence. void wait(int barrier_tag) { _impl.wait(barrier_tag); } }; typedef WaitBarrierType WaitBarrier; #endif // SHARE_UTILITIES_WAITBARRIER_HPP