/** * Initializes Java monitor. * * Monitor is a recursive lock with one conditional variable associated with it. * Implementation may use the knowledge of internal object layout in order to allocate lock * and conditional variable in the most efficient manner. * * @param[in] monitor object where monitor needs to be initialized. */ IDATA VMCALL jthread_monitor_init(jobject monitor) { assert(monitor); hythread_suspend_disable(); hythread_thin_monitor_t *lockword = vm_object_get_lockword_addr(monitor); IDATA status = hythread_thin_monitor_create(lockword); hythread_suspend_enable(); return status; } // jthread_monitor_init
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; }