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