@@ -32,11 +32,8 @@ extern "C" {
3232#define STATUS_FAILED 2
3333
3434static jvmtiEnv *jvmti = nullptr ;
35- static jrawMonitorID event_lock = nullptr ;
3635static jint result = PASSED;
3736static int check_idx = 0 ;
38- static int waits_to_enter = 0 ;
39- static int waits_to_be_notified = 0 ;
4037static jobject tested_monitor = nullptr ;
4138
4239static bool is_tested_monitor (JNIEnv *jni, jobject monitor) {
@@ -53,47 +50,10 @@ static void log_event(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread,
5350 deallocate (jvmti, jni, (void *)tname);
5451}
5552
56- JNIEXPORT void JNICALL
57- MonitorContendedEnter (jvmtiEnv *jvmti, JNIEnv *jni, jthread thread, jobject monitor) {
58- RawMonitorLocker rml (jvmti, jni, event_lock);
59- if (is_tested_monitor (jni, monitor)) {
60- waits_to_enter++;
61- log_event (jvmti, jni, thread, " MonitorContendedEnter" , waits_to_enter);
62- }
63- }
64-
65- JNIEXPORT void JNICALL
66- MonitorContendedEntered (jvmtiEnv *jvmti, JNIEnv *jni, jthread thread, jobject monitor) {
67- RawMonitorLocker rml (jvmti, jni, event_lock);
68- if (is_tested_monitor (jni, monitor)) {
69- waits_to_enter--;
70- log_event (jvmti, jni, thread, " MonitorContendedEntered" , waits_to_enter);
71- }
72- }
73-
74- JNIEXPORT void JNICALL
75- MonitorWait (jvmtiEnv *jvmti, JNIEnv *jni, jthread thread, jobject monitor, jlong timeout) {
76- RawMonitorLocker rml (jvmti, jni, event_lock);
77- if (is_tested_monitor (jni, monitor)) {
78- waits_to_be_notified++;
79- log_event (jvmti, jni, thread, " MonitorWait" , waits_to_be_notified);
80- }
81- }
82-
83- JNIEXPORT void JNICALL
84- MonitorWaited (jvmtiEnv *jvmti, JNIEnv *jni, jthread thread, jobject monitor, jboolean timed_out) {
85- RawMonitorLocker rml (jvmti, jni, event_lock);
86- if (is_tested_monitor (jni, monitor)) {
87- waits_to_be_notified--;
88- log_event (jvmti, jni, thread, " MonitorWaited" , waits_to_be_notified);
89- }
90- }
91-
9253jint Agent_Initialize (JavaVM *jvm, char *options, void *reserved) {
9354 jint res;
9455 jvmtiError err;
9556 jvmtiCapabilities caps;
96- jvmtiEventCallbacks callbacks;
9757
9858 res = jvm->GetEnv ((void **) &jvmti, JVMTI_VERSION_1_1);
9959 if (res != JNI_OK || jvmti == nullptr ) {
@@ -116,17 +76,6 @@ jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
11676 LOG (" Warning: Monitor events are not implemented\n " );
11777 return JNI_ERR;
11878 }
119- memset (&callbacks, 0 , sizeof (callbacks));
120- callbacks.MonitorContendedEnter = &MonitorContendedEnter;
121- callbacks.MonitorContendedEntered = &MonitorContendedEntered;
122- callbacks.MonitorWait = &MonitorWait;
123- callbacks.MonitorWaited = &MonitorWaited;
124-
125- err = jvmti->SetEventCallbacks (&callbacks, sizeof (jvmtiEventCallbacks));
126- check_jvmti_error (err, " Agent_Initialize: error in JVMTI SetEventCallbacks" );
127-
128- event_lock = create_raw_monitor (jvmti, " Events Monitor" );
129-
13079 return JNI_OK;
13180}
13281
@@ -216,41 +165,20 @@ Java_ObjectMonitorUsage_check(JNIEnv *jni, jclass cls, jobject obj, jthread owne
216165
217166JNIEXPORT void JNICALL
218167Java_ObjectMonitorUsage_setTestedMonitor (JNIEnv *jni, jclass cls, jobject monitor) {
219- jvmtiError err;
220- jvmtiEventMode event_mode = (monitor != nullptr ) ? JVMTI_ENABLE : JVMTI_DISABLE;
221-
222- RawMonitorLocker rml (jvmti, jni, event_lock);
223-
224168 if (tested_monitor != nullptr ) {
225169 jni->DeleteGlobalRef (tested_monitor);
226170 }
227171 tested_monitor = (monitor != nullptr ) ? jni->NewGlobalRef (monitor) : nullptr ;
228- waits_to_enter = 0 ;
229- waits_to_be_notified = 0 ;
230-
231- err = jvmti->SetEventNotificationMode (event_mode, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, nullptr );
232- check_jvmti_status (jni, err, " setTestedMonitor: error in JVMTI SetEventNotificationMode #1" );
233-
234- err = jvmti->SetEventNotificationMode (event_mode, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, nullptr );
235- check_jvmti_status (jni, err, " setTestedMonitor: error in JVMTI SetEventNotificationMode #2" );
236-
237- err = jvmti->SetEventNotificationMode (event_mode, JVMTI_EVENT_MONITOR_WAIT, nullptr );
238- check_jvmti_status (jni, err, " setTestedMonitor: error in JVMTI SetEventNotificationMode #3" );
239-
240- err = jvmti->SetEventNotificationMode (event_mode, JVMTI_EVENT_MONITOR_WAITED, nullptr );
241- check_jvmti_status (jni, err, " setTestedMonitor: error in JVMTI SetEventNotificationMode #4" );
242172}
243173
244- JNIEXPORT jint JNICALL
245- Java_ObjectMonitorUsage_waitsToEnter (JNIEnv *jni, jclass cls) {
246- RawMonitorLocker rml (jvmti, jni, event_lock);
247- return waits_to_enter;
174+ JNIEXPORT void JNICALL
175+ Java_ObjectMonitorUsage_ensureBlockedOnEnter (JNIEnv *jni, jclass cls, jthread thread) {
176+ wait_for_state (jvmti, jni, thread, JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER);
248177}
249178
250- JNIEXPORT jint JNICALL
251- Java_ObjectMonitorUsage_waitsToBeNotified (JNIEnv *jni, jclass cls) {
252- RawMonitorLocker rml (jvmti, jni, event_lock);
253- return waits_to_be_notified;
179+ JNIEXPORT void JNICALL
180+ Java_ObjectMonitorUsage_ensureWaitingToBeNotified (JNIEnv *jni, jclass cls, jthread thread) {
181+ wait_for_state (jvmti, jni, thread, JVMTI_THREAD_STATE_WAITING_INDEFINITELY);
254182}
255183
256184JNIEXPORT jint JNICALL
0 commit comments