#include "vm/types.h"
-#include "mm/memory.h"
+#include "mm/memory.hpp"
#if defined(ENABLE_GC_BOEHM)
/* We need to include Boehm's gc.h here for GC_register_my_thread and
case THREAD_STATE_TIMED_WAITING:
printf(" waiting on condition");
break;
+ case THREAD_STATE_PARKED:
+ printf(" parked");
+ break;
+ case THREAD_STATE_TIMED_PARKED:
+ printf(" timed parked");
+ break;
case THREAD_STATE_TERMINATED:
printf(" terminated");
break;
}
+/**
+ * Set the current state of the given thread. This method should only
+ * be called while holding the threadlist-lock and after checking that
+ * the new state is valid. It is best to not call this method directly
+ * but call the specific setter methods below.
+ */
+static inline void thread_set_state(threadobject *t, int state)
+{
+ // Set the state of our internal threadobject.
+ t->state = state;
+
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+ // Set the state of the java.lang.Thread object.
+ java_lang_Thread thread(thread_get_object(t));
+ assert(thread.is_non_null());
+ thread.set_threadStatus(state);
+#endif
+}
+
+
/* thread_set_state_runnable ***************************************************
Set the current state of the given thread to THREAD_STATE_RUNNABLE.
ThreadList::lock();
if (t->state != THREAD_STATE_TERMINATED) {
- t->state = THREAD_STATE_RUNNABLE;
+ thread_set_state(t, THREAD_STATE_RUNNABLE);
DEBUGTHREADS("is RUNNABLE", t);
}
ThreadList::lock();
if (t->state != THREAD_STATE_TERMINATED) {
- t->state = THREAD_STATE_WAITING;
+ thread_set_state(t, THREAD_STATE_WAITING);
DEBUGTHREADS("is WAITING", t);
}
ThreadList::lock();
if (t->state != THREAD_STATE_TERMINATED) {
- t->state = THREAD_STATE_TIMED_WAITING;
+ thread_set_state(t, THREAD_STATE_TIMED_WAITING);
DEBUGTHREADS("is TIMED_WAITING", t);
}
}
+/* thread_set_state_parked *****************************************************
+
+ Set the current state of the given thread to THREAD_STATE_PARKED.
+
+ NOTE: If the thread has already terminated, don't set the state.
+ This is important for threads_detach_thread.
+
+*******************************************************************************/
+
+void thread_set_state_parked(threadobject *t)
+{
+ /* Set the state inside a lock. */
+
+ ThreadList::lock();
+
+ if (t->state != THREAD_STATE_TERMINATED) {
+ thread_set_state(t, THREAD_STATE_PARKED);
+
+ DEBUGTHREADS("is PARKED", t);
+ }
+
+ ThreadList::unlock();
+}
+
+
+/* thread_set_state_timed_parked ***********************************************
+
+ Set the current state of the given thread to THREAD_STATE_TIMED_PARKED.
+
+ NOTE: If the thread has already terminated, don't set the state.
+ This is important for threads_detach_thread.
+
+*******************************************************************************/
+
+void thread_set_state_timed_parked(threadobject *t)
+{
+ /* Set the state inside a lock. */
+
+ ThreadList::lock();
+
+ if (t->state != THREAD_STATE_TERMINATED) {
+ thread_set_state(t, THREAD_STATE_TIMED_PARKED);
+
+ DEBUGTHREADS("is TIMED_PARKED", t);
+ }
+
+ ThreadList::unlock();
+}
+
+
/* thread_set_state_terminated *************************************************
Set the current state of the given thread to
ThreadList::lock();
- t->state = THREAD_STATE_TERMINATED;
+ thread_set_state(t, THREAD_STATE_TERMINATED);
DEBUGTHREADS("is TERMINATED", t);
case THREAD_STATE_BLOCKED:
case THREAD_STATE_WAITING:
case THREAD_STATE_TIMED_WAITING:
+ case THREAD_STATE_PARKED:
+ case THREAD_STATE_TIMED_PARKED:
return true;
default:
return false;
}
+/* thread_is_interrupted *******************************************************
+
+ Check if the given thread has been interrupted.
+
+ ARGUMENTS:
+ t ... the thread to check
+
+ RETURN VALUE:
+ true, if the given thread had been interrupted
+
+*******************************************************************************/
+
+bool thread_is_interrupted(threadobject *t)
+{
+ /* We need the mutex because classpath will call this function when
+ a blocking system call is interrupted. The mutex ensures that it will
+ see the correct value for the interrupted flag. */
+
+ t->waitmutex->lock();
+ bool interrupted = t->interrupted;
+ t->waitmutex->unlock();
+
+ return interrupted;
+}
+
+
+/* thread_set_interrupted ******************************************************
+
+ Set the interrupted flag to the given value.
+
+ ARGUMENTS:
+ interrupted ... value to set
+
+*******************************************************************************/
+
+void thread_set_interrupted(threadobject *t, bool interrupted)
+{
+ t->waitmutex->lock();
+ t->interrupted = interrupted;
+ t->waitmutex->unlock();
+}
+
+
/*
* These are local overrides for various environment variables in Emacs.