Skip to content

Commit fd1909a

Browse files
author
karic
committed
Add Thread support
1 parent 0ecc7ba commit fd1909a

File tree

11 files changed

+219
-5
lines changed

11 files changed

+219
-5
lines changed

include/Thread.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*************************
2+
* file: Thread.h
3+
* desc: define Thread
4+
* author: Kari.Zhang
5+
* date: 2017-12-14
6+
************************/
7+
8+
#ifndef __THREAD__H__
9+
#define __THREAD__H__
10+
11+
#ifdef linux
12+
#include <pthread.h>
13+
#endif
14+
15+
#ifdef WIN32
16+
#include <windows.h>
17+
#endif
18+
19+
#include "comm.h"
20+
21+
typedef void* (*ThreadRoutine)(void*);
22+
typedef struct Thread Thread;
23+
24+
Thread* createThread(ThreadRoutine func, void *param);
25+
bool startThread(Thread* thread);
26+
void destroyThread(Thread* thread);
27+
bool isThreadValid(const Thread* thread);
28+
29+
#ifdef linux
30+
pthread_t getThreadId(const Thread* thread);
31+
#endif
32+
33+
#ifdef WIN32
34+
HANDLE getThreadHandle(const Thread* thread);
35+
#endif
36+
37+
#endif

include/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "class.h"
1414
#include "runtime.h"
1515

16+
extern void* engineRoutine(void *env);
1617
extern void executeMethod(ExecEnv *env, const MethodEntry *method);
1718
extern void executeMethod_spec(ExecEnv *env, const MethodEntry *method);
1819

include/gc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ void gcDestroy(gc_context* gc);
3232

3333
extern void gcWork(gc_context* gc);
3434

35+
extern void* gcRoutine(void *param);
36+
3537
#endif

include/runtime.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
#include "class.h"
1515
#include "comm.h"
16+
#include "gc.h"
1617
#include "mem.h"
18+
#include "Thread.h"
1719

