92 }
93
94 friend constexpr bool operator<(Rank lhs, Rank rhs) {
95 return static_cast<int>(lhs) < static_cast<int>(rhs);
96 }
97
98 friend constexpr bool operator>(Rank lhs, Rank rhs) { return rhs < lhs; }
99 friend constexpr bool operator<=(Rank lhs, Rank rhs) { return !(lhs > rhs); }
100 friend constexpr bool operator>=(Rank lhs, Rank rhs) { return !(lhs < rhs); }
101
102 private:
103 // The _owner field is only set by the current thread, either to itself after it has acquired
104 // the low-level _lock, or to null before it has released the _lock. Accesses by any thread other
105 // than the lock owner are inherently racy.
106 Thread* volatile _owner;
107 void raw_set_owner(Thread* new_owner) { Atomic::store(&_owner, new_owner); }
108
109 protected: // Monitor-Mutex metadata
110 PlatformMonitor _lock; // Native monitor implementation
111 const char* _name; // Name of mutex/monitor
112
113 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
114 #ifndef PRODUCT
115 bool _allow_vm_block;
116 #endif
117 static Mutex** _mutex_array;
118 static int _num_mutex;
119
120 #ifdef ASSERT
121 Rank _rank; // rank (to avoid/detect potential deadlocks)
122 Mutex* _next; // Used by a Thread to link up owned locks
123 Thread* _last_owner; // the last thread to own the lock
124 bool _skip_rank_check; // read only by owner when doing rank checks
125
126 static Mutex* get_least_ranked_lock(Mutex* locks);
127 Mutex* get_least_ranked_lock_besides_this(Mutex* locks);
128 bool skip_rank_check() {
129 assert(owned_by_self(), "only the owner should call this");
130 return _skip_rank_check;
131 }
191 bool try_lock_inner(bool do_rank_checks);
192 public:
193
194 void release_for_safepoint();
195
196 // Lock without safepoint check. Should ONLY be used by safepoint code and other code
197 // that is guaranteed not to block while running inside the VM.
198 void lock_without_safepoint_check();
199 void lock_without_safepoint_check(Thread* self);
200 // A thread should not call this if failure to acquire ownership will blocks its progress
201 bool try_lock_without_rank_check();
202
203 // Current owner - note not MT-safe. Can only be used to guarantee that
204 // the current running thread owns the lock
205 Thread* owner() const { return Atomic::load(&_owner); }
206 void set_owner(Thread* owner) { set_owner_implementation(owner); }
207 bool owned_by_self() const;
208
209 const char *name() const { return _name; }
210
211 static void add_mutex(Mutex* var);
212
213 void print_on_error(outputStream* st) const;
214 #ifndef PRODUCT
215 void print_on(outputStream* st) const;
216 void print() const;
217 #endif
218
219 // Print all mutexes/monitors that are currently owned by a thread; called
220 // by fatal error handler.
221 static void print_owned_locks_on_error(outputStream* st);
222 static void print_lock_ranks(outputStream* st);
223 };
224
225 class Monitor : public Mutex {
226 public:
227 Monitor(Rank rank, const char *name, bool allow_vm_block) :
228 Mutex(rank, name, allow_vm_block) {}
229
230 Monitor(Rank rank, const char *name) :
231 Mutex(rank, name) {}
232 // default destructor
233
234 // Wait until monitor is notified (or times out).
235 // Defaults are to make safepoint checks, wait time is forever (i.e.,
236 // zero). Returns true if wait times out; otherwise returns false.
237 bool wait(uint64_t timeout = 0);
238 bool wait_without_safepoint_check(uint64_t timeout = 0);
239 void notify();
240 void notify_all();
241 };
242
|
92 }
93
94 friend constexpr bool operator<(Rank lhs, Rank rhs) {
95 return static_cast<int>(lhs) < static_cast<int>(rhs);
96 }
97
98 friend constexpr bool operator>(Rank lhs, Rank rhs) { return rhs < lhs; }
99 friend constexpr bool operator<=(Rank lhs, Rank rhs) { return !(lhs > rhs); }
100 friend constexpr bool operator>=(Rank lhs, Rank rhs) { return !(lhs < rhs); }
101
102 private:
103 // The _owner field is only set by the current thread, either to itself after it has acquired
104 // the low-level _lock, or to null before it has released the _lock. Accesses by any thread other
105 // than the lock owner are inherently racy.
106 Thread* volatile _owner;
107 void raw_set_owner(Thread* new_owner) { Atomic::store(&_owner, new_owner); }
108
109 protected: // Monitor-Mutex metadata
110 PlatformMonitor _lock; // Native monitor implementation
111 const char* _name; // Name of mutex/monitor
112 int _id; // ID for named mutexes
113
114 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
115 #ifndef PRODUCT
116 bool _allow_vm_block;
117 #endif
118 static Mutex** _mutex_array;
119 static int _num_mutex;
120
121 #ifdef ASSERT
122 Rank _rank; // rank (to avoid/detect potential deadlocks)
123 Mutex* _next; // Used by a Thread to link up owned locks
124 Thread* _last_owner; // the last thread to own the lock
125 bool _skip_rank_check; // read only by owner when doing rank checks
126
127 static Mutex* get_least_ranked_lock(Mutex* locks);
128 Mutex* get_least_ranked_lock_besides_this(Mutex* locks);
129 bool skip_rank_check() {
130 assert(owned_by_self(), "only the owner should call this");
131 return _skip_rank_check;
132 }
192 bool try_lock_inner(bool do_rank_checks);
193 public:
194
195 void release_for_safepoint();
196
197 // Lock without safepoint check. Should ONLY be used by safepoint code and other code
198 // that is guaranteed not to block while running inside the VM.
199 void lock_without_safepoint_check();
200 void lock_without_safepoint_check(Thread* self);
201 // A thread should not call this if failure to acquire ownership will blocks its progress
202 bool try_lock_without_rank_check();
203
204 // Current owner - note not MT-safe. Can only be used to guarantee that
205 // the current running thread owns the lock
206 Thread* owner() const { return Atomic::load(&_owner); }
207 void set_owner(Thread* owner) { set_owner_implementation(owner); }
208 bool owned_by_self() const;
209
210 const char *name() const { return _name; }
211
212 int id() const { return _id; }
213 // void set_id(int id) { _id = id; }
214
215 static void add_mutex(Mutex* var);
216
217 void print_on_error(outputStream* st) const;
218 #ifndef PRODUCT
219 void print_on(outputStream* st) const;
220 void print() const;
221 #endif
222
223 // Print all mutexes/monitors that are currently owned by a thread; called
224 // by fatal error handler.
225 static void print_owned_locks_on_error(outputStream* st);
226 static void print_lock_ranks(outputStream* st);
227
228 static int num_mutex() { return _num_mutex; }
229 };
230
231 class Monitor : public Mutex {
232 public:
233 Monitor(Rank rank, const char *name, bool allow_vm_block) :
234 Mutex(rank, name, allow_vm_block) {}
235
236 Monitor(Rank rank, const char *name) :
237 Mutex(rank, name) {}
238 // default destructor
239
240 // Wait until monitor is notified (or times out).
241 // Defaults are to make safepoint checks, wait time is forever (i.e.,
242 // zero). Returns true if wait times out; otherwise returns false.
243 bool wait(uint64_t timeout = 0);
244 bool wait_without_safepoint_check(uint64_t timeout = 0);
245 void notify();
246 void notify_all();
247 };
248
|