11/*
2- * Copyright (c) 2003, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2003, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,7 @@ extern "C" {
3333/* ============================================================================= */
3434
3535static jlong timeout = 0 ;
36+ static jrawMonitorID monitor; // a monitor to serialize event callbacks and checkCpuTime calls
3637
3738#define TESTED_THREAD_NAME " thrcputime002Thread"
3839
@@ -150,6 +151,7 @@ agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
150151
151152 NSK_DISPLAY0 (" >>> Testcase #2: Check initial cpu time in agent thread\n " );
152153 {
154+ RawMonitorLocker rml (jvmti, jni, monitor);
153155 if (!checkCpuTime (jvmti, testAgentThread, &prevAgentThreadTime, nullptr , " agent thread" )) {
154156 nsk_jvmti_setFailStatus ();
155157 }
@@ -173,6 +175,7 @@ agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
173175
174176 NSK_DISPLAY0 (" >>> Testcase #5: Check middle cpu time from agent thread\n " );
175177 {
178+ RawMonitorLocker rml (jvmti, jni, monitor);
176179 julong time = 0 ;
177180 runIterations (iterations);
178181 if (!checkCpuTime (jvmti, testAgentThread, &time, &prevAgentThreadTime, " agent thread" )) {
@@ -183,6 +186,7 @@ agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
183186 if (testedThread != nullptr ) {
184187 NSK_DISPLAY0 (" >>> Testcase #6: Check tested thread cpu time from agent thread\n " );
185188 {
189+ RawMonitorLocker rml (jvmti, jni, monitor);
186190 julong time = 0 ;
187191 runIterations (iterations);
188192 if (!checkCpuTime (jvmti, testedThread, &time, &prevTestedThreadTime, " agent thread" )) {
@@ -224,6 +228,7 @@ agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
224228 */
225229JNIEXPORT void JNICALL
226230callbackVMInit (jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
231+ RawMonitorLocker rml (jvmti, jni, monitor);
227232
228233 NSK_DISPLAY0 (" >>> Testcase #1: Check initial cpu time in VM_INIT callback\n " );
229234 {
@@ -239,6 +244,8 @@ callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
239244 */
240245JNIEXPORT void JNICALL
241246callbackVMDeath (jvmtiEnv* jvmti, JNIEnv* jni) {
247+ RawMonitorLocker rml (jvmti, jni, monitor);
248+
242249 int success = NSK_TRUE;
243250
244251 NSK_DISPLAY1 (" Disable events: %d events\n " , EVENTS_COUNT);
@@ -261,6 +268,7 @@ callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
261268 */
262269JNIEXPORT void JNICALL
263270callbackThreadStart (jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
271+ RawMonitorLocker rml (jvmti, jni, monitor);
264272
265273 jvmtiThreadInfo threadInfo;
266274 {
@@ -296,6 +304,7 @@ callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
296304 */
297305JNIEXPORT void JNICALL
298306callbackThreadEnd (jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
307+ RawMonitorLocker rml (jvmti, jni, monitor);
299308
300309 jvmtiThreadInfo threadInfo;
301310 {
@@ -389,6 +398,7 @@ jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
389398 if (nsk_jvmti_enableEvents (JVMTI_ENABLE, EVENTS_COUNT, events, nullptr )) {
390399 NSK_DISPLAY0 (" ... enabled\n " );
391400 }
401+ monitor = create_raw_monitor (jvmti, " Raw monitor for synchronization" );
392402
393403 return JNI_OK;
394404}
0 commit comments