< prev index next >

src/hotspot/share/jfr/utilities/jfrTryLock.hpp

Print this page




  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_JFR_UTILITIES_JFRTRYLOCK_HPP
  26 #define SHARE_JFR_UTILITIES_JFRTRYLOCK_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/orderAccess.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 class JfrTryLock {
  34  private:
  35   volatile int* const _lock;
  36   bool _has_lock;
  37 
  38  public:
  39   JfrTryLock(volatile int* lock) : _lock(lock), _has_lock(Atomic::cmpxchg(1, lock, 0) == 0) {}
  40 
  41   ~JfrTryLock() {
  42     if (_has_lock) {
  43       OrderAccess::fence();
  44       *_lock = 0;
  45     }
  46   }
  47 
  48   bool has_lock() const {
  49     return _has_lock;
  50   }
  51 };
  52 
  53 class JfrMonitorTryLock : public StackObj {
  54  private:
  55   Monitor* _lock;
  56   bool _acquired;
  57 
  58  public:
  59   JfrMonitorTryLock(Monitor* lock) : _lock(lock), _acquired(lock->try_lock()) {}


  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_JFR_UTILITIES_JFRTRYLOCK_HPP
  26 #define SHARE_JFR_UTILITIES_JFRTRYLOCK_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/orderAccess.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 class JfrTryLock {
  34  private:
  35   volatile int* const _lock;
  36   bool _has_lock;
  37 
  38  public:
  39   JfrTryLock(volatile int* lock) : _lock(lock), _has_lock(Atomic::cmpxchg(lock, 0, 1) == 0) {}
  40 
  41   ~JfrTryLock() {
  42     if (_has_lock) {
  43       OrderAccess::fence();
  44       *_lock = 0;
  45     }
  46   }
  47 
  48   bool has_lock() const {
  49     return _has_lock;
  50   }
  51 };
  52 
  53 class JfrMonitorTryLock : public StackObj {
  54  private:
  55   Monitor* _lock;
  56   bool _acquired;
  57 
  58  public:
  59   JfrMonitorTryLock(Monitor* lock) : _lock(lock), _acquired(lock->try_lock()) {}
< prev index next >