1820
/*
1921
* Store system property
@@ -131,6 +133,9 @@ typedef struct ExecEnv {
131133
Class **rtClsArea; // runtime class address list
132134
MethodEntry *mainMethod;// user class main()
133135
void* dl_handle; // dynamic link library handle
136+
Thread* gcThread; // garbage collector thread
137+
Thread* ngThread; // engine thread
138+
gc_context* gcctx; // gc context
134139
} ExecEnv;
135140

136141
typedef struct VM {

java/lang/Runnable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
public interface Runnable {
3+
void run();
4+
}

java/lang/Thread.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
public class Thread implements Runnable {
3+
4+
private Runnable target;
5+
6+
public Thread() {
7+
8+
}
9+
10+
public Thread(Runnable r) {
11+
target = r;
12+
}
13+
14+
private native void nativeCreate(Thread t);
15+
16+
public synchronized void start() {
17+
nativeCreate(this);
18+
}
19+
20+
@Override
21+
public void run() {
22+
if (target != null) {
23+
target.run();
24+
}
25+
}
26+
}

src/vm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ INCLUDE_DIRECTORIES(${libjar_dir} ${libvmbase_dir})
44

55
AUX_SOURCE_DIRECTORY(. src_files)
66
ADD_LIBRARY(libvmbase ${src_files})
7-
TARGET_LINK_LIBRARIES(libvmbase libminiz -ldl)
7+
TARGET_LINK_LIBRARIES(libvmbase libminiz -ldl -lpthread)

src/vm/Thread.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*************************
2+
* file: Thread.c
3+
* desc: define Thread
4+
* author: Kari.Zhang
5+
* date: 2017-12-14
6+
************************/
7+
8+
#include <assert.h>
9+
#include <stdlib.h>
10+
11+
#ifdef linux
12+
#include <pthread.h>
13+
#endif
14+
15+
#ifdef WIN32
16+
#include <windows.h>
17+
#endif
18+
19+
#include "Thread.h"
20+
21+
typedef struct Thread {
22+
#ifdef linux
23+
pthread_t pid;
24+
pthread_attr_t *attr;
25+
#endif
26+
27+
#ifdef WIN32
28+
HANDLE hThread;
29+
// thread attr
30+
#endif
31+
32+
ThreadRoutine func;
33+
void* param;
34+
35+
} Thread;
36+
37+
Thread* createThread(ThreadRoutine func, void* param)
38+
{
39+
assert(func && "func must not be NULL !");
40+
Thread* thread = (Thread *)calloc(1, sizeof(Thread));
41+
do {
42+
if (thread == NULL) {
43+
break;
44+
}
45+
thread->func = func;
46+
thread->param = param;
47+
} while (0);
48+
49+
return thread;
50+
}
51+
52+
bool startThread(Thread* thread) {
53+
assert(thread && "thread must not be NULL !");
54+
#ifdef linux
55+
int result = pthread_create(
56+
&thread->pid,
57+
thread->attr,
58+
thread->func,
59+
thread->param);
60+
61+
if (result) {
62+
free(thread);
63+
thread = NULL;
64+
return FALSE;
65+
}
66+
return TRUE;
67+
#endif
68+
69+
return FALSE;
70+
}
71+
72+
void destroyThread(Thread* thread)
73+
{
74+
assert(thread && "thread must not be NULL !");
75+
}
76+
77+
bool isThreadValid(const Thread* thread)
78+
{
79+
if (thread != NULL) {
80+
#ifdef linux
81+
return thread->pid != 0;
82+
#endif
83+
84+
#ifdef WIN32
85+
return thread->hThread != NULL;
86+
#endif
87+
}
88+
89+
return FALSE;
90+
}
91+
92+
#ifdef linux
93+
pthread_t getThreadId(const Thread* thread)
94+
{
95+
assert(thread && "thread must not be NULL !");
96+
return thread->pid;
97+
}
98+
#endif
99+
100+
#ifdef WIN32
101+
HANDLE getThreadHandle(const Thread* thread)
102+
{
103+
assert(thread && "thread must not be NULL !");
104+
return thread->hThread;
105+
}
106+
#endif

src/vm/engine.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,14 @@ void executeMethod_spec(ExecEnv *env, const MethodEntry *method)
249249
#endif
250250

251251
}
252+
253+
void* engineRoutine(void *param)
254+
{
255+
assert(param);
256+
ExecEnv *env = (ExecEnv *)param;
257+
JavaStack *stack = env->javaStack;
258+
// TODO
259+
printf("java stack top:%d\n", stack->top);
260+
261+
return NULL;
262+
}

src/vm/gc.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gc_context* gcCreate(MemoryArea *mem) {
2828
assert (NULL != mem);
2929
gc_context *gc = (gc_context *)calloc(1, sizeof(gc_context));
3030
if (NULL != gc) {
31-
gc->mem = mem;
31+
gc->mem = mem;
3232
}
3333

3434
return gc;
@@ -39,14 +39,27 @@ gc_context* gcCreate(MemoryArea *mem) {
3939
*/
4040
void gcDestroy(gc_context* gc) {
4141
if (NULL != gc) {
42-
free (gc);
43-
gc = NULL;
42+
free (gc);
43+
gc = NULL;
4444
}
4545
}
4646

4747
/*
4848
* gc start to work
4949
*/
5050
extern void gcWork(gc_context* gc) {
51+
// TODO
52+
printf("gc works!\n");
53+
}
54+
55+
extern void* gcRoutine(void *param)
56+
{
57+
assert(param);
58+
ExecEnv* env = (ExecEnv *)param;
59+
if (env->gcctx == NULL){
60+
env->gcctx = gcCreate(env->heapArea);
61+
}
62+
gcWork(env->gcctx);
5163

64+
return NULL;
5265
}

0 commit comments

Comments
 (0)