< prev index next >

src/hotspot/share/runtime/mutex.hpp

Print this page

 81   }
 82 
 83   friend constexpr bool operator<(Rank lhs, Rank rhs) {
 84     return static_cast<int>(lhs) < static_cast<int>(rhs);
 85   }
 86 
 87   friend constexpr bool operator>(Rank lhs, Rank rhs)  { return rhs < lhs; }
 88   friend constexpr bool operator<=(Rank lhs, Rank rhs) { return !(lhs > rhs); }
 89   friend constexpr bool operator>=(Rank lhs, Rank rhs) { return !(lhs < rhs); }
 90 
 91  private:
 92   // The _owner field is only set by the current thread, either to itself after it has acquired
 93   // the low-level _lock, or to null before it has released the _lock. Accesses by any thread other
 94   // than the lock owner are inherently racy.
 95   Thread* volatile _owner;
 96   void raw_set_owner(Thread* new_owner) { Atomic::store(&_owner, new_owner); }
 97 
 98  protected:                              // Monitor-Mutex metadata
 99   PlatformMonitor _lock;                 // Native monitor implementation
100   const char* _name;                     // Name of mutex/monitor

101 
102   // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
103 #ifndef PRODUCT
104   bool    _allow_vm_block;
105 #endif
106 #ifdef ASSERT
107   Rank    _rank;                 // rank (to avoid/detect potential deadlocks)
108   Mutex*  _next;                 // Used by a Thread to link up owned locks
109   Thread* _last_owner;           // the last thread to own the lock
110   bool _skip_rank_check;         // read only by owner when doing rank checks
111 
112   static Mutex* get_least_ranked_lock(Mutex* locks);
113   Mutex* get_least_ranked_lock_besides_this(Mutex* locks);
114   bool skip_rank_check() {
115     assert(owned_by_self(), "only the owner should call this");
116     return _skip_rank_check;
117   }
118 
119  public:
120   Rank   rank() const          { return _rank; }

177   bool try_lock_inner(bool do_rank_checks);
178  public:
179 
180   void release_for_safepoint();
181 
182   // Lock without safepoint check. Should ONLY be used by safepoint code and other code
183   // that is guaranteed not to block while running inside the VM.
184   void lock_without_safepoint_check();
185   void lock_without_safepoint_check(Thread* self);
186   // A thread should not call this if failure to acquire ownership will blocks its progress
187   bool try_lock_without_rank_check();
188 
189   // Current owner - note not MT-safe. Can only be used to guarantee that
190   // the current running thread owns the lock
191   Thread* owner() const         { return Atomic::load(&_owner); }
192   void set_owner(Thread* owner) { set_owner_implementation(owner); }
193   bool owned_by_self() const;
194 
195   const char *name() const                  { return _name; }
196 





197   void print_on_error(outputStream* st) const;
198   #ifndef PRODUCT
199     void print_on(outputStream* st) const;
200     void print() const;
201   #endif
202 };
203 
204 class Monitor : public Mutex {
205  public:
206   Monitor(Rank rank, const char *name, bool allow_vm_block)  :
207     Mutex(rank, name, allow_vm_block) {}
208 
209   Monitor(Rank rank, const char *name) :
210     Mutex(rank, name) {}
211   // default destructor
212 
213   // Wait until monitor is notified (or times out).
214   // Defaults are to make safepoint checks, wait time is forever (i.e.,
215   // zero). Returns true if wait times out; otherwise returns false.
216   bool wait(uint64_t timeout = 0);

 81   }
 82 
 83   friend constexpr bool operator<(Rank lhs, Rank rhs) {
 84     return static_cast<int>(lhs) < static_cast<int>(rhs);
 85   }
 86 
 87   friend constexpr bool operator>(Rank lhs, Rank rhs)  { return rhs < lhs; }
 88   friend constexpr bool operator<=(Rank lhs, Rank rhs) { return !(lhs > rhs); }
 89   friend constexpr bool operator>=(Rank lhs, Rank rhs) { return !(lhs < rhs); }
 90 
 91  private:
 92   // The _owner field is only set by the current thread, either to itself after it has acquired
 93   // the low-level _lock, or to null before it has released the _lock. Accesses by any thread other
 94   // than the lock owner are inherently racy.
 95   Thread* volatile _owner;
 96   void raw_set_owner(Thread* new_owner) { Atomic::store(&_owner, new_owner); }
 97 
 98  protected:                              // Monitor-Mutex metadata
 99   PlatformMonitor _lock;                 // Native monitor implementation
100   const char* _name;                     // Name of mutex/monitor
101   int _id;                               // ID for named mutexes
102 
103   // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
104 #ifndef PRODUCT
105   bool    _allow_vm_block;
106 #endif
107 #ifdef ASSERT
108   Rank    _rank;                 // rank (to avoid/detect potential deadlocks)
109   Mutex*  _next;                 // Used by a Thread to link up owned locks
110   Thread* _last_owner;           // the last thread to own the lock
111   bool _skip_rank_check;         // read only by owner when doing rank checks
112 
113   static Mutex* get_least_ranked_lock(Mutex* locks);
114   Mutex* get_least_ranked_lock_besides_this(Mutex* locks);
115   bool skip_rank_check() {
116     assert(owned_by_self(), "only the owner should call this");
117     return _skip_rank_check;
118   }
119 
120  public:
121   Rank   rank() const          { return _rank; }

178   bool try_lock_inner(bool do_rank_checks);
179  public:
180 
181   void release_for_safepoint();
182 
183   // Lock without safepoint check. Should ONLY be used by safepoint code and other code
184   // that is guaranteed not to block while running inside the VM.
185   void lock_without_safepoint_check();
186   void lock_without_safepoint_check(Thread* self);
187   // A thread should not call this if failure to acquire ownership will blocks its progress
188   bool try_lock_without_rank_check();
189 
190   // Current owner - note not MT-safe. Can only be used to guarantee that
191   // the current running thread owns the lock
192   Thread* owner() const         { return Atomic::load(&_owner); }
193   void set_owner(Thread* owner) { set_owner_implementation(owner); }
194   bool owned_by_self() const;
195 
196   const char *name() const                  { return _name; }
197 
198   int      id() const { return _id; }
199 //  void set_id(int id) { _id = id; }
200 
201   static const char* id2name(int id);
202 
203   void print_on_error(outputStream* st) const;
204   #ifndef PRODUCT
205     void print_on(outputStream* st) const;
206     void print() const;
207   #endif
208 };
209 
210 class Monitor : public Mutex {
211  public:
212   Monitor(Rank rank, const char *name, bool allow_vm_block)  :
213     Mutex(rank, name, allow_vm_block) {}
214 
215   Monitor(Rank rank, const char *name) :
216     Mutex(rank, name) {}
217   // default destructor
218 
219   // Wait until monitor is notified (or times out).
220   // Defaults are to make safepoint checks, wait time is forever (i.e.,
221   // zero). Returns true if wait times out; otherwise returns false.
222   bool wait(uint64_t timeout = 0);
< prev index next >