/* * Test test_jthread_set_and_get_local_storage */ int test_jthread_set_and_get_local_storage(void) { void * data; hythread_t hythread; tested_thread_sturct_t *tts; // Initialize tts structures and run all tested threads tested_threads_run(default_run_for_test); reset_tested_thread_iterator(&tts); while (next_tested_thread(&tts)) { hythread = jthread_get_native_thread(tts->java_thread); tf_assert(hythread); tf_assert_same(hythread_tls_set(hythread, TEST_TLS_KEY, tts), TM_ERROR_NONE); } reset_tested_thread_iterator(&tts); while (next_tested_thread(&tts)) { hythread = jthread_get_native_thread(tts->java_thread); tf_assert(hythread); data = hythread_tls_get(hythread, TEST_TLS_KEY); tf_assert_same(data, tts); } // Terminate all threads and clear tts structures tested_threads_destroy(); return TEST_PASSED; }
int test_jthread_get_owned_monitors(void) { tested_thread_sturct_t *tts; tested_thread_sturct_t *critical_tts; int i; jint owned_monitors_count; jobject *owned_monitors = NULL; hysem_create(&mon_enter, 0, 1); // Initialize tts structures and run all tested threads tested_threads_run(run_for_test_jthread_get_owned_monitors); for (i = 0; i < MAX_TESTED_THREAD_NUMBER; i++){ critical_tts = NULL; hysem_wait(mon_enter); reset_tested_thread_iterator(&tts); while(next_tested_thread(&tts)) { while(tts->phase == TT_PHASE_NONE) { // thread is not started yet hythread_yield(); } if (tts->phase == TT_PHASE_IN_CRITICAL_SECTON){ tf_assert_same(jthread_get_owned_monitors (tts->java_thread, &owned_monitors_count, &owned_monitors), TM_ERROR_NONE); tf_assert(critical_tts == NULL); critical_tts = tts; tf_assert_same(owned_monitors_count, 1); tf_assert_same(owned_monitors[0]->object, tts->monitor->object); } else if (tts->phase == TT_PHASE_WAITING_ON_MONITOR){ tf_assert_same(jthread_get_owned_monitors (tts->java_thread, &owned_monitors_count, &owned_monitors), TM_ERROR_NONE); tf_assert_same(owned_monitors_count, 0); } } tf_assert(critical_tts); tested_thread_send_stop_request(critical_tts); tested_thread_wait_ended(critical_tts); } // Terminate all threads and clear tts structures tested_threads_destroy(); return TEST_PASSED; }
int test_jthread_get_lock_owner(void) { tested_thread_sturct_t *tts; tested_thread_sturct_t *critical_tts = NULL; jthread lock_owner = NULL; int blocked_count; int i; hysem_create(&mon_enter, 0 , 1); // Initialize tts structures and run all tested threads tested_threads_run(run_for_test_jthread_get_lock_owner); for (i = 0; i < MAX_TESTED_THREAD_NUMBER; i++){ blocked_count = 0; critical_tts = NULL; hysem_wait(mon_enter); reset_tested_thread_iterator(&tts); while(next_tested_thread(&tts)){ while(tts->phase == TT_PHASE_NONE) { // thread is not started yet hythread_yield(); } if (tts->phase == TT_PHASE_IN_CRITICAL_SECTON){ tf_assert_null(critical_tts); critical_tts = tts; } else if (tts->phase == TT_PHASE_WAITING_ON_MONITOR){ blocked_count++; } } tf_assert(critical_tts); // thread in critical section found tf_assert_same(blocked_count, MAX_TESTED_THREAD_NUMBER - i - 1); tf_assert_same(jthread_get_lock_owner(critical_tts->monitor, &lock_owner), TM_ERROR_NONE); tf_assert(lock_owner); tf_assert_same(critical_tts->java_thread->object, lock_owner->object); tested_thread_send_stop_request(critical_tts); tested_thread_wait_ended(critical_tts); check_tested_thread_phase(critical_tts, TT_PHASE_DEAD); } // Terminate all threads and clear tts structures tested_threads_destroy(); return TEST_PASSED; }
int check_phase(tested_thread_sturct_t *tts, int phase) { tf_assert(tts->phase != TT_PHASE_ERROR); if (phase == TT_PHASE_ANY) { tf_assert(tts->phase != TT_PHASE_NONE); } else if (tts->phase != phase) { log_info("Expected phase %d, but got %d", phase, tts->phase); tf_assert_same(tts->phase, phase); } return TEST_PASSED; }
int check_structure(tested_thread_sturct_t *tts){ jthread java_thread = tts->java_thread; vm_thread_t vm_thread; jvmti_thread_t jvmti_thread; hythread_t hythread; vm_thread = jthread_get_vm_thread_from_java(java_thread); tf_assert(vm_thread); hythread = (hythread_t)vm_thread; tf_assert_same(hythread, tts->native_thread); jvmti_thread = &(vm_thread->jvmti_thread); tf_assert(jvmti_thread); return TEST_PASSED; }
int start_proc(void *args) { hythread_thin_monitor_t *lock_p = (hythread_thin_monitor_t*)((void**)args)[0]; hythread_thin_monitor_t *monitor_p = (hythread_thin_monitor_t*)((void**)args)[1]; IDATA *ret = (IDATA*)&(((void**)args)[2]); IDATA status; // wait to start hythread_suspend_disable(); status = hythread_thin_monitor_enter(monitor_p); if (status != TM_ERROR_NONE) { hythread_suspend_enable(); tf_assert_same(status, TM_ERROR_NONE); } // notify main thread about thread start status = hythread_thin_monitor_enter(lock_p); if (status != TM_ERROR_NONE) { hythread_suspend_enable(); tf_assert_same(status, TM_ERROR_NONE); } started_thread_count++; status = hythread_thin_monitor_notify(lock_p); if (status != TM_ERROR_NONE) { hythread_suspend_enable(); tf_assert_same(status, TM_ERROR_NONE); } status = hythread_thin_monitor_exit(lock_p); if (status != TM_ERROR_NONE) { hythread_suspend_enable(); tf_assert_same(status, TM_ERROR_NONE); } // fall to infinite wait status = hythread_thin_monitor_wait(monitor_p); if (status != TM_ERROR_NONE) { hythread_suspend_enable(); tf_assert_same(status, TM_ERROR_NONE); } (*ret)++; status = hythread_thin_monitor_exit(monitor_p); if (status != TM_ERROR_NONE) { hythread_suspend_enable(); tf_assert_same(status, TM_ERROR_NONE); } hythread_suspend_enable(); return 0; }
int test_jthread_get_contended_monitor(void) { tested_thread_sturct_t *tts; tested_thread_sturct_t *critical_tts = NULL; jobject contended_monitor; int i; hysem_create(&mon_enter, 0, 1); // Initialize tts structures and run all tested threads tested_threads_run(run_for_test_jthread_get_contended_monitor); for (i = 0; i < MAX_TESTED_THREAD_NUMBER; i++){ critical_tts = NULL; hysem_wait(mon_enter); reset_tested_thread_iterator(&tts); while(next_tested_thread(&tts)){ while(tts->phase == TT_PHASE_NONE) { // thread is not started yet hythread_yield(); } if (tts->phase == TT_PHASE_IN_CRITICAL_SECTON){ tf_assert_same(jthread_get_contended_monitor(tts->java_thread, &contended_monitor), TM_ERROR_NONE); tf_assert_null(contended_monitor); tf_assert_null(critical_tts); critical_tts = tts; } else if (tts->phase != TT_PHASE_DEAD) { check_tested_thread_phase(tts, TT_PHASE_WAITING_ON_MONITOR); // This can't be guaranteed //tf_assert(vm_objects_are_equal(contended_monitor, tts->monitor)); } } tested_thread_send_stop_request(critical_tts); tested_thread_wait_ended(critical_tts); check_tested_thread_phase(critical_tts, TT_PHASE_DEAD); } // Terminate all threads and clear tts structures tested_threads_destroy(); return TEST_PASSED; }
int test_hythread_thread_suspend_all(void) { void **args; hythread_t thread_list[THREAD_COUNT]; hythread_thin_monitor_t lock; hythread_thin_monitor_t monitor; IDATA status; int i; // create monitors status = hythread_thin_monitor_create(&monitor); tf_assert_same(status, TM_ERROR_NONE); status = hythread_thin_monitor_create(&lock); tf_assert_same(status, TM_ERROR_NONE); // alloc and set thread start procedure args args = (void**)calloc(3, sizeof(void*)); args[0] = &lock; args[1] = &monitor; args[2] = 0; // create threads hythread_suspend_disable(); status = hythread_thin_monitor_enter(&lock); tf_assert_same(status, TM_ERROR_NONE); hythread_suspend_enable(); started_thread_count = 0; for(i = 0; i < THREAD_COUNT; i++) { thread_list[i] = NULL; status = hythread_create(&thread_list[i], 0, 0, 0, (hythread_entrypoint_t)start_proc, args); tf_assert_same(status, TM_ERROR_NONE); log_info("%d thread is started", i + 1); } // waiting start of tested thread hythread_suspend_disable(); while (started_thread_count < 10) { status = hythread_thin_monitor_wait(&lock); tf_assert_same(status, TM_ERROR_NONE); } status = hythread_thin_monitor_exit(&lock); tf_assert_same(status, TM_ERROR_NONE); hythread_suspend_enable(); // suspend tested thread status = hythread_suspend_all(NULL, ((HyThread_public*)hythread_self())->group); tf_assert_same(status, TM_ERROR_NONE); log_info("all threads are suspended"); // notify tested threads hythread_suspend_disable(); status = hythread_thin_monitor_enter(&monitor); tf_assert_same(status, TM_ERROR_NONE); status = hythread_thin_monitor_notify_all(&monitor); tf_assert_same(status, TM_ERROR_NONE); status = hythread_thin_monitor_exit(&monitor); tf_assert_same(status, TM_ERROR_NONE); hythread_suspend_enable(); log_info("notify all suspended threads"); // check tested argument for(i = 0; i < 1000; i++) { tf_assert_same(args[2], 0); hythread_sleep(1); } // resume thread status = hythread_resume_all(((HyThread_public*)hythread_self())->group); tf_assert_same(status, TM_ERROR_NONE); log_info("resume all suspended threads"); for(i = 0; i < THREAD_COUNT; i++) { test_thread_join(thread_list[i], i); log_info("%d thread is terminated", i + 1); } tf_assert_same((IDATA)args[2], THREAD_COUNT); return 0; }
int test_hythread_thread_suspend(void){ void **args; hythread_t thread = NULL; hythread_thin_monitor_t lock; hythread_thin_monitor_t monitor; IDATA status; int i; // create monitors status = hythread_thin_monitor_create(&lock); tf_assert_same(status, TM_ERROR_NONE); status = hythread_thin_monitor_create(&monitor); tf_assert_same(status, TM_ERROR_NONE); // alloc and set thread start procedure args args = (void**)calloc(3, sizeof(void*)); args[0] = &lock; args[1] = &monitor; args[2] = 0; // create thread hythread_suspend_disable(); status = hythread_thin_monitor_enter(&lock); tf_assert_same(status, TM_ERROR_NONE); hythread_suspend_enable(); status = hythread_create(&thread, 0, 0, 0, (hythread_entrypoint_t)start_proc, args); tf_assert_same(status, TM_ERROR_NONE); // waiting start of tested thread hythread_suspend_disable(); status = hythread_thin_monitor_wait(&lock); tf_assert_same(status, TM_ERROR_NONE); status = hythread_thin_monitor_exit(&lock); tf_assert_same(status, TM_ERROR_NONE); hythread_suspend_enable(); // suspend tested thread status = hythread_suspend_other(thread); tf_assert_same(status, TM_ERROR_NONE); // notify tested thread hythread_suspend_disable(); status = hythread_thin_monitor_enter(&monitor); tf_assert_same(status, TM_ERROR_NONE); status = hythread_thin_monitor_notify_all(&monitor); tf_assert_same(status, TM_ERROR_NONE); status = hythread_thin_monitor_exit(&monitor); tf_assert_same(status, TM_ERROR_NONE); hythread_suspend_enable(); // check tested argument for(i = 0; i < 1000; i++) { tf_assert_same(args[2], 0); hythread_sleep(1); } // resume thread hythread_resume(thread); test_thread_join(thread, 1); tf_assert_same((IDATA)args[2], 1); return 0; }