Skip to content

Commit

Permalink
Implement java.lang.Thread.isAlive native method
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Feb 8, 2024
1 parent b4c0afb commit 5aaa36a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/Library/java/lang/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS)
{
registerNative("java/lang/Thread/currentThread", "()Ljava/lang/Thread;", lib_java_lang_Thread_currentThread);
registerNative("java/lang/Thread/setPriority0", "(I)V", lib_java_lang_Thread_setPriority0);
registerNative("java/lang/Thread/isAlive", "()Z", lib_java_lang_Thread_isAlive);
}

JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS)
Expand All @@ -41,8 +42,6 @@ JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS)
JCALL void lib_java_lang_Thread_setPriority0(NATIVE_ARGS)
{
const Object* threadObject = getThisObjectReference(thread, heap, VM);


const StackFrame* currentFrame = thread->m_currentFrame;
const Variable argument = currentFrame->localVariables[1];
VM->checkType(argument, VariableType_INT, thread);
Expand All @@ -51,3 +50,21 @@ JCALL void lib_java_lang_Thread_setPriority0(NATIVE_ARGS)
field->data->data = argument.data;
}

JCALL void lib_java_lang_Thread_isAlive(NATIVE_ARGS)
{
const StackFrame* currentFrame = thread->m_currentFrame;
const Variable var = currentFrame->localVariables[0];
VM->checkType(var, VariableType_REFERENCE, thread);
const u4 objectReference = var.data;
const VMThread* vmThread = VM->getVMThreadByObjectRef(objectReference);

bool alive = false;
if (vmThread != nullptr)
{
alive = vmThread->alive;
}

StackFrame* returnFrame = thread->getTopFrameNonNative();
returnFrame->pushInt(alive? 1 : 0);
}

3 changes: 2 additions & 1 deletion src/Library/java/lang/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS);
JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS);
JCALL void lib_java_lang_Thread_setPriority0(NATIVE_ARGS);
JCALL void lib_java_lang_Thread_setPriority0(NATIVE_ARGS);
JCALL void lib_java_lang_Thread_isAlive(NATIVE_ARGS);
9 changes: 9 additions & 0 deletions src/VM/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ class VM {
void shutdown();
void executeLoop(VMThread* thread);
static void checkType(Variable var, VariableType type, VMThread *thread);
VMThread* getVMThreadByObjectRef(const u4 objectReference)
{
if (m_mainThread.threadObject == objectReference)
{
return &m_mainThread;
}

return nullptr;
}
private:
inline static constexpr std::array<Instruction,110> m_instructions{{
// Constants
Expand Down
1 change: 1 addition & 0 deletions src/VM/VMThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class VMThread
std::string_view m_name;
u4 threadObject{0};
i1 priority{5};
bool alive{false};

explicit VMThread(const std::string_view name, const size_t frameSize) noexcept
: m_stack(frameSize), m_name(name)
Expand Down

0 comments on commit 5aaa36a

Please sign in to comment.