< prev index next >

src/hotspot/share/runtime/mutex.hpp

Print this page

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

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

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





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

 80   }
 81 
 82   friend constexpr bool operator<(Rank lhs, Rank rhs) {
 83     return static_cast<int>(lhs) < static_cast<int>(rhs);
 84   }
 85 
 86   friend constexpr bool operator>(Rank lhs, Rank rhs)  { return rhs < lhs; }
 87   friend constexpr bool operator<=(Rank lhs, Rank rhs) { return !(lhs > rhs); }
 88   friend constexpr bool operator>=(Rank lhs, Rank rhs) { return !(lhs < rhs); }
 89 
 90  private:
 91   // The _owner field is only set by the current thread, either to itself after it has acquired
 92   // the low-level _lock, or to null before it has released the _lock. Accesses by any thread other
 93   // than the lock owner are inherently racy.
 94   Thread* volatile _owner;
 95   void raw_set_owner(Thread* new_owner) { Atomic::store(&_owner, new_owner); }
 96 
 97  protected:                              // Monitor-Mutex metadata
 98   PlatformMonitor _lock;                 // Native monitor implementation
 99   const char* _name;                     // Name of mutex/monitor
100   int _id;                               // ID for named mutexes
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   int      id() const { return _id; }
198 //  void set_id(int id) { _id = id; }
199 
200   static const char* id2name(int id);
201 
202   void print_on_error(outputStream* st) const;
203   #ifndef PRODUCT
204     void print_on(outputStream* st) const;
205     void print() const;
206   #endif
207 };
208 
209 class Monitor : public Mutex {
210  public:
211   Monitor(Rank rank, const char *name, bool allow_vm_block)  :
212     Mutex(rank, name, allow_vm_block) {}
213 
214   Monitor(Rank rank, const char *name) :
215     Mutex(rank, name) {}
216   // default destructor
217 
218   // Wait until monitor is notified (or times out).
219   // Defaults are to make safepoint checks, wait time is forever (i.e.,
220   // zero). Returns true if wait times out; otherwise returns false.
221   bool wait(uint64_t timeout = 0);
< prev index next >