Ejemplo n.º 1
0
/*
 * 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_holds_lock(void) {

    tested_thread_sturct_t *tts;
    tested_thread_sturct_t *critical_tts = 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_holds_lock);
    

    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(jthread_holds_lock(tts->java_thread, tts->monitor) > 0);
                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);
                tf_assert(jthread_holds_lock(tts->java_thread, tts->monitor) == 0);
                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);
        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_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_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;
}