@@ -74,6 +74,7 @@ static volatile int eventsEnd = 0;
7474/* testcase(s) */
7575static int prepare ();
7676static int checkThreads (const char * kind);
77+ static int waitSuspended (const char * kind);
7778static int resumeThreads (const char * kind);
7879static int clean ();
7980
@@ -125,14 +126,19 @@ agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
125126 nsk_jvmti_setFailStatus ();
126127 }
127128
128- /* check if all threads suspended on THREAD_START events */
129+ /* check if all THREAD_START events are generated */
129130 if (eventsStart != THREADS_COUNT) {
130131 NSK_COMPLAIN2 (" Unexpected number of THREAD_START events:\n "
131132 " # received: %d\n "
132133 " # expected: %d\n " ,
133134 eventsStart, THREADS_COUNT);
134135 }
135136
137+ /* wait until all threads are suspended */
138+ if (!NSK_VERIFY(waitSuspended (" starting" ))) {
139+ return ;
140+ }
141+
136142 NSK_DISPLAY0 (" Testcase #1: check threads on THREAD_START\n " );
137143 if (!NSK_VERIFY(checkThreads (" starting" )))
138144 return ;
@@ -175,14 +181,19 @@ agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
175181 nsk_jvmti_setFailStatus ();
176182 }
177183
178- /* check ia all threads suspended on THREAD_END event */
184+ /* check if all THREAD_END event are generated */
179185 if (eventsEnd != THREADS_COUNT) {
180186 NSK_COMPLAIN2 (" Unexpected number of THREAD_END events:\n "
181187 " # received: %d\n "
182188 " # expected: %d\n " ,
183189 eventsEnd, THREADS_COUNT);
184190 }
185191
192+ /* wait until all threads are suspended */
193+ if (!NSK_VERIFY(waitSuspended (" finishing" ))) {
194+ return ;
195+ }
196+
186197 NSK_DISPLAY0 (" Testcase #2: check threads on THREAD_END\n " );
187198 if (!NSK_VERIFY(checkThreads (" finishing" )))
188199 return ;
@@ -210,6 +221,31 @@ agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
210221
211222/* ============================================================================= */
212223
224+ static int waitSuspended (const char * kind) {
225+ NSK_DISPLAY1 (" Wait for %s threads to be suspended\n " , kind);
226+ for (int i = 0 ; i < THREADS_COUNT; i++) {
227+ for (int j = 0 ; j * TIMEOUT_DELTA < timeout; j++) {
228+ jint state = 0 ;
229+ if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState (threadsList[i], &state))) {
230+ nsk_jvmti_setFailStatus ();
231+ break ;
232+ }
233+ if ((state & JVMTI_THREAD_STATE_ALIVE) == 0 ) {
234+ NSK_COMPLAIN3 (" %s thread %s is not alive: %x\n " , kind, threadsName[i], (int )state);
235+ nsk_jvmti_setFailStatus ();
236+ break ;
237+ }
238+ if ((state & JVMTI_THREAD_STATE_SUSPENDED) != 0 ) {
239+ NSK_DISPLAY2 (" OK: %s thread %s is suspended\n " , kind, threadsName[i]);
240+ break ;
241+ }
242+ NSK_DISPLAY2 (" %s thread %s is not suspended, waiting\n " , kind, threadsName[i]);
243+ nsk_jvmti_sleep (TIMEOUT_DELTA);
244+ }
245+ }
246+ return NSK_TRUE; // continue execution
247+ }
248+
213249/* *
214250 * Resume all threads in given state.
215251 */
0 commit comments