00001
00010 #include "thread.h"
00011
00012 #ifdef __cplusplus
00013 extern "C" {
00014 #endif
00015
00023 jobject getThreadObject(Thread* thread) {
00024 return thread->thread.thread;
00025 }
00026
00032 void startThread(Thread* thread) {
00033 callVoidMethod(thread->thread.thread, thread->thread.mstart);
00034 }
00035
00044 void setThreadPriority(Thread* thread, jint priority) {
00045 callVoidMethod(thread->thread.thread, thread->thread.msetPriority, priority);
00046 }
00047
00057 jint getThreadPriority(Thread* thread) {
00058 return callIntMethod(thread->thread.thread, thread->thread.mgetPriority);
00059 }
00060
00067 void sleepThread(Thread* thread, jlong milliseconds) {
00068 callVoidMethod(thread->thread.thread, thread->thread.msleep, milliseconds);
00069 }
00070
00077 void yieldThread(Thread* thread) {
00078 callVoidMethod(thread->thread.thread, thread->thread.myield);
00079 }
00080
00089 void cbThreadRun(JNIEnv *env, jobject jobj, jlong ptr) {
00090 void (*func)() = (void (*)())ptr;
00091 func();
00092 }
00093
00099 void initThread_(Thread_* thread) {
00100 thread->getObject = &getThreadObject;
00101 thread->start = &startThread;
00102 thread->setPriority = &setThreadPriority;
00103 thread->getPriority = &getThreadPriority;
00104 thread->sleep = &sleepThread;
00105 thread->yield = &yieldThread;
00106 }
00107
00115 void createThread_(Thread* thread, void (*func)()) {
00116 JNINativeMethod nm;
00117
00118 thread->thread.clsthread = findClass(CLS_THREAD);
00119 thread->thread.mstart = findMethod(thread->thread.clsthread, "start", "()V");
00120 thread->thread.mgetPriority = findMethod(thread->thread.clsthread, "getPriority", "()I");
00121 thread->thread.msetPriority = findMethod(thread->thread.clsthread, "setPriority", "(I)V");
00122 thread->thread.msleep = findStaticMethod(thread->thread.clsthread, "sleep", "(J)V");
00123 thread->thread.myield = findStaticMethod(thread->thread.clsthread, "yield", "()V");
00124
00125 nm.name = "cbThreadRun";
00126 nm.signature = "(J)V";
00127 nm.fnPtr = (void*)&cbThreadRun;
00128 registerNatives(thread->thread.clsthread, &nm, 1);
00129
00130 thread->thread.thread = newObjectBySignature(thread->thread.clsthread, "(J)V", func);
00131 }
00132
00140 void createThreadCPP(ThreadCPP_* thread, void (*func)()) {
00141 thread->functions = (Thread_*)malloc(sizeof(Thread_));
00142 initThread_(thread->functions);
00143 createThread_((Thread*)thread, func);
00144 }
00145
00153 void createThreadC(Thread_* thread, void (*func)()) {
00154 initThread_(thread);
00155 createThread_((Thread*)thread, func);
00156 }
00157
00163 void deleteThread_(Thread* thread) {
00164 deleteGlobalReference(thread->thread.thread);
00165 deleteGlobalReference(thread->thread.clsthread);
00166 }
00167
00173 void deleteThreadCPP(ThreadCPP_* thread) {
00174 deleteThread_((Thread*)thread);
00175 free(thread->functions);
00176 }
00177
00183 void deleteThreadC(Thread_* thread) {
00184 deleteThread_((Thread*)thread);
00185 }
00186
00187 #ifdef __cplusplus
00188 }
00189 #endif
00190
00191