Skip to content

Commit

Permalink
update thread source, optimize java threadlock function
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalgust committed Feb 27, 2024
1 parent c82fba9 commit ed3a1e7
Show file tree
Hide file tree
Showing 6 changed files with 829 additions and 408 deletions.
2 changes: 1 addition & 1 deletion minijvm/c/jvm/jdwp.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ s32 jdwp_start_server(MiniJVM *jvm) {
jdwpserver->event_sets = pairlist_create(32);
jdwpserver->runtime_jdwp = runtime_create(jvm);
jdwpserver->runtime_jdwp->thrd_info->type = THREAD_TYPE_JDWP;
mtx_init(&jdwpserver->event_sets_lock, mtx_recursive);
mtx_init(&jdwpserver->event_sets_lock, mtx_recursive | mtx_timed);
jvm->jdwpserver = jdwpserver;

thrd_create(&jdwpserver->pt_listener, jdwp_thread_listener, jdwpserver);
Expand Down
1 change: 1 addition & 0 deletions minijvm/c/jvm/jni_std.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ s32 java_lang_System_loadLibrary0(Runtime *runtime, JClass *clazz) {
f = (jni_fun) fp;
f(runtime->jvm);
}
break;
}

#else
Expand Down
30 changes: 19 additions & 11 deletions minijvm/c/jvm/jvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void thread_stop_all(MiniJVM *jvm) {
void thread_lock_init(ThreadLock *lock) {
if (lock) {
cnd_init(&lock->thread_cond);
mtx_init(&lock->mutex_lock, mtx_recursive);
mtx_init(&lock->mutex_lock, mtx_recursive | mtx_timed);
}
}

Expand Down Expand Up @@ -996,20 +996,28 @@ s32 jthread_lock(MemoryBlock *mb, Runtime *runtime) { //可能会重入,同一
jthreadlock_create(runtime, mb);
}
ThreadLock *jtl = mb->thread_lock;
s32 i = 0;
#if _JVM_DEBUG_LOG_LEVEL > 5
s64 waitTime = currentTimeMillis();
#endif
struct timespec t;
t.tv_nsec = 5 * NANO_2_MILLS_SCALE;
t.tv_sec = 0;
//can pause when lock
while (mtx_trylock(&jtl->mutex_lock) != thrd_success) {
while (mtx_timedlock(&jtl->mutex_lock, &t) != thrd_success) {
check_suspend_and_pause(runtime);
if (runtime->thrd_info->type == THREAD_TYPE_JDWP) {
break;
}
//jthread_yield(runtime);
jthread_waitTime(mb, runtime, 20);
jthread_yield(runtime);
i++;
}

#if _JVM_DEBUG_LOG_LEVEL > 5
invoke_deepth(runtime);
jvm_printf(" lock: %llx lock holder: %s \n", (s64) (intptr_t) (runtime->thrd_info->jthread),
utf8_cstr(mb->clazz->name));
if (i > 0) {
waitTime = currentTimeMillis() - waitTime;
invoke_deepth(runtime);
jvm_printf(" lock holder: %s , lock count: %d waitTime: %lld \n", utf8_cstr(mb->clazz->name), i, waitTime);
}
#endif
return 0;
}
Expand Down Expand Up @@ -1851,9 +1859,9 @@ s64 nanoTime() {
s64 threadSleep(s64 ms) {
//wait time
struct timespec req;
clock_gettime(CLOCK_REALTIME, &req);
req.tv_sec += ms / MILL_2_SEC_SCALE;
req.tv_nsec += (ms % MILL_2_SEC_SCALE) * NANO_2_MILLS_SCALE;
//clock_gettime(CLOCK_REALTIME, &req);
req.tv_sec = ms / MILL_2_SEC_SCALE;
req.tv_nsec = (ms % MILL_2_SEC_SCALE) * NANO_2_MILLS_SCALE;
//if notify or notifyall ,the thread is active again, rem record remain wait time
struct timespec rem;
rem.tv_sec = 0;
Expand Down
14 changes: 7 additions & 7 deletions minijvm/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,33 @@ int main(int argc, char **argv) {
//compiler test
// utf8_append(cp, startup_dir);
// utf8_append_c(cp, "../libex/janino.jar");
utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, PATHSEPARATOR);
// utf8_append(cp, startup_dir);
// utf8_append_c(cp, "../libex/commons-compiler.jar");
utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, PATHSEPARATOR);
// classpath = (c8 *) utf8_cstr(cp);
// main_name = "org.codehaus.janino.Compiler";
// arraylist_push_back(java_para,"../res/BpDeepTest.java");

//test awtk
// utf8_append(cp, startup_dir);
// utf8_append_c(cp, "../libex/awtk_gui.jar");
utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, PATHSEPARATOR);
// utf8_append(cp, startup_dir);
// utf8_append_c(cp, "../libex/awtk_demos.jar");
utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, "./");
utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, PATHSEPARATOR);
// classpath = (c8 *) utf8_cstr(cp);
// main_name = "DemoBasic";
// main_name = "DemoButton";

//test luaj
// utf8_append(cp, startup_dir);
// utf8_append_c(cp, "../libex/luncher.jar");
utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, "./");
utf8_append_c(cp, PATHSEPARATOR);
// utf8_append_c(cp, PATHSEPARATOR);
// classpath = (c8 *) utf8_cstr(cp);
// main_name = "org.luaj.vm2.lib.jme.TestLuaJ";

Expand Down
Loading

0 comments on commit ed3a1e7

Please sign in to comment.